Use this trigger will send email alert to email
USE [msdb]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
----------------------------------------------------------------------------
-- Object Type : job failure alert
----------------------------------------------------------------------------
create trigger [dbo].[trg_stepfailures]
on [dbo].[sysjobhistory]
for insert
as
declare @strMsg varchar(400),
@strRecipient varchar(128),
@copyRecipient varchar(128),
@bccRecipient varchar(128)
set @strRecipient = 'dinesh@t.com'
set @copyRecipient = 'dinesh@t.com'
set @bccRecipient = 'dinesh@t.com'
if exists (select * from inserted where run_status = 0 and step_name <> '(job outcome)')
begin
select @strMsg =
convert(char(10),'Server') + char(58) + @@servername +
char(10) +
convert(char(10),'Job') + char(58) + convert(varchar(50), sysjobs.name) +
char(10) +
convert(char(10),'Step') + char(58) + convert(varchar(50), inserted.step_name)+
char(10) +
convert(char(10),'Message') + char(58) + convert(varchar(150), inserted.message)
from inserted
join sysjobs
on inserted.job_id = sysjobs.job_id
where inserted.run_status = 0
raiserror (@strMsg, 16, 10) with log
exec msdb.dbo.sp_send_dbmail
@profile_name = N'SQL DBA SUPPORT',
@recipients = @strRecipient,
@copy_recipients = @copyRecipient,
@blind_copy_recipients = @bccRecipient,
@body = @strMsg,
@subject = 'Job Failure on server'
end
-
Edited by
Dinesh Vishe
21 hours 40 minutes ago