SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Email Failed SQL Jobs


Email Failed SQL Jobs

Author
Message
SQLAssAS
SQLAssAS
SSCarpal Tunnel
SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)SSCarpal Tunnel (4K reputation)

Group: General Forum Members
Points: 4046 Visits: 1385
Comments posted to this topic are about the item Email Failed SQL Jobs
Bert De Haes
Bert De Haes
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1769 Visits: 233
I changed the code and made it a stored procedure in my dba database.
I also added a filter to list only enabled jobs.



if object_id('dbo.usp_mailFailedJobs') is null exec('create procedure dbo.usp_mailFailedJobs as begin;return;end;')
go
alter procedure dbo.usp_mailFailedJobs
(
@recipients varchar(max)
,@profile_name sysname = null
,@subject nvarchar(255) = @@servername
) as
begin
set nocount on;
declare @body nvarchar(max) = cast
(
(
select
J.name as 'td',''
,S.last_run_outcome as 'td',''
,S.last_outcome_message as 'td',''
,S.last_run_date as 'td',''
from msdb.dbo.sysjobs as J
join msdb.dbo.sysjobservers as S on S.job_id = J.job_id
where S.last_outcome_message like '%failed%'
and J.enabled = 1
for xml path('tr'),type
)
as nvarchar(max)
);

if @body is not null
begin;
set @body = concat
(
N'<html><body><H3>Failed jobs</H3>'
,N'<table border="1">'
,N'<tr><th>Job Name</th><th>Last Run Outcome</th><th>Last Outcome Message</th><th>Last Run Date</th></tr>'
,@body
,N'</table></body></html>'
);
exec msdb.dbo.sp_send_dbmail
@profile_name = @profile_name
,@recipients = @recipients
,@subject = @subject
,@body = @body
,@body_format = 'HTML';
end;
end;
go

Jorge Serres
Jorge Serres
SSC-Enthusiastic
SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)

Group: General Forum Members
Points: 174 Visits: 201
Tables should be fully qualified, otherwise it'll generate some blood! ....from msdb..sysjobs inner join msdb..sysjobservers;
A quote needs to be closed after the word HTML'
Vimal Lohani
Vimal Lohani
SSC Eights!
SSC Eights! (896 reputation)SSC Eights! (896 reputation)SSC Eights! (896 reputation)SSC Eights! (896 reputation)SSC Eights! (896 reputation)SSC Eights! (896 reputation)SSC Eights! (896 reputation)SSC Eights! (896 reputation)

Group: General Forum Members
Points: 896 Visits: 661
Good script :-)
Either use
USE msdb
GO
in starting or use msdb.sys.....

Vimal Lohani
SQL DBA | MCP (70-461,70-462)

==============================
The greatest barrier to success is the fear of failure
** Success is a journey not a destination
**

Think before you print, SAVE TREES, Protect Mother Nature
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search