Script to find failed sql agent jobs and notify via email

This is one of the useful script to check and find failed jobs on sql server every day and send notification via email.

USE MSDB
GO

create table #Failed_Jobs (
[Status] [varchar](6) NOT NULL,
[Job Name] [varchar](100) NULL,
[Step ID] [varchar](5) NULL,
[Step Name] [varchar](30) NULL,
[Start Date Time] [varchar](30) NULL,
[Message] [nvarchar](4000) NULL)

insert into #Failed_Jobs
select 'FAILED' as Status, cast(sj.name as varchar(100)) as "Job Name",
cast(sjs.step_id as varchar(5)) as "Step ID",
cast(sjs.step_name as varchar(30)) as "Step Name",
cast(REPLACE(CONVERT(varchar,convert(datetime,convert(varchar,sjh.run_date)),102),'.','-')+' '+SUBSTRING(RIGHT('000000'+CONVERT(varchar,sjh.run_time),6),1,2)+':'+SUBSTRING(RIGHT('000000'+CONVERT(varchar,sjh.run_time),6),3,2)+':'+SUBSTRING(RIGHT('000000'+CONVERT(varchar,sjh.run_time),6),5,2) as varchar(30)) 'Start Date Time',
sjh.message as "Message"
from sysjobs sj
join sysjobsteps sjs
on sj.job_id = sjs.job_id
join sysjobhistory sjh
on sj.job_id = sjh.job_id and sjs.step_id = sjh.step_id
where sjh.run_status <> 1
and cast(sjh.run_date as float)*1000000+sjh.run_time >
cast(convert(varchar(8), getdate()-1, 112) as float)*1000000+70000 --yesterday at 7am
union
select 'FAILED',cast(sj.name as varchar(100)) as "Job Name",
'MAIN' as "Step ID",
'MAIN' as "Step Name",
cast(REPLACE(CONVERT(varchar,convert(datetime,convert(varchar,sjh.run_date)),102),'.','-')+' '+SUBSTRING(RIGHT('000000'+CONVERT(varchar,sjh.run_time),6),1,2)+':'+SUBSTRING(RIGHT('000000'+CONVERT(varchar,sjh.run_time),6),3,2)+':'+SUBSTRING(RIGHT('000000'+CONVERT(varchar,sjh.run_time),6),5,2) as varchar(30)) 'Start Date Time',
sjh.message as "Message"
from sysjobs sj
join sysjobhistory sjh
on sj.job_id = sjh.job_id
where sjh.run_status <> 1 and sjh.step_id=0
and cast(sjh.run_date as float)*1000000+sjh.run_time >
cast(convert(varchar(8), getdate()-1, 112) as float)*1000000+70000 --yesterday at

declare @cnt int
select @cnt=COUNT(1) from #Failed_Jobs
if (@cnt > 0)
begin

declare @strsubject varchar(100)
declare @oper_email nvarchar(100)

set @oper_email = (select email_address from msdb.dbo.sysoperators where name = 'DBA')
select @strsubject='Check the following failed jobs on ' + @@SERVERNAME

declare @tableHTML nvarchar(max);
set @tableHTML =
N'<H1>Failed Jobs Listing - ' + @@SERVERNAME +'</H1>' +
N'
<table border="1">' +
N'
<tr>
<th>Status</th>
<th>Job Name</th>
' +
N'
<th>Step ID</th>
<th>Step Name</th>
<th>Start Date</th>
' +
N'
<th>Message</th>
</tr>
' +
CAST ( ( SELECT td = [Status], '',
td = [Job Name], '',
td = [Step ID], '',
td = [Step Name], '',
td = [Start Date Time], '',
td = [Message]
FROM #Failed_Jobs
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) ) +
N'</table>
' ;

EXEC msdb.dbo.sp_send_dbmail
@from_address='',
@recipients= @oper_email,
@subject = @strsubject,
@body = @tableHTML,
@body_format = 'HTML',
@profile_name='DBAProfile'
end

drop table #Failed_Jobs

GO

 

Cheers

Hope you like the post!

Ramasankar Molleti

LinkedIn: LinkedIn Profile

Twitter: Twitter

 

Advertisements

About Ramasankar

My name is Ramasankar Molleti, and I have been working on sql server over 10 years, primarily focusing on Database Administration, Architect , Development and Business intelligence of sql server. I also hold various Microsoft certifications (Microsoft Certified Information Technology Professional, Microsoft Certified Technology Specialist, and Microsoft Certified Solution Associate). Disclaimer The views expressed on this website/blog are mine alone and do not reflect the views of my company. All postings on this blog are provided “AS IS” with no warranties, and confers no rights. The owner of https://sqlram.wordpress.com will not be liable for any errors or omissions in this information nor for the availability of this information. The owner will not be liable for any losses, injuries, or damages from the display or use of this information.
This entry was posted in Database Administration, T-SQL. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s