<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title> &#187; Most Recent Job Status</title>
	<atom:link href="http://benchmarkitconsulting.com/tag/most-recent-job-status/feed/" rel="self" type="application/rss+xml" />
	<link>http://benchmarkitconsulting.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 May 2012 12:54:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Most Recent Job Status of All Enabled Jobs</title>
		<link>http://benchmarkitconsulting.com/colin-stasiuk/2009/11/17/most-recent-job-status-of-all-enabled-jobs/</link>
		<comments>http://benchmarkitconsulting.com/colin-stasiuk/2009/11/17/most-recent-job-status-of-all-enabled-jobs/#comments</comments>
		<pubDate>Tue, 17 Nov 2009 18:50:03 +0000</pubDate>
		<dc:creator>Colin Stasiuk</dc:creator>
				<category><![CDATA[Benchmark IT Consulting]]></category>
		<category><![CDATA[Colin Stasiuk]]></category>
		<category><![CDATA[Monitoring]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Central Management Server]]></category>
		<category><![CDATA[CMS]]></category>
		<category><![CDATA[Job Status]]></category>
		<category><![CDATA[Jobs]]></category>
		<category><![CDATA[Most Recent Job Status]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://benchmarkitconsulting.com/?p=1255</guid>
		<description><![CDATA[ Seems easy enough&#8230; and maybe it is easy and someone is going to comment with something fantastical *hint hint hint* but until that happens&#8230; I would like&#8230; if I may&#8230; to take you on a strange journey Use Case: I want to be able (using CMS- Central Management Server)  connect to all my SQL Servers (2000, [...]]]></description>
			<content:encoded><![CDATA[<p> Seems easy enough&#8230; and maybe it is easy and someone is going to comment with something fantastical *hint hint hint* but until that happens&#8230; I would like&#8230; if I may&#8230; to take you on a strange journey <img src='http://benchmarkitconsulting.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>Use Case</strong>: I want to be able (using CMS- Central Management Server)  connect to all my SQL Servers (2000, 2005, and 2008) and get the most recent job execution status for all enabled SQL Server Agent Jobs.</p>
<p>At first I thought I was going to be smart (keep your jokes to yourself and try to concentrate <img src='http://benchmarkitconsulting.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  )&#8230;. so like I was saying&#8230; at first I thought I was going to be smart and create a temp table to store the value of sp_help_job and then work with the data from there.</p>
<p>Apparently sp_help_job uses INSERT EXEC itself so when I tried to run my fancy code I got this:</p>
<div><span style="font-size: xx-small;"><span style="color: #ff0000;">Msg 8164, Level 16, State 1, Procedure sp_get_composite_job_info, Line 67</span></span></div>
<p><span style="font-size: xx-small;"><span style="color: #ff0000;">An INSERT EXEC statement cannot be nested.</span></span></p>
<p>Fail <img src='http://benchmarkitconsulting.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>So yeah&#8230; that pretty well stopped me in my tracks with that idea.   There are a couple tricks to get around that one but I either couldn&#8217;t use them cause I&#8217;m querying against SQL 2000 still or I wasn&#8217;t comfortable with the &#8220;workaround&#8221; as it felt a bit hack-fu.</p>
<p>So this is what I came up with&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="t-sql" style="font-family:monospace;">SELECT	A.Name as 'JobName', Sub5.MaxRunDate, Sub5.MaxRunTime, Sub5.MostRecentJobStatus
FROM	msdb.dbo.sysJobs A LEFT OUTER JOIN
		(SELECT	A.Job_ID, Sub4.MaxRunDate, Sub4.MaxRunTime,
				CASE	A.run_status
						WHEN 0 THEN 'Failed'
						WHEN 1 THEN 'Successful'
						WHEN 2 THEN 'Retry'
						WHEN 3 THEN 'Cancelled'
						WHEN 4 THEN 'In Progress'
				END as 'MostRecentJobStatus'
		FROM	msdb.dbo.sysJobHistory A INNER JOIN
				(	SELECT	A.Job_ID, Sub3.MaxRunDate, Sub3.MaxRunTime, Sub3.MaxStepID, MAX(A.Instance_ID) as 'MaxInstanceID'
					FROM msdb.dbo.sysJobHistory A INNER JOIN
						(	SELECT	A.Job_ID, Sub2.MaxRunDate, Sub2.MaxRunTime, MAX(A.Step_ID) as 'MaxStepID'
							FROM msdb.dbo.sysJobHistory A INNER JOIN
								(	SELECT	A.Job_ID, Sub1.MaxRunDate, MAX(A.run_time) as 'MaxRunTime'
									FROM	msdb.dbo.sysJobHistory A INNER JOIN
									(	SELECT	A.Job_ID, MAX(A.run_date) as 'MaxRunDate'
										FROM	msdb.dbo.sysJobHistory A
										GROUP BY A.Job_ID) Sub1 ON
											A.Job_ID = Sub1.Job_ID AND
											A.run_date = Sub1.MaxRunDate
									GROUP BY A.Job_ID, Sub1.MaxRunDate) Sub2 ON
										A.Job_ID = Sub2.Job_ID AND
										A.run_date = Sub2.MaxRunDate AND
										A.run_time = Sub2.MaxRunTime
							GROUP BY A.Job_ID, Sub2.MaxRunDate, Sub2.MaxRunTime) Sub3 ON
									A.Job_ID = Sub3.Job_ID AND
									A.run_date = Sub3.MaxRunDate AND
									A.run_time = Sub3.MaxRunTime AND
									A.Step_ID = Sub3.MaxStepID
						GROUP BY A.Job_ID, Sub3.MaxRunDate, Sub3.MaxRunTime, Sub3.MaxStepID) Sub4 ON
							A.Job_ID = Sub4.Job_ID AND
							A.run_date = Sub4.MaxRunDate AND
							A.run_time = Sub4.MaxRunTime AND
							A.Step_ID = Sub4.MaxStepID AND
							A.Instance_ID = Sub4.MaxInstanceID) Sub5 ON
		A.Job_ID = Sub5.Job_ID
WHERE	A.[Enabled] = 1
ORDER BY A.Name</pre></div></div>

<p>Now I&#8217;m the first to admit that this looks&#8230; well horrible&#8230;but it seems to work and get me the information I want.</p>
<p>Add in a simple <span style="color: #0000ff;">Sub5</span><span style="color: #0000ff;">.MostRecentJobStatus = &#8216;Failed&#8217;</span> and now you&#8217;re querying your whole environment for any job that has failed.</p>
<p>I do have notifications setup to email me whenever a job fails, but being a bit on the anal retentive side I like to have a script like this ready to go just in case I ever want a quick ad-hoc view of things</p>
<p>Like I said at the start of this post&#8230; I&#8217;m hoping that someone has something much easier to look at and use&#8230; so if you got it&#8230; flaunt it and comment below!!</p>
<p>Enjoy!!</p>
<p><a href="http://benchmarkitconsulting.com" target="_blank"><img class="alignnone size-full wp-image-402" title="benchmark_sm" src="http://benchmarkitconsulting.com/wp-content/uploads/2009/02/benchmark_sm.jpg" alt="" width="157" height="74" /></a><a href="http://sqlserverpedia.com/wiki/Editors#Colin_Stasiuk" target="_blank"><img src="http://sqlserverpedia.com/badges/SQLServerPedia_Badge_Blogger.jpg" alt="" width="120" height="60" /> </a></p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Most+Recent+Job+Status+of+All+Enabled+Jobs+http://tinyurl.com/yz9huyx" title="Post to Twitter"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-twitter-big4.png" alt="Post to Twitter" /></a> <a class="tt" href="http://delicious.com/post?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/11/17/most-recent-job-status-of-all-enabled-jobs/&amp;title=Most+Recent+Job+Status+of+All+Enabled+Jobs" title="Post to Delicious"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-delicious-big4.png" alt="Post to Delicious" /></a> <a class="tt" href="http://digg.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/11/17/most-recent-job-status-of-all-enabled-jobs/&amp;title=Most+Recent+Job+Status+of+All+Enabled+Jobs" title="Post to Digg"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-digg-big4.png" alt="Post to Digg" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/11/17/most-recent-job-status-of-all-enabled-jobs/&amp;title=Most+Recent+Job+Status+of+All+Enabled+Jobs" title="Post to StumbleUpon"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-su-big4.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://benchmarkitconsulting.com/colin-stasiuk/2009/11/17/most-recent-job-status-of-all-enabled-jobs/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

