USE [msdb];
GO
SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO
/* drop trigger trg_stepfailures */
Alter TRIGGER [dbo].[trg_stepfailures] ON [dbo].[sysjobhistory]
FOR INSERT
AS
DECLARE @strcmd VARCHAR(800) ,
@strRecipient VARCHAR(500) ,
@strMsg VARCHAR(2000) ,
@strServer VARCHAR(255) ,
@strTo VARCHAR(255);
DECLARE @Subject VARCHAR(500);
IF EXISTS ( SELECT *
FROM inserted
WHERE run_status = 0
AND step_name NOT IN ('job outcome'))
BEGIN
SELECT @strMsg = @@servername + '-Job: ' + sysjobs.name
+ '. Step= ' + inserted.step_name + 'Message '
+ inserted.message
FROM inserted
JOIN sysjobs ON inserted.job_id = sysjobs.job_id
WHERE inserted.run_status = 0;
SELECT @Subject = 'Job ' + sysjobs.name + ' Failed on Job Server'
+ @@Servername
FROM inserted
JOIN sysjobs ON inserted.job_id = sysjobs.job_id
WHERE inserted.run_status = 0;
SET @strRecipient = 'naresh.koudagani@xyz.com';
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'DbMail', --uses the default profile
@recipients = @strRecipient,
@subject = @Subject,
@body = @strMsg,
@body_format = 'HTML'; --default is TEXT
END;
GO
SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO
/* drop trigger trg_stepfailures */
Alter TRIGGER [dbo].[trg_stepfailures] ON [dbo].[sysjobhistory]
FOR INSERT
AS
DECLARE @strcmd VARCHAR(800) ,
@strRecipient VARCHAR(500) ,
@strMsg VARCHAR(2000) ,
@strServer VARCHAR(255) ,
@strTo VARCHAR(255);
DECLARE @Subject VARCHAR(500);
IF EXISTS ( SELECT *
FROM inserted
WHERE run_status = 0
AND step_name NOT IN ('job outcome'))
BEGIN
SELECT @strMsg = @@servername + '-Job: ' + sysjobs.name
+ '. Step= ' + inserted.step_name + 'Message '
+ inserted.message
FROM inserted
JOIN sysjobs ON inserted.job_id = sysjobs.job_id
WHERE inserted.run_status = 0;
SELECT @Subject = 'Job ' + sysjobs.name + ' Failed on Job Server'
+ @@Servername
FROM inserted
JOIN sysjobs ON inserted.job_id = sysjobs.job_id
WHERE inserted.run_status = 0;
SET @strRecipient = 'naresh.koudagani@xyz.com';
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'DbMail', --uses the default profile
@recipients = @strRecipient,
@subject = @Subject,
@body = @strMsg,
@body_format = 'HTML'; --default is TEXT
END;
No comments:
Post a Comment