<?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>tim laqua dot com &#187; sql server 2005</title>
	<atom:link href="http://timlaqua.com/tag/sql-server-2005/feed/" rel="self" type="application/rss+xml" />
	<link>http://timlaqua.com</link>
	<description>Thoughts and Code from Tim Laqua</description>
	<lastBuildDate>Sun, 09 May 2010 15:25:58 +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>A Cleaner Way to Detect the State of a SQL Server Agent Job In SQL Server 2005</title>
		<link>http://timlaqua.com/2009/05/a-cleaner-way-to-detect-the-state-of-a-sql-server-agent-job-in-sql-server-2005/</link>
		<comments>http://timlaqua.com/2009/05/a-cleaner-way-to-detect-the-state-of-a-sql-server-agent-job-in-sql-server-2005/#comments</comments>
		<pubDate>Sat, 23 May 2009 13:18:38 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[agent job]]></category>
		<category><![CDATA[sql server 2005]]></category>
		<category><![CDATA[t-sql]]></category>
		<category><![CDATA[table valued function]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=212</guid>
		<description><![CDATA[I have always felt that the traditional approach to getting Agent Job status by creating a table and populating it with output from xp_sqlagent_enum_jobs was incredibly crude. So I poked around a bit and came up with a solution using a Table Valued Function that queries sysjobactivity to determine the status of a given job. [...]]]></description>
			<content:encoded><![CDATA[<p>I have always felt that the traditional approach to getting Agent Job status by creating a table and populating it with output from xp_sqlagent_enum_jobs was incredibly crude.  So I poked around a bit and came up with a solution using a Table Valued Function that queries sysjobactivity to determine the status of a given job.  The reason I use a Table Valued Function is because we have a few tables that contain a list of jobs that need to be started at certain points in a given process (and they may or may not already be running) - with the TVF, you can simply CROSS APPLY the function and bob's your uncle.<span id="more-212"></span></p>
<p><em>Example</em><br />
Say you needed to run a chunk of code only if a set of jobs in a given table aren't running:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">	<span style="color: #993333; font-weight: bold;">IF</span>  <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">1</span>
		<span style="color: #993333; font-weight: bold;">FROM</span> AppJobDependency a <span style="color: #993333; font-weight: bold;">WITH</span><span style="color: #66cc66;">&#40;</span>NOLOCK<span style="color: #66cc66;">&#41;</span>
		<span style="color: #993333; font-weight: bold;">CROSS</span> APPLY <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>Utility_GetAgentJobStatus_F01<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>JobName<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> c
		<span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #66cc66;">&#91;</span>JobStatus<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
	BEGIN
		<span style="color: #808080; font-style: italic;">-- Code goes here</span>
	END</pre></div></div>

<p><em>Table Valued Function</em></p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/*************************************************************************************
***
*** Procedure:  		[Utility_GetAgentJobStatus_F01]
*** Purpose:		    Get the current status of given agent job
***				
***			
*** Author:		      tl
*** Date Created:	  2009-03-03
*** 
*** Revision History
*** Date		Author			Description
*** 2009-03-03	tl				Created
*************************************************************************************/</span>
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>Utility_GetAgentJobStatus_F01<span style="color: #66cc66;">&#93;</span> 
<span style="color: #66cc66;">&#40;</span>
	@JobName varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>
RETURNS @JobStatus <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>JobStatus<span style="color: #66cc66;">&#93;</span> BIT<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">AS</span>
BEGIN
&nbsp;
	<span style="color: #993333; font-weight: bold;">WITH</span> JobActivity <span style="color: #993333; font-weight: bold;">AS</span>
	<span style="color: #66cc66;">&#40;</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span> 
		 a<span style="color: #66cc66;">.</span>start_execution_date
		<span style="color: #66cc66;">,</span>a<span style="color: #66cc66;">.</span>stop_execution_date
		<span style="color: #66cc66;">,</span>ROW_NUMBER<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> OVER <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> session_id <span style="color: #993333; font-weight: bold;">DESC</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>RowNumber<span style="color: #66cc66;">&#93;</span>
	<span style="color: #993333; font-weight: bold;">FROM</span>
		msdb<span style="color: #66cc66;">.</span>dbo<span style="color: #66cc66;">.</span>sysjobactivity a <span style="color: #993333; font-weight: bold;">WITH</span><span style="color: #66cc66;">&#40;</span>NOLOCK<span style="color: #66cc66;">&#41;</span>
		<span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> msdb<span style="color: #66cc66;">.</span>dbo<span style="color: #66cc66;">.</span>sysjobs b <span style="color: #993333; font-weight: bold;">WITH</span><span style="color: #66cc66;">&#40;</span>NOLOCK<span style="color: #66cc66;">&#41;</span>
		   <span style="color: #993333; font-weight: bold;">ON</span> 
			a<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>job_id<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> b<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>job_id<span style="color: #66cc66;">&#93;</span>
			<span style="color: #993333; font-weight: bold;">AND</span> b<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>name<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> @JobName
	<span style="color: #66cc66;">&#41;</span>
	<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> @JobStatus
	<span style="color: #993333; font-weight: bold;">SELECT</span> COUNT<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #993333; font-weight: bold;">FROM</span> JobActivity
	<span style="color: #993333; font-weight: bold;">WHERE</span>
		start_execution_date <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>
		<span style="color: #993333; font-weight: bold;">AND</span> stop_execution_date <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NULL</span>
		<span style="color: #993333; font-weight: bold;">AND</span> RowNumber <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>
&nbsp;
	<span style="color: #993333; font-weight: bold;">RETURN</span>
END</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2009/05/a-cleaner-way-to-detect-the-state-of-a-sql-server-agent-job-in-sql-server-2005/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Server Analysis Services Command (XMLA) Agent Job Step Reports Success On Command Failure</title>
		<link>http://timlaqua.com/2009/03/sql-server-analysis-services-command-xmla-agent-job-step-reports-success-on-command-failure/</link>
		<comments>http://timlaqua.com/2009/03/sql-server-analysis-services-command-xmla-agent-job-step-reports-success-on-command-failure/#comments</comments>
		<pubDate>Sat, 07 Mar 2009 20:35:11 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[agent job]]></category>
		<category><![CDATA[analysis services]]></category>
		<category><![CDATA[business intelligence]]></category>
		<category><![CDATA[cube processing]]></category>
		<category><![CDATA[job failure]]></category>
		<category><![CDATA[sql server 2005]]></category>
		<category><![CDATA[t-sql]]></category>
		<category><![CDATA[workaround]]></category>
		<category><![CDATA[xmla]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=65</guid>
		<description><![CDATA[I ran in to this the other day, did some googling, and really did not like what I saw for workarounds. In SQL Server 2005, when an XMLA job step fails (returns an Exception node in the XML response), the job step still reports success (because it's defining success as "did I get a response") [...]]]></description>
			<content:encoded><![CDATA[<p>I ran in to this the other day, did some googling, and really did not like what I saw for workarounds.  In SQL Server 2005, when an XMLA job step fails (returns an Exception node in the XML response), the job step still reports success (because it's defining success as "did I get a response") - this has been fixed in SQL Server 2008.  Common workarounds are using ascmd.exe or SSIS to handle the XMLA commands (ish - both of those solutions add a lot of complexity for a simple problem).  So, I came up with a workaround that checks the text of the previous job step for the substring "&lt;Exception ".  It's been working thus far, with no issues.</p>
<p>After each XMLA command step, insert a T-SQL step to verify that the XMLA command step succeeded:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">DECLARE</span> @JobName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">64</span><span style="color: #808080;">&#41;</span>
&nbsp;
<span style="color: #0000FF;">SET</span> @JobName <span style="color: #808080;">=</span> ‘Name <span style="color: #0000FF;">OF</span> Job This Step Belongs <span style="color: #0000FF;">TO</span>’
&nbsp;
<span style="color: #0000FF;">DECLARE</span> @Message <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1024</span><span style="color: #808080;">&#41;</span>
&nbsp;
<span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">1</span> @Message <span style="color: #808080;">=</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>message<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1024</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">FROM</span> msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">sysjobhistory</span> a
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">sysjobs</span> b
<span style="color: #0000FF;">ON</span> a.<span style="color: #202020;">job_id</span> <span style="color: #808080;">=</span> b.<span style="color: #202020;">job_id</span> <span style="color: #808080;">AND</span> b.<span style="color: #808080;">&#91;</span>NAME<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> @JobName
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> run_date <span style="color: #0000FF;">DESC</span>, run_time <span style="color: #0000FF;">DESC</span>, step_id <span style="color: #0000FF;">DESC</span>
&nbsp;
<span style="color: #0000FF;">IF</span> @Message <span style="color: #808080;">LIKE</span> ‘<span style="color: #808080;">%&lt;</span>Exception <span style="color: #808080;">%</span>’
<span style="color: #0000FF;">RAISERROR</span> <span style="color: #808080;">&#40;</span>@Message, <span style="color: #000;">17</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span></pre></div></div>

<p><strong><em>UPDATE (2009-04-03): Added <code>, step_id DESC</code> to <code>ORDER BY</code> clause - when the XMLA job fails instantly (say you tried to process a nonexistant partition), run_time doesn't have enough granularity to sort properly.</em></strong></p>
<p>Once your done, your job steps will look something like this:</p>
<p><a href="http://timlaqua.com/wp-content/uploads/2009/03/xmla-command-verification-steps.png"><img src="http://timlaqua.com/wp-content/uploads/2009/03/xmla-command-verification-steps.png" alt="xmla-command-verification-steps" title="xmla-command-verification-steps" width="450" class="alignnone size-full wp-image-67" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2009/03/sql-server-analysis-services-command-xmla-agent-job-step-reports-success-on-command-failure/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
