<?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>Never Say Never &#187; T-SQL Tuesday</title>
	<atom:link href="http://phelabaum.com/archive/tag/t-sql-tuesday/feed/" rel="self" type="application/rss+xml" />
	<link>http://phelabaum.com</link>
	<description>MS SQL Server Development</description>
	<lastBuildDate>Thu, 29 Sep 2011 02:20:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>T-SQL Tuesday #7: T-SQL Enhancements in SQL 2008.</title>
		<link>http://phelabaum.com/archive/2010/06/t-sql-tuesday-7-t-sql-enhancements-in-sql-2008/</link>
		<comments>http://phelabaum.com/archive/2010/06/t-sql-tuesday-7-t-sql-enhancements-in-sql-2008/#comments</comments>
		<pubDate>Tue, 08 Jun 2010 17:08:42 +0000</pubDate>
		<dc:creator>Seth Phelabaum</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[T-SQL Tuesday]]></category>

		<guid isPermaLink="false">http://phelabaum.com/archive/2010/06/t-sql-tuesday-7-t-sql-enhancements-in-sql-2008/</guid>
		<description><![CDATA[&#160; In this edition of T-SQL Tuesday, Jorge Segarra (Blog &#124; Twitter) asks us what our favorite new feature of SQL 2008 or 2008 R2 is.&#160; I&#8217;ve decided to focus on the T-SQL and query writing enhancements of 2008.&#160; Before I do so though, let me preface this by noting that in no way do [...]]]></description>
			<content:encoded><![CDATA[<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal">&#160;</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><a href="http://sqlchicken.com/2010/06/t-sql-tuesday-007-summertime-in-the-sql/"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="image" border="0" alt="image" align="left" src="http://phelabaum.com/wp-content/uploads/2010/06/image.png" width="131" height="131" /></a> </p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-size: 10pt; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes">In this edition of T-SQL Tuesday, Jorge Segarra (<a href="http://sqlchicken.com/" target="_blank">Blog</a> | <a href="http://twitter.com/sqlchicken" target="_blank">Twitter</a>) asks us what our favorite new feature of SQL 2008 or 2008 R2 is.<span style="mso-spacerun: yes">&#160; </span>I&#8217;ve decided to focus on the T-SQL and query writing enhancements of 2008.<span style="mso-spacerun: yes">&#160; </span>Before I do so though, let me preface this by noting that in no way do I believe these changes are the biggest improvements or best new things in SQL 2008, but things like Data compression are bound to be covered by several others.<span style="mso-spacerun: yes">&#160; </span>Also, while writing this post I noticed that most of this has already been covered better than I could hope to by Itzik Ben-Gan in his white paper (<a href="http://technet.microsoft.com/en-us/library/cc721270(SQL.100).aspx">Link</a>).<span style="mso-spacerun: yes">&#160; </span>Please refer to that for more information on the new features of 2008. </span></p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-size: 10pt; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes"></span>
<p>&#160;</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-size: 10pt; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes"></span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><b style="mso-bidi-font-weight: normal"><span style="mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes"><u>Your New home for One stop Variable Declaration </u>
</p>
<p>     </span></b></p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-size: 10pt; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes">In the past, you always had to declare variables on one line and then assign them on the next, as so: </span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"></span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes">DECLARE</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> @MyInt <span style="color: blue">int </span>
</p>
<p>   </span></p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes">SET</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> @MyInt <span style="color: gray">=</span> <span style="color: #ff8040">44</span></span><span style="font-family: &quot;Courier New&quot;; color: #ff8040; font-size: 10pt; mso-no-proof: yes"> </span>
<p>&#160;</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-size: 10pt; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes">Now, you can do this in one statement.<span style="mso-spacerun: yes">&#160; </span></span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: #ff8040; font-size: 10pt; mso-no-proof: yes"></span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes">DECLARE</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> @MyInt <span style="color: blue">int</span> <span style="color: gray">=</span> <span style="color: #ff8040">44</span></span><span style="font-family: &quot;Courier New&quot;; color: #ff8040; font-size: 10pt; mso-no-proof: yes"> </span>
<p>&#160;</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-size: 10pt; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes">That might seem small, but it&#8217;s significant when you&#8217;re dealing with large numbers of variables.</span></p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-size: 10pt; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes"></span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: #ff8040; font-size: 10pt; mso-no-proof: yes"></span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><b style="mso-bidi-font-weight: normal"><span style="mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes"><u>SQL++ &#8230; almost </u>
</p>
<p>     </span></b></p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-size: 10pt; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes">While you still can&#8217;t do something like:</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> </span>
<p>&#160;</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes">SET</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> @MyInt<span style="color: gray">++ </span>
</p>
<p>   </span></p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes"></span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-size: 10pt; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes">You can do the slightly longer version of:</span><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes"> </span>
<p>&#160;</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes">SET</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> @MyInt<span style="color: gray">+=</span>@Myint </span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: green; font-size: 10pt; mso-no-proof: yes">&#8211; OR </span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes">SET</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> @MyInt<span style="color: gray">+=</span><span style="color: #ff8040">5 </span>
</p>
<p>   </span></p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: #ff8040; font-size: 10pt; mso-no-proof: yes"></span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-size: 10pt; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes">Instead of :</span><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes"> </span>
<p>&#160;</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes">SET</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> @MyInt <span style="color: gray">=</span> @MyInt <span style="color: gray">+</span> @MyInt </span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: green; font-size: 10pt; mso-no-proof: yes">&#8211; OR </span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes">SET</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> @MyInt <span style="color: gray">=</span> @MyInt <span style="color: gray">+</span> <span style="color: #ff8040">5 </span>
</p>
<p>   </span></p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: #ff8040; font-size: 10pt; mso-no-proof: yes"></span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><u><span style="font-size: 10pt; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes">This applies to: </span>
</p>
<p>   </u></p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-size: 10pt; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes">+= (plus equals) </span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-size: 10pt; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes">-=<span style="mso-spacerun: yes">&#160; </span>(minus equals) </span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-size: 10pt; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes">*=<span style="mso-spacerun: yes">&#160; </span>(multiplication equals) </span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-size: 10pt; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes">/=<span style="mso-spacerun: yes">&#160; </span>(division equals) </span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-size: 10pt; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes">%=<span style="mso-spacerun: yes">&#160; </span>(modulo equals)</span><span style="font-size: 10pt; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes"> </span>
<p>&#160;</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-size: 10pt; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes">This one isn&#8217;t too huge a deal in my opinion, but it&#8217;s a nice shortcut for those used to using it in other programming languages.</span></p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-size: 10pt; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes"></span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-size: 10pt; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes"></span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><b style="mso-bidi-font-weight: normal"><span style="mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes"><u>Values of all rows, Union of None </u>
</p>
<p>     </span></b></p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-size: 10pt; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes">In 2008, you can create multiple lines of data using the VALUES clause.<span style="mso-spacerun: yes">&#160; </span>This one is really handy when I&#8217;m doing code examples in blog posts / on forums / in presentations etc.<span style="mso-spacerun: yes">&#160; </span>It serves as an excellent replacement for the UNION ALL or repeated insert values pairs you used to have to use when supplying sample data.<span style="mso-spacerun: yes">&#160; </span></span><span style="font-size: 10pt; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes"></span>
<p>&#160;</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-size: 10pt; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes">Say you have a simple table: </span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: #ff8040; font-size: 10pt; mso-no-proof: yes"></span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes">CREATE</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> <span style="color: blue">TABLE</span> #Cake<span style="color: gray">( </span>
</p>
<p>   </span></p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes">SomeInt<span style="mso-tab-count: 2">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: blue">int</span><span style="color: gray">, </span>
</p>
<p>   </span></p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes">SomeChar<span style="mso-tab-count: 1">&#160;&#160;&#160; </span><span style="color: blue">char</span><span style="color: gray">(</span><span style="color: #ff8040">5</span><span style="color: gray">) </span>
</p>
<p>   </span></p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes">)</span><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes"> </span>
<p>&#160;</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-size: 10pt; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes">You want to provide some sample data for that table.<span style="mso-spacerun: yes">&#160; </span>The most common ways prior to now were either: </span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes"></span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes">INSERT</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> <span style="color: blue">INTO</span> #Cake<span style="color: gray">(</span>SomeInt<span style="color: gray">,</span>SomeChar<span style="color: gray">) </span>
</p>
<p>   </span></p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes">VALUES</span><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes">(</span><span style="font-family: &quot;Courier New&quot;; color: #ff8040; font-size: 10pt; mso-no-proof: yes">1</span><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes">,</span><span style="font-family: &quot;Courier New&quot;; color: red; font-size: 10pt; mso-no-proof: yes">&#8216;AAA&#8217;</span><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes">) </span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes"></span>
<p>&#160;</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes">INSERT</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> <span style="color: blue">INTO</span> #Cake<span style="color: gray">(</span>SomeInt<span style="color: gray">,</span>SomeChar<span style="color: gray">) </span>
</p>
<p>   </span></p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes">VALUES</span><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes">(</span><span style="font-family: &quot;Courier New&quot;; color: #ff8040; font-size: 10pt; mso-no-proof: yes">2</span><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes">,</span><span style="font-family: &quot;Courier New&quot;; color: red; font-size: 10pt; mso-no-proof: yes">&#8216;BBB&#8217;</span><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes">) </span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes"></span>
<p>&#160;</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes">INSERT</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> <span style="color: blue">INTO</span> #Cake<span style="color: gray">(</span>SomeInt<span style="color: gray">,</span>SomeChar<span style="color: gray">) </span>
</p>
<p>   </span></p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes">VALUES</span><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes">(</span><span style="font-family: &quot;Courier New&quot;; color: #ff8040; font-size: 10pt; mso-no-proof: yes">3</span><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes">,</span><span style="font-family: &quot;Courier New&quot;; color: red; font-size: 10pt; mso-no-proof: yes">&#8216;CCC&#8217;</span><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes">)</span><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes"> </span>
<p>&#160;</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: green; font-size: 10pt; mso-no-proof: yes">&#8211; OR </span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: green; font-size: 10pt; mso-no-proof: yes"></span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes">INSERT</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> <span style="color: blue">INTO</span> #Cake<span style="color: gray">(</span>SomeInt<span style="color: gray">,</span>SomeChar<span style="color: gray">) </span>
</p>
<p>   </span></p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes">SELECT</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> <span style="color: #ff8040">1</span><span style="color: gray">,</span><span style="color: red">&#8216;AAA&#8217;</span> <span style="color: blue">UNION</span> <span style="color: gray">ALL </span>
</p>
<p>   </span></p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes">SELECT</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> <span style="color: #ff8040">2</span><span style="color: gray">,</span><span style="color: red">&#8216;BBB&#8217;</span> <span style="color: blue">UNION</span> <span style="color: gray">ALL </span>
</p>
<p>   </span></p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes">SELECT</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> <span style="color: #ff8040">3</span><span style="color: gray">,</span><span style="color: red">&#8216;CCC&#8217;</span></span><span style="font-family: &quot;Courier New&quot;; color: red; font-size: 10pt; mso-no-proof: yes"> </span>
<p>&#160;</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-size: 10pt; mso-bidi-font-family: calibri; mso-bidi-theme-font: minor-latin; mso-no-proof: yes">Now, you can use the much cleaner: </span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: red; font-size: 10pt; mso-no-proof: yes"></span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes">INSERT</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> <span style="color: blue">INTO</span> #Cake<span style="color: gray">(</span>SomeInt<span style="color: gray">,</span>SomeChar<span style="color: gray">) </span>
</p>
<p>   </span></p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes">VALUES</span><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes">(</span><span style="font-family: &quot;Courier New&quot;; color: #ff8040; font-size: 10pt; mso-no-proof: yes">1</span><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes">,</span><span style="font-family: &quot;Courier New&quot;; color: red; font-size: 10pt; mso-no-proof: yes">&#8216;AAA&#8217;</span><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes">), </span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes"><span style="mso-spacerun: yes">&#160;&#160;&#160;&#160;&#160; </span></span><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes">(</span><span style="font-family: &quot;Courier New&quot;; color: #ff8040; font-size: 10pt; mso-no-proof: yes">2</span><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes">,</span><span style="font-family: &quot;Courier New&quot;; color: red; font-size: 10pt; mso-no-proof: yes">&#8216;BBB&#8217;</span><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes">), </span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes"><span style="mso-tab-count: 1">&#160;&#160;&#160;&#160;&#160; </span></span><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes">(</span><span style="font-family: &quot;Courier New&quot;; color: #ff8040; font-size: 10pt; mso-no-proof: yes">3</span><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes">,</span><span style="font-family: &quot;Courier New&quot;; color: red; font-size: 10pt; mso-no-proof: yes">&#8216;CCC&#8217;</span><span style="font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes">)</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"><span style="mso-tab-count: 1">&#160;&#160;&#160;&#160;&#160; </span><span style="mso-spacerun: yes">&#160; </span></span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: green; font-size: 10pt; mso-no-proof: yes">&#8211; Or, on 1 line: </span>
</p>
</p>
<p style="line-height: normal; margin-bottom: 0pt; mso-layout-grid-align: none" class="MsoNormal"><span style="font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes">INSERT</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> <span style="color: blue">INTO</span> #Cake<span style="color: gray">(</span>SomeInt<span style="color: gray">,</span>SomeChar<span style="color: gray">) </span>
</p>
<p>   </span></p>
</p>
<p class="MsoNormal"><span style="line-height: 115%; font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes">VALUES</span><span style="line-height: 115%; font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes">(</span><span style="line-height: 115%; font-family: &quot;Courier New&quot;; color: #ff8040; font-size: 10pt; mso-no-proof: yes">1</span><span style="line-height: 115%; font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes">,</span><span style="line-height: 115%; font-family: &quot;Courier New&quot;; color: red; font-size: 10pt; mso-no-proof: yes">&#8216;AAA&#8217;</span><span style="line-height: 115%; font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes">),(</span><span style="line-height: 115%; font-family: &quot;Courier New&quot;; color: #ff8040; font-size: 10pt; mso-no-proof: yes">2</span><span style="line-height: 115%; font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes">,</span><span style="line-height: 115%; font-family: &quot;Courier New&quot;; color: red; font-size: 10pt; mso-no-proof: yes">&#8216;BBB&#8217;</span><span style="line-height: 115%; font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes">),(</span><span style="line-height: 115%; font-family: &quot;Courier New&quot;; color: #ff8040; font-size: 10pt; mso-no-proof: yes">3</span><span style="line-height: 115%; font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes">,</span><span style="line-height: 115%; font-family: &quot;Courier New&quot;; color: red; font-size: 10pt; mso-no-proof: yes">&#8216;CCC&#8217;</span><span style="line-height: 115%; font-family: &quot;Courier New&quot;; color: gray; font-size: 10pt; mso-no-proof: yes">) </span>
</p>
</p>
<p style="margin-bottom: 0pt" class="MsoNormal"><b style="mso-bidi-font-weight: normal"><u>Lack of Intellisense</u></b><i style="mso-bidi-font-style: normal"> </i>
</p>
</p>
<p style="margin-bottom: 0pt" class="MsoNormal"><span style="line-height: 115%; font-size: 10pt">No conversation on the topic of coding enhancements would be complete without mentioning Intellisense in some way.<span style="mso-spacerun: yes">&#160; </span>So&#8230; it&#8217;s there.<span style="mso-spacerun: yes">&#160; </span>It works (kinda).<span style="mso-spacerun: yes">&#160; </span>It could use a whole lot more fine tuning and configuration options than are there right now, but if you don&#8217;t have a copy of <a href="http://www.red-gate.com/products/SQL_Prompt/index.htm" target="_blank">SQL Prompt</a>, it&#8217;s better than not having anything (sometimes).<span style="mso-spacerun: yes">&#160; </span>If you *do* have a copy of SQL Prompt and want to use a couple of the cool things SQL intellisense has that SQL Prompt does not, you can use the hybrid approach that I&#8217;ve gone with.<span style="mso-spacerun: yes">&#160; </span>This allows me to get the () highlighting and error underlines from SQL intellisense without overriding my much more configurable (and in my opinion less annoying) suggestions from SQL Prompt.<span style="mso-spacerun: yes">&#160; </span></span></p>
<p style="margin-bottom: 0pt" class="MsoNormal"><span style="line-height: 115%; font-size: 10pt"><span style="mso-spacerun: yes"></span></span></p>
<p class="MsoNormal"><span style="line-height: 115%; font-size: 10pt">If you go to Tools &gt; Options &gt; Text Editor &gt; Transact-SQL and turn intellisense on, but turn auto list members off(General), you can have what is (in my own opinion) the best of both worlds.<span style="mso-spacerun: yes">&#160; </span></span></p>
<p class="MsoNormal"><a href="http://phelabaum.com/wp-content/uploads/2010/06/image1.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://phelabaum.com/wp-content/uploads/2010/06/image_thumb.png" width="380" height="226" /></a> </p>
<p class="MsoNormal"><a href="http://phelabaum.com/wp-content/uploads/2010/06/image2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://phelabaum.com/wp-content/uploads/2010/06/image_thumb1.png" width="385" height="229" /></a> </p>
<p class="MsoNormal"><span style="mso-no-proof: yes; mso-fareast-language: zh-cn"><shape style="width: 365.25pt; height: 212.25pt; visibility: visible; mso-wrap-style: square" id="Picture_x0020_0" alt="Description: Intellisense1.jpg" type="#_x0000_t75" o:spid="_x0000_i1026"><imagedata o:title="Intellisense1" src="file:///C:\Users\Garadin\AppData\Local\Temp\msohtmlclip1\01\clip_image002.jpg"></imagedata></shape></span></p>
</p>
<p class="MsoNormal"><span style="mso-no-proof: yes; mso-fareast-language: zh-cn"><shape style="width: 365.25pt; height: 212.25pt; visibility: visible; mso-wrap-style: square" id="Picture_x0020_1" alt="Description: Intellisense2.jpg" type="#_x0000_t75" o:spid="_x0000_i1025"><imagedata o:title="Intellisense2" src="file:///C:\Users\Garadin\AppData\Local\Temp\msohtmlclip1\01\clip_image003.jpg"></imagedata></shape></span></p>
</p>
<p class="MsoNormal"><span style="line-height: 115%; font-size: 10pt">That about wraps up what I wanted to point out this go round, hopefully you found something new that you didn&#8217;t know about before.<span style="mso-spacerun: yes">&#160; </span>Don&#8217;t forget to check out all the other T-SQL Tuesday posts (Click the image at the top for a link to the others) that will no doubt point out many of the much bigger improvements in SQL Server 2008 and R2. </span></p>
]]></content:encoded>
			<wfw:commentRss>http://phelabaum.com/archive/2010/06/t-sql-tuesday-7-t-sql-enhancements-in-sql-2008/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>T-SQL Tuesday #005: Monitoring Reports with SSRS</title>
		<link>http://phelabaum.com/archive/2010/04/t-sql-tuesday-monitoring-reports-with-ssrs/</link>
		<comments>http://phelabaum.com/archive/2010/04/t-sql-tuesday-monitoring-reports-with-ssrs/#comments</comments>
		<pubDate>Tue, 13 Apr 2010 15:32:22 +0000</pubDate>
		<dc:creator>Seth Phelabaum</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[SSRS]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[T-SQL Tuesday]]></category>

		<guid isPermaLink="false">http://phelabaum.com/?p=187</guid>
		<description><![CDATA[This post is a T-SQL Tuesday Entry, hosted this week by Aaron Nelson on the topic of “Reporting”.&#160; (It got a little long.&#160; Ordinarily I’d have broken this up into a series and fleshed out individual pieces a bit better, but this touches on most of the general points) I like babysitter reports.&#160; What is [...]]]></description>
			<content:encoded><![CDATA[<p>This post is a <a href="http://sqlvariant.com/wordpress/index.php/2010/04/t-sql-tuesday-005-reporting/" target="_blank">T-SQL Tuesday Entry</a>, hosted this week by Aaron Nelson on the topic of “Reporting”.&#160; (It got a little long.&#160; Ordinarily I’d have broken this up into a series and fleshed out individual pieces a bit better, but this touches on most of the general points)</p>
<p>I like babysitter reports.&#160; What is a &quot;babysitter&quot; report?&#160; It&#8217;s a report that you schedule to run on a recurring basis that checks things for you.&#160; I call them babysitter reports because they can monitor things without you having to worry about it.&#160; Every environment has different things that they need to look for.&#160; Maybe a certain value found its way into a certain table and you need to take action because of it.&#160; Maybe a certain query is on a rampage again and you need to kill it.&#160; There are all kinds of things that you know you should keep an eye on that you don&#8217;t always remember to do.&#160; Instead of putting that burden on your memory or calendar, these automated reports do the work for you.</p>
<p>Here I will show you how to create one simple babysitter report.&#160; I intentionally chose one of the more complicated ones (CPU Threshold) to note how far it could be expanded upon, but more basic things would not require this level of customization.&#160; Here are a few examples of things that you could create babysitter reports for:</p>
<ul>
<li>Long Running Queries </li>
<li>Blocking SP&#8217;s </li>
<li>Index fragmentation </li>
<li>Log Size </li>
<li>System Info </li>
<li>Specific entries into tables </li>
</ul>
<p>The sky is the limit.&#160; The same strategies can be used to get information to your users when rare events occur that require immediate action if your system doesn&#8217;t already provide a means to get this information to them in a timely manner.&#160; There are certain reports in my environment that can run for *days* if the wrong parameters are sent to them&#8230; and while ideally these would be fixed in other ways, it&#8217;s good to identify the situations that occur in the interim and take action until that can be accomplished.</p>
<p>Here are a few sample queries for finding queries with abnormally high CPU usage.&#160;&#160; There are two basic parts to these.&#160; The first is the data driven subscription.&#160; You want this to be as streamlined as possible.&#160; This is the piece that will be run repeatedly to see if a problem exists, and because it could be running hundreds of times before its&#8217; criteria is met once, you want it to be as efficient as possible.</p>
<pre class="brush : sql">/*
  =============================================================================================
CREATE DATE:&#160;&#160;&#160;&#160; 04/12/2010
LAST MODIFIED:&#160;&#160;&#160; 04/12/2010
CREATED BY:&#160;&#160;&#160;&#160;&#160;&#160;&#160; SETH PHELABAUM
PURPOSE:&#160;&#160;&#160;&#160;&#160;&#160;&#160; Data Driven Subscription that monitors for queries using high CPU.
ISSUES:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Will Notify you Repeatedly.
Notes:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; This can be expanded upon quite a bit.&#160; For instance, you could also:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Set up a Logging table / set of tables to control how often this notifies you (To stop you from getting multiple emails overnight)&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Set up a list of excluded sp's&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Set up a list of different actions depending on the time of day (You could also change the schedule in reporting services)&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Much more...
=============================================================================================
*/ 

CREATE PROCEDURE DDS_HighCPU
  AS 

SELECT DISTINCT spid, 'youremailaddress@yourdomain.com' Notify
  FROM sys.sysprocesses
WHERE [dbid] &gt; 4 -- May Need to filter out additional Databases here for your setup&#160;&#160;&#160;&#160; and cpu &gt; 10000&#160;&#160;&#160; -- Adjust to whatever you consider worth knowing about.&#160;&#160;&#160;&#160; and cmd &lt;&gt; 'AWAITING COMMAND'&#160; -- Don't want to be notified about these.&#160;&#160;&#160;&#160; and spid IN (SELECT spid&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; FROM sys.dm_exec_connections DMV_EC&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; WHERE DMV_EC.last_read &gt; DATEADD(mm,-2,GETDATE())&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; OR DMV_EC.last_write &gt; DATEADD(mm,-2,GETDATE())) -- Another filter to hopefully stop some excess emails</pre>
<p>
  <br />The second part is the actual report query.&#160; This can be a bit more in depth and contain all kinds of information that helps you take action based on the event that transpired. </p>
<p></p>
<pre class="brush: sql">/*
  =============================================================================================
CREATE DATE:&#160;&#160;&#160;&#160; 04/12/2010
LAST MODIFIED:&#160;&#160;&#160; 04/12/2010
CREATED BY:&#160;&#160;&#160;&#160;&#160;&#160;&#160; SETH PHELABAUM
PURPOSE:&#160;&#160;&#160;&#160;&#160;&#160;&#160; Pulls information about queries that use a large amount of CPU
ISSUES:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Will Notify you Repeatedly.
Notes:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; This can be expanded upon quite a bit.&#160; For instance, you could also include:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Trace events&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Blocked Processes&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; system stats (current cpu usage/io etc.)&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Much more...
============================================================================================= 

*/
CREATE PROCEDURE Rpt_HighCPU(&#160;&#160;&#160;&#160; @SPID&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; int
)
AS

DECLARE @sql_handle varbinary(64),&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; @stmt_start Int,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; @stmt_end&#160;&#160;&#160; Int,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; @FNGS&#160;&#160;&#160;&#160;&#160;&#160;&#160; nvarchar(max),&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; @DBIB&#160;&#160;&#160;&#160;&#160;&#160;&#160; nvarchar(4000) 

SELECT top 1 @sql_handle = sql_handle, @stmt_start = stmt_start, @stmt_end = stmt_end from sys.sysprocesses (nolock)
  WHERE spid = @spid

ORDER BY sql_handle DESC --Or stmt_start DESC 

SELECT @FNGS = CASE WHEN @stmt_start &gt; 0&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; THEN SUBSTRING(text, (@stmt_start + 2)/2,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; CASE @stmt_end&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; WHEN -1 THEN (datalength(text))&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ELSE (@stmt_end - @stmt_start +2)/2&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; END)&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ELSE [Text]&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; END&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; FROM ::fn_get_sql(@sql_handle) 

CREATE TABLE #B(eventtype nvarchar(30), parameters int, eventinfo nvarchar(4000))

 INSERT INTO #B(EventType, Parameters, EventInfo)
EXEC ('dbcc inputbuffer (' + @spid + ') with no_infomsgs') 

SELECT @DBIB = EventInfo FROM #B 

SELECT&#160;&#160;&#160; TOP 1&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; @FNGS FNGS,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; @DBIB DBIB,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; cpu,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; physical_io,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; memusage,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; status,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; nt_username,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; last_batch
from sys.sysprocesses

where spid = @SPID

ORDER BY sql_handle DESC</pre>
<p>
  <br />We also need something that will put a strain on the server to demonstrate the report in action, so I created this ridiculous little SP to run for a while. </p>
<p></p>
<pre class="brush:sql">/CREATEPROCEDURE dbo.SillyLongRun
  AS
exec dbo.SillyLongRun2
  GO 

/*
  =============================================================================================
CREATE DATE:&#160;&#160;&#160;&#160; 04/12/2010
LAST MODIFIED:&#160;&#160;&#160; 04/12/2010
CREATED BY:&#160;&#160;&#160;&#160;&#160;&#160;&#160; SETH PHELABAUM
PURPOSE:&#160;&#160;&#160;&#160;&#160;&#160;&#160; To run for a while.
ISSUES:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Totally Pointless
Notes:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Header here mainly to demonstrate the usage of stmt_start and stmt_end with fn_get_sql.&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; This thing sucks up resources, so don't run it on a production box.
=============================================================================================
exec dbo.SillyLongRun2 

*/
  CREATE PROCEDURE dbo.SillyLongRun2

AS 

; WITH
  -- Tally table Gen&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; Tally Rows:&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; X2&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; X3
t1 AS (SELECT 1 N UNION ALL SELECT 1 N),&#160;&#160;&#160;&#160;&#160;&#160;&#160; -- 4&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ,&#160;&#160;&#160; 8
t2 AS (SELECT 1 N FROM t1 x, t1 y),&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; -- 16&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ,&#160;&#160;&#160; 64
t3 AS (SELECT 1 N FROM t2 x, t2 y),&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; -- 256&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ,&#160;&#160;&#160; 4096
t4 AS (SELECT 1 N FROM t3 x, t3 y),&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; -- 65536&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ,&#160;&#160;&#160; 16,777,216
t5 AS (SELECT 1 N FROM t4 x, t4 y),&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; -- 4,294,967,296&#160;&#160;&#160; ,&#160;&#160;&#160; A lot
Tally AS (SELECT CAST(ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) as bigint) N
FROM t5 x, t5 y) -- Change the t3's to one of the other numbers above for more/less rows 

SELECT N, CAST(N as varchar(30)), DATEADD(ms,n,0)
  FROM Tally</pre>
<p>(You can call the above with exec dbo.SillyLongRun)</p>
<p>As mentioned in the headers, you would ideally keep a log of when you were notified about things.&#160; Different alerts could be scheduled to have a different frequency.&#160; Perhaps you only want to be notified about certain things once a week, but other things you want to be notified about once an hour until they are taken care of.&#160; This is where a logging table comes in.&#160; I won’t go into that here, but wanted to mention it.</p>
<p>Now that we have the queries, we need to set up the report.&#160; I’m going to assume that you already have Reporting Services set up.&#160; Here is a Screenshot of a very basic report that I created to pull in the data.</p>
<p><a href="http://phelabaum.com/wp-content/uploads/2010/04/ReportSetup.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="ReportSetup" border="0" alt="ReportSetup" src="http://phelabaum.com/wp-content/uploads/2010/04/ReportSetup_thumb.jpg" width="381" height="251" /></a></p>
<p><a href="http://phelabaum.com/wp-content/uploads/2010/04/ReportDesignMode.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="ReportDesignMode" border="0" alt="ReportDesignMode" src="http://phelabaum.com/wp-content/uploads/2010/04/ReportDesignMode_thumb.jpg" width="398" height="220" /></a></p>
<p>Once you deploy this report, there are a couple more things you need to do before you can create a data driven subscription for it.&#160; The first is to set up a shared Schedule.&#160; Log into your reports server (<a href="http://localhost/reports">http://localhost/reports</a>) and go to Site Settings at the top.&#160; Then click on schedules at the left and New Schedule.&#160; For this one I’m just going to create a basic 15m recurring schedule.</p>
<p><a href="http://phelabaum.com/wp-content/uploads/2010/04/SharedSchedule.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="SharedSchedule" border="0" alt="SharedSchedule" src="http://phelabaum.com/wp-content/uploads/2010/04/SharedSchedule_thumb.jpg" width="407" height="305" /></a></p>
<p>Next we need to modify the credentials of the shared data source used for the report.&#160; My data source name for the report is SS2K8CL100.&#160; To modify it, I go back to Home –&gt; Data Sources –&gt; SS2K8CL100.&#160; The below screenshot shows me modifying it to use a windows account.</p>
<p><a href="http://phelabaum.com/wp-content/uploads/2010/04/SecuritySettings.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="SecuritySettings" border="0" alt="SecuritySettings" src="http://phelabaum.com/wp-content/uploads/2010/04/SecuritySettings_thumb.jpg" width="417" height="354" /></a></p>
<p>Now, we’re ready to create the data driven subscription.&#160; Rather than explain it in text, I’ve taken screenshots of each step of creating a data driven subscription.</p>
<p><a href="http://phelabaum.com/wp-content/uploads/2010/04/DDS.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="DDS" border="0" alt="DDS" src="http://phelabaum.com/wp-content/uploads/2010/04/DDS_thumb.jpg" width="244" height="126" /></a> <a href="http://phelabaum.com/wp-content/uploads/2010/04/DDS2.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="DDS2" border="0" alt="DDS2" src="http://phelabaum.com/wp-content/uploads/2010/04/DDS2_thumb.jpg" width="244" height="118" /></a> <a href="http://phelabaum.com/wp-content/uploads/2010/04/DDS3.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="DDS3" border="0" alt="DDS3" src="http://phelabaum.com/wp-content/uploads/2010/04/DDS3_thumb.jpg" width="244" height="117" /></a> <a href="http://phelabaum.com/wp-content/uploads/2010/04/DDS4.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="DDS4" border="0" alt="DDS4" src="http://phelabaum.com/wp-content/uploads/2010/04/DDS4_thumb.jpg" width="244" height="91" /></a> <a href="http://phelabaum.com/wp-content/uploads/2010/04/DDS5.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="DDS5" border="0" alt="DDS5" src="http://phelabaum.com/wp-content/uploads/2010/04/DDS5_thumb.jpg" width="244" height="46" /></a> <a href="http://phelabaum.com/wp-content/uploads/2010/04/DDS6.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="DDS6" border="0" alt="DDS6" src="http://phelabaum.com/wp-content/uploads/2010/04/DDS6_thumb.jpg" width="244" height="102" /></a> <a href="http://phelabaum.com/wp-content/uploads/2010/04/DDS7.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="DDS7" border="0" alt="DDS7" src="http://phelabaum.com/wp-content/uploads/2010/04/DDS7_thumb.jpg" width="244" height="114" /></a></p>
<p>Click Finish and you have your report.</p>
<p><a href="http://phelabaum.com/wp-content/uploads/2010/04/image1.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://phelabaum.com/wp-content/uploads/2010/04/image_thumb1.png" width="436" height="305" /></a></p>
<p>In closing, I’ll note that I had a lot of problems getting Reporting Services to function correctly on my windows 7 installation, so this isn’t as polished as I would have liked.&#160; I didn’t get the email working and I forgot to include SPID anywhere on the report (pretty useful piece of information to have)</p>
]]></content:encoded>
			<wfw:commentRss>http://phelabaum.com/archive/2010/04/t-sql-tuesday-monitoring-reports-with-ssrs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

