<?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; Debugging</title>
	<atom:link href="http://benchmarkitconsulting.com/tag/debugging/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>TSQL Debugging In SQL Server 2008</title>
		<link>http://benchmarkitconsulting.com/colin-stasiuk/2009/03/26/tsql-debugging-in-sql-server-2008/</link>
		<comments>http://benchmarkitconsulting.com/colin-stasiuk/2009/03/26/tsql-debugging-in-sql-server-2008/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 15:21:30 +0000</pubDate>
		<dc:creator>Colin Stasiuk</dc:creator>
				<category><![CDATA[Benchmark IT Consulting]]></category>
		<category><![CDATA[Colin Stasiuk]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Debug]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL 2008]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://benchmarkitconsulting.com/?p=589</guid>
		<description><![CDATA[Everyone else in the world can set breakpoints and navigate through their code line by line and see all the important stuff (errors, variable values, output, etc) but anyone writing some TSQL has to usually put in a bunch of SELECT statements,  PRINT statements (or use the business intellegence development studio) to get any clue as [...]]]></description>
			<content:encoded><![CDATA[<p>Everyone else in the world can set breakpoints and navigate through their code line by line and see all the important stuff (errors, variable values, output, etc) but anyone writing some TSQL has to usually put in a bunch of SELECT statements,  PRINT statements (or use the business intellegence development studio) to get any clue as to what was going on.</p>
<p>Enter the (return of the) DEBUG button (or ALT+F5 if you&#8217;re like me and hate buttons&#8230; especially buttons that look like the execute button from Query Analyzer&#8230; I mean seriously people&#8230; ok I get the fact that yes the execute button <img class="aligncenter size-full wp-image-595" title="execute" src="http://benchmarkitconsulting.com/wp-content/uploads/2009/03/execute.jpg" alt="execute" width="72" height="27" />  has been there since Management Studio 2005 but a green &#8220;play&#8221; button  <img class="aligncenter size-full wp-image-596" title="play" src="http://benchmarkitconsulting.com/wp-content/uploads/2009/03/play.jpg" alt="play" width="36" height="22" />  always means GO GO GO!!! to me&#8230; but I digress&#8230;)</p>
<p>Weird how a post about loving something turned into a rant&#8230; hmmm perhaps I need to lay off the coffee LOL</p>
<p>You need to make sure that you&#8217;re a member of the sysadmin server role&#8230; but you&#8217;re debugging in DEV so I&#8217;m sure that wont be a problem&#8230; cause no one ever debugs stuff directly in production&#8230; I mean that would be silly <img src='http://benchmarkitconsulting.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  hehehe</p>
<p>So let&#8217;s work with an example:</p>
<div><code style="font-size: 12px;"><span style="color:blue">DECLARE </span><span style="color:#434343">@RowID </span><span style="color:blue">INT = </span><span style="color:black">0</span></code></div>
<div><code style="font-size: 12px;"><span style="color:black"><span style="color:blue">WHILE </span><span style="color:#434343">@RowID </span><span style="color:gray">&lt; </span><span style="color:black">10<br />
</span><span style="color:blue">BEGIN<br />
   SELECT </span><span style="color:#434343">@@SERVERNAME<br />
</span><span style="color:blue">END<br />
</span></span></code></div>
<p><code style="font-size: 12px;"><span style="color:black"> </p>
<p></span></code></p>
<p>OK I think it&#8217;s pretty obvious what&#8217;s wrong with this query&#8230; but let&#8217;s say for the sake of just showing off the hotness of debugging in SQL Server 2008 you didn&#8217;t know why you didn&#8217;t get 10 rows returned and ended up with an endless list of your server name LOL</p>
<p>so put your cursor on:</p>
<div><code style="font-size: 12px;"><span style="color:black">   </span><span style="color:blue">SELECT </span><span style="color:#434343">@@SERVERNAME</span></code></div>
<div><code style="font-size: 12px;"><span style="color:#434343"> </span></code></div>
<p><code style="font-size: 12px;"><span style="color:#434343">and hit F9. You should now see the familar breakpoint red circle. Now if you hit ALT-F5 and look at your "Locals" window at the bottom you should see @RowID with no value. hit ALT-F5 again to continue to debug and your code should stop on:</p>
<p></span></code></p>
<p style="text-align: left;">
<div style="text-align: left;"><code style="font-size: 12px;"><span style="color:black">   </span><span style="color:blue">SELECT </span><span style="color:#434343">@@SERVERNAME</span></code></div>
<p style="text-align: left;">
<div style="text-align: left;"><code style="font-size: 12px;"></code></div>
<p><code style="font-size: 12px;"><span style="color:#434343"></p>
<p style="text-align: center;"><a href="http://benchmarkitconsulting.com/wp-content/uploads/2009/03/tsqldebug_ex1.jpg"><img class="aligncenter size-full wp-image-590" title="tsqldebug_ex1" src="http://benchmarkitconsulting.com/wp-content/uploads/2009/03/tsqldebug_ex1.jpg" alt="tsqldebug_ex1" width="632" height="504" /></a></p>
<p> </p>
<p></span></code></p>
<p style="text-align: left;"> </p>
<p style="text-align: left;">and now you should see @RowID with a value of 0&#8230; great&#8230; exactly as expected now let&#8217;s go through the next loop&#8230; hit ALT-F5 again. We now see in our Results window our server name but in the locals window @RowID is still 0. If we hit ALT-F5 a few more times we&#8217;ll see our server name continually being returned but @RowID never changing.</p>
<p style="text-align: left;">So now if we exit out of &#8220;debug&#8221; mode. (SHIFT-F5) and add the following line of code after the SELECT @@SERVERNAME:</p>
<p style="text-align: left;"><code style="font-size: 12px;"><span style="color:black">   </span><span style="color:blue">SET </span><span style="color:#434343">@RowID </span><span style="color:blue">= </span><span style="color:#434343">@RowID </span><span style="color:gray">+ </span><span style="color:black">1</span></code></p>
<p style="text-align: left;">Code should now look like this:</p>
<p style="text-align: left;"> </p>
<div style="text-align: left;"><code style="font-size: 12px;"><span style="color:blue">DECLARE </span><span style="color:#434343">@RowID </span><span style="color:blue">INT = </span><span style="color:black">0</span></code></div>
<p><code style="font-size: 12px;"><span style="color:black"><span style="color:blue">WHILE </span><span style="color:#434343">@RowID </span><span style="color:gray">&lt; </span><span style="color:black">10<br />
</span><span style="color:blue">BEGIN<br />
   SELECT </span><span style="color:#434343">@@SERVERNAME<br />
   </span><span style="color:blue">SET </span><span style="color:#434343">@RowID </span><span style="color:blue">= </span><span style="color:#434343">@RowID </span><span style="color:gray">+ </span><span style="color:black">1<br />
</span><span style="color:blue">END<br />
</span></p>
<p></span></code></p>
<p style="text-align: left;">and if we lather, rinse, and repeat the debugging we should now see in our &#8220;Locals&#8221; window that @RowID increases by 1 every time we loop through and when it gets to 10 we exit out of our loop and we&#8217;re done.</p>
<p style="text-align: left;">So there you go&#8230; a nice easy example of debugging in SQL Server 2008&#8230; now this only works in SQL Server 2008 currently, if you try and do this in SQL Server 2005 you&#8217;ll get a popup window indicating that the Transact-SQL debugger does not support SQL Server 2005 or earlier versions of SQL Server.</p>
<p style="text-align: left;">Enjoy!!</p>
<p style="text-align: left;"><a href="http://benchmarkitconsulting.com"><img class="alignnone size-full wp-image-402" title="benchmark_sm" src="http://benchmarkitconsulting.com/wp-content/uploads/2009/02/benchmark_sm.jpg" alt="" /></a><a href="http://sqlserverpedia.com/wiki/Editors#Colin_Stasiuk" target="_blank"><img src="http://sqlserverpedia.com/badges/SQLServerPedia_Badge_Blogger.jpg" alt="" /> </a></p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=TSQL+Debugging+In+SQL+Server+2008+http://tinyurl.com/dgb6bt" 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/03/26/tsql-debugging-in-sql-server-2008/&amp;title=TSQL+Debugging+In+SQL+Server+2008" 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/03/26/tsql-debugging-in-sql-server-2008/&amp;title=TSQL+Debugging+In+SQL+Server+2008" 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/03/26/tsql-debugging-in-sql-server-2008/&amp;title=TSQL+Debugging+In+SQL+Server+2008" 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/03/26/tsql-debugging-in-sql-server-2008/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

