<?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</title>
	<atom:link href="http://phelabaum.com/archive/tag/t-sql/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>Create Indexes from the Missing Indexes DMV</title>
		<link>http://phelabaum.com/archive/2010/05/create-indexes-from-the-missing-indexes-dmv/</link>
		<comments>http://phelabaum.com/archive/2010/05/create-indexes-from-the-missing-indexes-dmv/#comments</comments>
		<pubDate>Mon, 24 May 2010 15:43:01 +0000</pubDate>
		<dc:creator>Seth Phelabaum</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[DMV's]]></category>
		<category><![CDATA[Indexes]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://phelabaum.com/archive/2010/05/create-indexes-from-the-missing-indexes-dmv/</guid>
		<description><![CDATA[Glenn Berry (Blog) writes a lot of queries to extract information from the system DMV’s.&#160; One of them in particular I found extremely helpful in fixing some of the issues in my system.&#160; I took his query (the CTE at the top) and added some text manipulation to actually generate the create statements for you [...]]]></description>
			<content:encoded><![CDATA[<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"><font color="#000000" face="Calibri">Glenn Berry (<a href="http://glennberrysqlperformance.spaces.live.com" target="_blank">Blog</a>) writes a lot of queries to extract information from the system DMV’s.&#160; One of them in particular I found extremely helpful in fixing some of the issues in my system.&#160; I took his query (the CTE at the top) and added some text manipulation to actually generate the create statements for you to save you some time.&#160; I had much grander plans for this, but unfortunately I’ve been meaning to post this for over a month now and simply haven’t had time to get back to it, so rather than just let it go by the wayside and never post it, I figured I’d just post what I had now and then possibly post an update sometime in the future if I ever finish it.&#160; </font></span></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"><font color="#000000" size="2" face="Calibri"></font></span></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"><font color="#000000" face="Calibri">A couple of the known problems right now are:&#160; </font></span></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"><font color="#000000" size="2" face="Calibri"></font></span></p>
<ul>
<li>
<div 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"><font color="#000000" face="Calibri">Index names could already be taken, there’s nothing here that checks to make sure they are unique based on other indexes in your database.</font></span></div>
</li>
<li>
<div 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"><font color="#000000" face="Calibri">No compression options are taken into account.</font></span></div>
</li>
</ul>
<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"><font color="#000000" size="2" face="Calibri"></font></span></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"><font color="#000000" face="Calibri">That said, I still found this fairly useful and hopefully somebody else will as well.&#160; Thanks again to Glenn for all his excellent work at creating queries to pull information from the DMV’s.</font></span></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 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: blue; font-size: 10pt; mso-no-proof: yes">WITH</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> I <span style="color: blue">AS </span><span style="color: gray">(
</p>
<p>     </span></span></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; Missing Indexes current database by Index Advantage
</p>
<p>   </span></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; This DMV Query written by Glenn Berry
</p>
<p>   </span></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"> user_seeks <span style="color: gray">*</span> avg_total_user_cost <span style="color: gray">*</span>
</p>
<p>   </span></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;; font-size: 10pt; mso-no-proof: yes">avg_user_impact <span style="color: gray">*</span> <span style="color: #ff8000">0.01</span><span style="color: gray">)</span> <span style="color: blue">AS</span> [index_advantage]<span style="color: gray">,</span>
</p>
<p>   </span></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">migs<span style="color: gray">.</span>last_user_seek<span style="color: gray">,</span>
</p>
<p>   </span></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">mid<span style="color: gray">.</span>[statement] <span style="color: blue">AS</span> [Database.Schema.Table]<span style="color: gray">,
</p>
<p>     </span></span></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">mid<span style="color: gray">.</span>equality_columns<span style="color: gray">,</span> mid<span style="color: gray">.</span>inequality_columns<span style="color: gray">,</span>
</p>
<p>   </span></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">mid<span style="color: gray">.</span>included_columns<span style="color: gray">,</span>migs<span style="color: gray">.</span>unique_compiles<span style="color: gray">,</span> migs<span style="color: gray">.</span>user_seeks<span style="color: gray">,</span>
</p>
<p>   </span></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">migs<span style="color: gray">.</span>avg_total_user_cost<span style="color: gray">,</span> migs<span style="color: gray">.</span>avg_user_impact
</p>
<p>   </span></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">FROM</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> <span style="color: green">sys</span><span style="color: gray">.</span><span style="color: green">dm_db_missing_index_group_stats</span> <span style="color: blue">AS</span> migs <span style="color: blue">WITH </span><span style="color: gray">(</span><span style="color: blue">NOLOCK</span><span style="color: gray">)
</p>
<p>     </span></span></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">INNER</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> <span style="color: gray">JOIN</span> <span style="color: green">sys</span><span style="color: gray">.</span><span style="color: green">dm_db_missing_index_groups</span> <span style="color: blue">AS</span> mig <span style="color: blue">WITH </span><span style="color: gray">(</span><span style="color: blue">NOLOCK</span><span style="color: gray">)
</p>
<p>     </span></span></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">ON</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> migs<span style="color: gray">.</span>group_handle <span style="color: gray">=</span> mig<span style="color: gray">.</span>index_group_handle
</p>
<p>   </span></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">INNER</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> <span style="color: gray">JOIN</span> <span style="color: green">sys</span><span style="color: gray">.</span><span style="color: green">dm_db_missing_index_details</span> <span style="color: blue">AS</span> mid <span style="color: blue">WITH </span><span style="color: gray">(</span><span style="color: blue">NOLOCK</span><span style="color: gray">)
</p>
<p>     </span></span></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">ON</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> mig<span style="color: gray">.</span>index_handle <span style="color: gray">=</span> mid<span style="color: gray">.</span>index_handle
</p>
<p>   </span></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">WHERE</span><span style="font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> mid<span style="color: gray">.</span>database_id <span style="color: gray">=</span> <span style="color: fuchsia">DB_ID</span><span style="color: gray">()</span>
</p>
<p>   </span></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 style="mso-tab-count: 1">&#160;&#160;&#160;&#160;&#160; </span><span style="color: gray">AND</span> user_seeks <span style="color: gray">*</span> avg_total_user_cost <span style="color: gray">*</span>
</p>
<p>   </span></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;; font-size: 10pt; mso-no-proof: yes">avg_user_impact <span style="color: gray">*</span> <span style="color: #ff8000">0.01</span><span style="color: gray">)</span> <span style="color: gray">&gt;</span> <span style="color: #ff8000">9000</span> <span style="color: green">&#8211; Set this to Whatever
</p>
<p>     </span></span></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">)
</p>
<p>   </span></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">
<p>&#160;</p>
<p>   </span></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: red">&#8216;CREATE INDEX IX_&#8217;
</p>
<p>     </span></span></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 style="mso-tab-count: 2">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: gray">+</span> <span style="color: fuchsia">SUBSTRING</span><span style="color: gray">(</span>[Database.Schema.Table]<span style="color: gray">,
</p>
<p>     </span></span></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 style="mso-tab-count: 5">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: fuchsia">CHARINDEX</span><span style="color: gray">(</span><span style="color: red">&#8216;].['</span><span style="color: gray">,</span>[Database.Schema.Table]<span style="color: gray">,
</p>
<p>     </span></span></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 style="mso-tab-count: 5">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: fuchsia">CHARINDEX</span><span style="color: gray">(</span><span style="color: red">&#8216;].['</span><span style="color: gray">,</span>[Database.Schema.Table]<span style="color: gray">)+</span><span style="color: #ff8000">4</span><span style="color: gray">)+</span><span style="color: #ff8000">3</span><span style="color: gray">,
</p>
<p>     </span></span></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 style="mso-tab-count: 5">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: fuchsia">LEN</span><span style="color: gray">(</span>[Database.Schema.Table]<span style="color: gray">)</span> <span style="color: gray">-</span><span style="mso-spacerun: yes">&#160; </span>
</p>
<p>   </span></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: 5">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#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: fuchsia; font-size: 10pt; mso-no-proof: yes">CHARINDEX</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;].['</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">[Database.Schema.Table]<span style="color: gray">,
</p>
<p>     </span></span></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 style="mso-tab-count: 5">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: fuchsia">CHARINDEX</span><span style="color: gray">(</span><span style="color: red">&#8216;].['</span><span style="color: gray">,</span>[Database.Schema.Table]<span style="color: gray">)+</span><span style="color: #ff8000">4</span><span style="color: gray">)+</span><span style="color: #ff8000">3</span><span style="color: gray">))
</p>
<p>     </span></span></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 style="mso-tab-count: 2">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: gray">+</span> <span style="color: red">&#8216;_&#8217;</span> <span style="color: gray">+</span> <span style="color: gray">LEFT(</span><span style="color: fuchsia">REPLACE</span><span style="color: gray">(</span><span style="color: fuchsia">REPLACE</span><span style="color: gray">(</span><span style="color: fuchsia">REPLACE</span><span style="color: gray">(</span><span style="color: fuchsia">REPLACE</span><span style="color: gray">(
</p>
<p>     </span></span></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 style="mso-tab-count: 2">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: fuchsia">ISNULL</span><span style="color: gray">(</span>Equality_Columns<span style="color: gray">,</span>inequality_columns<span style="color: gray">),
</p>
<p>     </span></span></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 style="mso-tab-count: 2">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: red">&#8216;['</span><span style="color: gray">,</span><span style="color: red">''</span><span style="color: gray">),</span><span style="color: red">']&#8216;</span><span style="color: gray">,</span><span style="color: red">&#8221;</span><span style="color: gray">),</span><span style="color: red">&#8216; &#8216;</span><span style="color: gray">,</span><span style="color: red">&#8221;</span><span style="color: gray">),</span><span style="color: red">&#8216;,&#8217;</span><span style="color: gray">,</span><span style="color: red">&#8221;</span><span style="color: gray">),</span><span style="color: #ff8000">20</span><span style="color: gray">)
</p>
<p>     </span></span></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 style="mso-tab-count: 2">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: gray">+</span> <span style="color: red">&#8216; ON &#8216;
</p>
<p>     </span></span></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 style="mso-tab-count: 2">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: gray">+</span> [Database.Schema.Table]
</p>
<p>   </span></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 style="mso-tab-count: 2">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: gray">+</span> <span style="color: red">&#8216;(&#8216;
</p>
<p>     </span></span></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 style="mso-tab-count: 2">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: gray">+</span> <span style="color: fuchsia">ISNULL</span><span style="color: gray">(</span>equality_columns<span style="color: gray">,</span><span style="color: red">&#8221;</span><span style="color: gray">)
</p>
<p>     </span></span></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 style="mso-tab-count: 2">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: gray">+</span> <span style="color: blue">CASE</span> <span style="color: blue">WHEN</span> equality_columns <span style="color: gray">IS</span> <span style="color: gray">NOT</span> <span style="color: gray">NULL</span> <span style="color: gray">AND</span>
</p>
<p>   </span></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 style="mso-tab-count: 5">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>inequality_columns <span style="color: gray">IS</span> <span style="color: gray">NOT</span> <span style="color: gray">NULL</span>
</p>
<p>   </span></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 style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: blue">THEN</span> <span style="color: red">&#8216;,&#8217;</span>
</p>
<p>   </span></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 style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: blue">ELSE</span> <span style="color: red">&#8221;</span>
</p>
<p>   </span></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 style="mso-tab-count: 2">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="mso-spacerun: yes">&#160; </span><span style="color: blue">END
</p>
<p>     </span></span></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 style="mso-tab-count: 2">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: gray">+</span> <span style="color: fuchsia">ISNULL</span><span style="color: gray">(</span>inequality_columns<span style="color: gray">,</span><span style="color: red">&#8221;</span><span style="color: gray">)
</p>
<p>     </span></span></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 style="mso-tab-count: 2">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: gray">+</span> <span style="color: red">&#8216;)&#8217;
</p>
<p>     </span></span></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 style="mso-tab-count: 2">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: gray">+</span> <span style="color: blue">CASE</span> <span style="color: blue">WHEN</span> included_columns <span style="color: gray">IS</span> <span style="color: gray">NOT</span> <span style="color: gray">NULL</span>
</p>
<p>   </span></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 style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: blue">THEN</span> <span style="color: red">&#8216; INCLUDE(&#8216;</span> <span style="color: gray">+</span> included_columns <span style="color: gray">+</span> <span style="color: red">&#8216;)&#8217;
</p>
<p>     </span></span></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 style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: blue">ELSE</span> <span style="color: red">&#8221;
</p>
<p>     </span></span></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 style="mso-tab-count: 2">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="mso-spacerun: yes">&#160; </span><span style="color: blue">END</span> CreateStatement<span style="color: gray">,
</p>
<p>     </span></span></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 style="mso-tab-count: 2">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: red">&#8216;IX_&#8217;
</p>
<p>     </span></span></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 style="mso-tab-count: 2">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: gray">+</span> <span style="color: fuchsia">SUBSTRING</span><span style="color: gray">(</span>[Database.Schema.Table]<span style="color: gray">,
</p>
<p>     </span></span></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 style="mso-tab-count: 5">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: fuchsia">CHARINDEX</span><span style="color: gray">(</span><span style="color: red">&#8216;].['</span><span style="color: gray">,</span>[Database.Schema.Table]<span style="color: gray">,
</p>
<p>     </span></span></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 style="mso-tab-count: 5">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: fuchsia">CHARINDEX</span><span style="color: gray">(</span><span style="color: red">&#8216;].['</span><span style="color: gray">,</span>[Database.Schema.Table]<span style="color: gray">)+</span><span style="color: #ff8000">4</span><span style="color: gray">)+</span><span style="color: #ff8000">3</span><span style="color: gray">,
</p>
<p>     </span></span></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 style="mso-tab-count: 5">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: fuchsia">LEN</span><span style="color: gray">(</span>[Database.Schema.Table]<span style="color: gray">)</span> <span style="color: gray">-</span><span style="mso-spacerun: yes">&#160; </span>
</p>
<p>   </span></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: 5">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#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: fuchsia; font-size: 10pt; mso-no-proof: yes">CHARINDEX</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;].['</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">[Database.Schema.Table]<span style="color: gray">,
</p>
<p>     </span></span></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 style="mso-tab-count: 5">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: fuchsia">CHARINDEX</span><span style="color: gray">(</span><span style="color: red">&#8216;].['</span><span style="color: gray">,</span>[Database.Schema.Table]<span style="color: gray">)+</span><span style="color: #ff8000">4</span><span style="color: gray">)+</span><span style="color: #ff8000">3</span><span style="color: gray">))
</p>
<p>     </span></span></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 style="mso-tab-count: 2">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: gray">+</span> <span style="color: red">&#8216;_&#8217;</span> <span style="color: gray">+</span> <span style="color: gray">LEFT(</span><span style="color: fuchsia">REPLACE</span><span style="color: gray">(</span><span style="color: fuchsia">REPLACE</span><span style="color: gray">(</span><span style="color: fuchsia">REPLACE</span><span style="color: gray">(</span><span style="color: fuchsia">REPLACE</span><span style="color: gray">(
</p>
<p>     </span></span></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 style="mso-tab-count: 2">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: fuchsia">ISNULL</span><span style="color: gray">(</span>Equality_Columns<span style="color: gray">,</span>inequality_columns<span style="color: gray">),
</p>
<p>     </span></span></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 style="mso-tab-count: 2">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span style="color: red">&#8216;['</span><span style="color: gray">,</span><span style="color: red">''</span><span style="color: gray">),</span><span style="color: red">']&#8216;</span><span style="color: gray">,</span><span style="color: red">&#8221;</span><span style="color: gray">),</span><span style="color: red">&#8216; &#8216;</span><span style="color: gray">,</span><span style="color: red">&#8221;</span><span style="color: gray">),</span><span style="color: red">&#8216;,&#8217;</span><span style="color: gray">,</span><span style="color: red">&#8221;</span><span style="color: gray">),</span><span style="color: #ff8000">20</span><span style="color: gray">)
</p>
<p>     </span></span></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 style="mso-tab-count: 3">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>IndexName
</p>
<p>   </span></p>
<p class="MsoNormal"><span style="line-height: 115%; font-family: &quot;Courier New&quot;; color: blue; font-size: 10pt; mso-no-proof: yes">FROM</span><span style="line-height: 115%; font-family: &quot;Courier New&quot;; font-size: 10pt; mso-no-proof: yes"> I</span></p>
]]></content:encoded>
			<wfw:commentRss>http://phelabaum.com/archive/2010/05/create-indexes-from-the-missing-indexes-dmv/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>
		<item>
		<title>A T-SQL Holiday Message</title>
		<link>http://phelabaum.com/archive/2010/04/a-t-sql-holiday-message/</link>
		<comments>http://phelabaum.com/archive/2010/04/a-t-sql-holiday-message/#comments</comments>
		<pubDate>Sat, 03 Apr 2010 20:04:06 +0000</pubDate>
		<dc:creator>Seth Phelabaum</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://phelabaum.com/archive/2010/04/a-t-sql-holiday-message/</guid>
		<description><![CDATA[Totally pointless, but fun.  Run it to decode the message. DECLARE @Message VARCHAR&#40;20&#41; SET @Message = '????????????' DECLARE @Decode TABLE&#40; DSeq tinyint, DKey SMALLINT &#41; INSERT INTO @Decode&#40;DSeq, DKey&#41; VALUES&#40;1,9&#41;,&#40;2,2&#41;,&#40;3,17&#41;,&#40;4,17&#41;,&#40;5,26&#41;,&#40;6,6&#41;,&#40;7,2&#41;,&#40;8,20&#41;,&#40;9,21&#41;,&#40;10,6&#41;,&#40;11,19&#41; ; WITH Decoder AS &#40; SELECT D.DSeq N, CHAR&#40; D.DKey + ASCII&#40;SUBSTRING&#40;@Message,D.DSeq,1&#41;&#41; &#41; DV FROM @Decode D&#41;, Conc &#40;S&#41; AS &#40; SELECT DV + '' [...]]]></description>
			<content:encoded><![CDATA[<p>Totally pointless, but fun.  Run it to decode the message.</p>
<div class="codecolorer-container sql mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">DECLARE</span> @Message <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SET</span> @Message <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'????????????'</span><br />
<br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @Decode <span style="color: #993333; font-weight: bold;">TABLE</span><span style="color: #66cc66;">&#40;</span><br />
DSeq tinyint<span style="color: #66cc66;">,</span><br />
DKey <span style="color: #993333; font-weight: bold;">SMALLINT</span><br />
<span style="color: #66cc66;">&#41;</span><br />
<br />
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> @Decode<span style="color: #66cc66;">&#40;</span>DSeq<span style="color: #66cc66;">,</span> DKey<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">VALUES</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">9</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">17</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">17</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">26</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">6</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">7</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">9</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">21</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">19</span><span style="color: #66cc66;">&#41;</span><br />
<br />
; <span style="color: #993333; font-weight: bold;">WITH</span><br />
Decoder <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> D<span style="color: #66cc66;">.</span>DSeq N<span style="color: #66cc66;">,</span><br />
<span style="color: #993333; font-weight: bold;">CHAR</span><span style="color: #66cc66;">&#40;</span> D<span style="color: #66cc66;">.</span>DKey <span style="color: #66cc66;">+</span> ASCII<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SUBSTRING</span><span style="color: #66cc66;">&#40;</span>@Message<span style="color: #66cc66;">,</span>D<span style="color: #66cc66;">.</span>DSeq<span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> DV<br />
<span style="color: #993333; font-weight: bold;">FROM</span> @Decode D<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
Conc <span style="color: #66cc66;">&#40;</span>S<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> DV <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">''</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> Decoder D<br />
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> D<span style="color: #66cc66;">.</span>N<br />
<span style="color: #993333; font-weight: bold;">FOR</span> XML PATH<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> STUFF<span style="color: #66cc66;">&#40;</span>S<span style="color: #66cc66;">,</span><span style="color: #cc66cc;">6</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">' '</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#91;</span>Surprise<span style="color: #66cc66;">&#93;</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> Conc</div></div>
]]></content:encoded>
			<wfw:commentRss>http://phelabaum.com/archive/2010/04/a-t-sql-holiday-message/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>DBCC INPUTBUFFER vs fn_get_sql</title>
		<link>http://phelabaum.com/archive/2010/03/dbcc-inputbuffer-vs-fn-get-sql/</link>
		<comments>http://phelabaum.com/archive/2010/03/dbcc-inputbuffer-vs-fn-get-sql/#comments</comments>
		<pubDate>Sun, 28 Mar 2010 20:57:56 +0000</pubDate>
		<dc:creator>Seth Phelabaum</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Metadata]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://phelabaum.com/archive/2010/03/dbcc-inputbuffer-vs-fn-get-sql/</guid>
		<description><![CDATA[Someone asked a question in the forums the other day and I realized it would make a pretty decent blog post to explain the differences in functionality between these two and provide some code for working with them. Differences fn_get_sql returns the last *statement* executed, where as DBCC INPUTBUFFER returns the first statement in the [...]]]></description>
			<content:encoded><![CDATA[<p>Someone asked a question in the forums the other day and I realized it would make a pretty decent blog post to explain the differences in functionality between these two and provide some code for working with them.</p>
<p><span style="text-decoration: underline;">Differences</span></p>
<ul>
<li>fn_get_sql returns the last *statement* executed, where as DBCC INPUTBUFFER returns the first statement in the batch.  See the example below for a better idea of what that means.</li>
<li>fn_get_sql returns a TEXT field containing the statement that ran, DBCC INPUTBUFFER returns only a varchar(255).  This is important as you often won&#8217;t get the full line here&#8230; and unfortunately there&#8217;s not a lot you can do about that.  To my knowledge, DBCC INPUTBUFFER is still your only means of getting the first statement in the batch, however it returns an nvarchar(4000) in 2005/2008.</li>
<li>fn_get_sql requires a SQL_Handle binary type passed in which has to be pulled out of the sysprocesses table, DBCC INPUTBUFFER only requires the SPID as a parameter.</li>
<li>fn_get_sql will return the actual creation text for an SP(or trigger, etc.) that is executing instead of all the name of the object with the parameters that DBCC INPUTBUFFER returns.</li>
</ul>
<p>Note that fn_get_sql is available in SQL 2000 if you have sp3 and above.  You may actually have it with SP2 as well if you have the <a href="http://support.microsoft.com/kb/810010" target="_blank">correct hotfix</a> applied.  An easy way to tell if it will work on your system is to do a simple SELECT sql_handle FROM sysprocesses.  If that works, so will fn_get_sql.  If it doesn&#8217;t, you need to patch.   There are a couple other specifics about fn_get_sql that are mentioned in the article linked at the bottom by Andrew Novick.</p>
<p><span style="text-decoration: underline;">Sample Code</span></p>
<p>Both of these often require insertion into a table to work with.  Here is some sample code that will create the tables for you and insert the rows of the currently running SPID.</p>
<div class="codecolorer-container sql mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">--- Change eventinfo to nvarchar(4000) for sql 2005 and 2008</span><br />
REATE <span style="color: #993333; font-weight: bold;">TABLE</span> #B<span style="color: #66cc66;">&#40;</span>eventtype nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> parameters <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">,</span> eventinfo nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> #B<span style="color: #66cc66;">&#40;</span>EventType<span style="color: #66cc66;">,</span> Parameters<span style="color: #66cc66;">,</span> EventInfo<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">EXEC</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'dbcc inputbuffer ('</span> <span style="color: #66cc66;">+</span> @@spid <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">') with no_infomsgs'</span><span style="color: #66cc66;">&#41;</span><br />
<br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @handle <span style="color: #993333; font-weight: bold;">BINARY</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @handle <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#40;</span>sql_handle<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> master<span style="color: #66cc66;">..</span>sysprocesses <span style="color: #993333; font-weight: bold;">WHERE</span> spid <span style="color: #66cc66;">=</span> @@SPID<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> ::fn_get_sql<span style="color: #66cc66;">&#40;</span>@handle<span style="color: #66cc66;">&#41;</span></div></div>
<p><span style="text-decoration: underline;">Additional Links:</span></p>
<p>Andrew Novick: <a href="http://www.databasejournal.com/features/mssql/article.php/10894_2189761_2/Find-Out-What-Theyre-Doing-with-fngetsql.htm" target="_blank">Find out what they’re doing with fn_get_sql</a><br />
BOL: <a href="http://msdn.microsoft.com/en-us/library/ms181929.aspx" target="_blank">sys.dm_exec_sql_text</a><br />
BOL: <a href="http://msdn.microsoft.com/en-us/library/ms189451.aspx" target="_blank">fn_get_sql</a><br />
BOL: DBCC INPUTBUFFER: <a href="http://msdn.microsoft.com/en-us/library/ms187730.aspx" target="_blank">2008</a> | <a href="http://msdn.microsoft.com/en-us/library/ms187730(SQL.90).aspx" target="_blank">2005</a> | <a href="http://msdn.microsoft.com/en-us/library/aa258826(SQL.80).aspx" target="_blank">2000</a></p>
<p><span style="text-decoration: underline;">Examples:</span></p>
<p>Here are a couple examples that show some of the differences between the two in action.</p>
<div class="codecolorer-container sql mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">USE</span> tempdb<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">PROCEDURE</span> Proc1<span style="color: #66cc66;">&#40;</span><br />
@invar        <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
<br />
<span style="color: #993333; font-weight: bold;">SET</span> NOCOUNT <span style="color: #993333; font-weight: bold;">ON</span><br />
<br />
PRINT <span style="color: #ff0000;">'Proc 1 Executing'</span><br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @fgs    nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
@handle varbinary<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">64</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
@dbib    nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @handle <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#40;</span>sql_handle<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> master<span style="color: #66cc66;">..</span>sysprocesses <span style="color: #993333; font-weight: bold;">WHERE</span> spid <span style="color: #66cc66;">=</span> @@SPID<br />
<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> #A<span style="color: #66cc66;">&#40;</span>eventtype nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> parameters <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">,</span> eventinfo nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> #A<span style="color: #66cc66;">&#40;</span>EventType<span style="color: #66cc66;">,</span> Parameters<span style="color: #66cc66;">,</span> EventInfo<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">EXEC</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'dbcc inputbuffer ('</span> <span style="color: #66cc66;">+</span> @@spid <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">') with no_infomsgs'</span><span style="color: #66cc66;">&#41;</span><br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @fgs <span style="color: #66cc66;">=</span> text <span style="color: #993333; font-weight: bold;">FROM</span> ::fn_get_sql<span style="color: #66cc66;">&#40;</span>@handle<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @dbib <span style="color: #66cc66;">=</span> eventinfo <span style="color: #993333; font-weight: bold;">FROM</span> #A<br />
PRINT <span style="color: #ff0000;">'fn_get_sql'</span><br />
PRINT <span style="color: #ff0000;">'---------------------'</span><br />
PRINT @FGS<br />
PRINT <span style="color: #ff0000;">'DBCC INPUTBUFFER:'</span><br />
PRINT <span style="color: #ff0000;">'---------------------'</span><br />
PRINT @dbib<br />
<br />
<span style="color: #993333; font-weight: bold;">EXEC</span> Proc2 <span style="color: #ff0000;">'Executing Second Proc'</span><br />
<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">PROCEDURE</span> Proc2<span style="color: #66cc66;">&#40;</span><br />
@invar    <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
<br />
<span style="color: #993333; font-weight: bold;">SET</span> NOCOUNT <span style="color: #993333; font-weight: bold;">ON</span><br />
<br />
PRINT <span style="color: #ff0000;">'Proc 2 Executing'</span><br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @fgs    nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
@handle varbinary<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">64</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
@dbib    nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @handle <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#40;</span>sql_handle<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> master<span style="color: #66cc66;">..</span>sysprocesses <span style="color: #993333; font-weight: bold;">WHERE</span> spid <span style="color: #66cc66;">=</span> @@SPID<br />
<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> #B<span style="color: #66cc66;">&#40;</span>eventtype nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> parameters <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">,</span> eventinfo nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> #B<span style="color: #66cc66;">&#40;</span>EventType<span style="color: #66cc66;">,</span> Parameters<span style="color: #66cc66;">,</span> EventInfo<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">EXEC</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'dbcc inputbuffer ('</span> <span style="color: #66cc66;">+</span> @@spid <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">') with no_infomsgs'</span><span style="color: #66cc66;">&#41;</span><br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @fgs <span style="color: #66cc66;">=</span> text <span style="color: #993333; font-weight: bold;">FROM</span> ::fn_get_sql<span style="color: #66cc66;">&#40;</span>@handle<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @dbib <span style="color: #66cc66;">=</span> eventinfo <span style="color: #993333; font-weight: bold;">FROM</span> #B<br />
PRINT <span style="color: #ff0000;">'fn_get_sql'</span><br />
PRINT <span style="color: #ff0000;">'---------------------'</span><br />
PRINT @FGS<br />
PRINT <span style="color: #ff0000;">'DBCC INPUTBUFFER:'</span><br />
PRINT <span style="color: #ff0000;">'---------------------'</span><br />
PRINT @dbib<br />
<span style="color: #993333; font-weight: bold;">EXEC</span> Proc3 <span style="color: #ff0000;">'Executing Third Proc'</span><br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">PROCEDURE</span> Proc3<span style="color: #66cc66;">&#40;</span><br />
@invar    <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
<br />
<span style="color: #993333; font-weight: bold;">SET</span> NOCOUNT <span style="color: #993333; font-weight: bold;">ON</span><br />
PRINT <span style="color: #ff0000;">'Proc 3 Executing'</span><br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @fgs    nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
@handle varbinary<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">64</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
@dbib    nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @handle <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#40;</span>sql_handle<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> master<span style="color: #66cc66;">..</span>sysprocesses <span style="color: #993333; font-weight: bold;">WHERE</span> spid <span style="color: #66cc66;">=</span> @@SPID<br />
<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> #C<span style="color: #66cc66;">&#40;</span>eventtype nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> parameters <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">,</span> eventinfo nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> #C<span style="color: #66cc66;">&#40;</span>EventType<span style="color: #66cc66;">,</span> Parameters<span style="color: #66cc66;">,</span> EventInfo<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">EXEC</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'dbcc inputbuffer ('</span> <span style="color: #66cc66;">+</span> @@spid <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">') with no_infomsgs'</span><span style="color: #66cc66;">&#41;</span><br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @fgs <span style="color: #66cc66;">=</span> text <span style="color: #993333; font-weight: bold;">FROM</span> ::fn_get_sql<span style="color: #66cc66;">&#40;</span>@handle<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @dbib <span style="color: #66cc66;">=</span> eventinfo <span style="color: #993333; font-weight: bold;">FROM</span> #C<br />
PRINT <span style="color: #ff0000;">'fn_get_sql'</span><br />
PRINT <span style="color: #ff0000;">'---------------------'</span><br />
PRINT @FGS<br />
PRINT <span style="color: #ff0000;">'DBCC INPUTBUFFER:'</span><br />
PRINT <span style="color: #ff0000;">'---------------------'</span><br />
PRINT @dbib<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<br />
<span style="color: #993333; font-weight: bold;">EXEC</span> Proc1 <span style="color: #ff0000;">'Executing Proc 1'</span><br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<br />
<span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">PROCEDURE</span> Proc1<br />
<span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">PROCEDURE</span> Proc2<br />
<span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">PROCEDURE</span> Proc3<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<br />
<span style="color: #808080; font-style: italic;">---- Example 2 -----</span><br />
PRINT <span style="color: #ff0000;">'------------------------------- Example 2 ---------------------------------------'</span><br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> A<span style="color: #66cc66;">&#40;</span><br />
aval <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TRIGGER</span> Atrig <span style="color: #993333; font-weight: bold;">ON</span> A<br />
<span style="color: #993333; font-weight: bold;">FOR</span> <span style="color: #993333; font-weight: bold;">INSERT</span><br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #993333; font-weight: bold;">SET</span> NOCOUNT <span style="color: #993333; font-weight: bold;">ON</span><br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @fgs    nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
@handle varbinary<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">64</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
@dbib    nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @handle <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#40;</span>sql_handle<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FROM</span> master<span style="color: #66cc66;">..</span>sysprocesses <span style="color: #993333; font-weight: bold;">WHERE</span> spid <span style="color: #66cc66;">=</span> @@SPID<br />
<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> #C<span style="color: #66cc66;">&#40;</span>eventtype nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> parameters <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">,</span> eventinfo nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4000</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> #C<span style="color: #66cc66;">&#40;</span>EventType<span style="color: #66cc66;">,</span> Parameters<span style="color: #66cc66;">,</span> EventInfo<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">EXEC</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'dbcc inputbuffer ('</span> <span style="color: #66cc66;">+</span> @@spid <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">') with no_infomsgs'</span><span style="color: #66cc66;">&#41;</span><br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @fgs <span style="color: #66cc66;">=</span> text <span style="color: #993333; font-weight: bold;">FROM</span> ::fn_get_sql<span style="color: #66cc66;">&#40;</span>@handle<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @dbib <span style="color: #66cc66;">=</span> eventinfo <span style="color: #993333; font-weight: bold;">FROM</span> #C<br />
PRINT <span style="color: #ff0000;">'fn_get_sql'</span><br />
PRINT <span style="color: #ff0000;">'---------------------'</span><br />
PRINT @FGS<br />
PRINT <span style="color: #ff0000;">'DBCC INPUTBUFFER:'</span><br />
PRINT <span style="color: #ff0000;">'---------------------'</span><br />
PRINT @dbib<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<br />
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> A<span style="color: #66cc66;">&#40;</span>aval<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">123456</span><br />
<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TABLE</span> A<br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://phelabaum.com/archive/2010/03/dbcc-inputbuffer-vs-fn-get-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tally Table CTE</title>
		<link>http://phelabaum.com/archive/2010/03/tally-table-cte/</link>
		<comments>http://phelabaum.com/archive/2010/03/tally-table-cte/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 13:24:48 +0000</pubDate>
		<dc:creator>Seth Phelabaum</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Tally Table]]></category>

		<guid isPermaLink="false">http://phelabaum.com/archive/2010/03/tally-table-cte/</guid>
		<description><![CDATA[Now that I have several posts on what you can do with a Tally table, I figured I&#8217;d share my favorite way to create one inline.  I still prefer to have a physical tally table (usually in a Utility database that can be accessed from anywhere and doesn&#8217;t need to be created in each individual [...]]]></description>
			<content:encoded><![CDATA[<p>Now that I have several posts on what you can do with a Tally table, I figured I&#8217;d share my favorite way to create one inline.  I still prefer to have a physical tally table (usually in a Utility database that can be accessed from anywhere and doesn&#8217;t need to be created in each individual database) for permament code, but for times when you need one on the fly, this is my preferred method.  I can&#8217;t really take the credit for this query, the base construct is based on something I&#8217;ve seen attributed to Itzik Ben-Gan.   I&#8217;ve modified it a bit and changed up the formatting to be the way I like it.  Anything over a few thousand rows I&#8217;d probably use a physical tally table for, but on small numbers you shouldn&#8217;t see much of a performance hit with this script.</p>
<div class="codecolorer-container sql mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">-- Tally Table CTE script (SQL 2005+ only)</span><br />
<span style="color: #808080; font-style: italic;">-- You can use this to create many different numbers of rows... for example:</span><br />
<span style="color: #808080; font-style: italic;">-- You could use a 3 way cross join (t3 x, t3 y, t3 z) instead of just 2 way to generate a different number of rows.</span><br />
<span style="color: #808080; font-style: italic;">-- The # of rows this would generate for each is noted in the X3 comment column below.</span><br />
<span style="color: #808080; font-style: italic;">-- For most common usage, I find t3 or t4 to be enough, so that is what is coded here.</span><br />
<span style="color: #808080; font-style: italic;">-- If you use t3 in ‘Tally’, you can delete t4 and t5.</span><br />
<br />
; <span style="color: #993333; font-weight: bold;">WITH</span><br />
<span style="color: #808080; font-style: italic;">-- Tally table Gen            Tally Rows:     X2                X3</span><br />
t1 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">1</span> N <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">1</span> N<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>    <span style="color: #808080; font-style: italic;">-- 4            ,    8</span><br />
t2 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">1</span> N <span style="color: #993333; font-weight: bold;">FROM</span> t1 x<span style="color: #66cc66;">,</span> t1 y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>            <span style="color: #808080; font-style: italic;">-- 16            ,    64</span><br />
t3 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">1</span> N <span style="color: #993333; font-weight: bold;">FROM</span> t2 x<span style="color: #66cc66;">,</span> t2 y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>            <span style="color: #808080; font-style: italic;">-- 256            ,    4096</span><br />
t4 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">1</span> N <span style="color: #993333; font-weight: bold;">FROM</span> t3 x<span style="color: #66cc66;">,</span> t3 y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>            <span style="color: #808080; font-style: italic;">-- 65536        ,    16,777,216</span><br />
t5 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">1</span> N <span style="color: #993333; font-weight: bold;">FROM</span> t4 x<span style="color: #66cc66;">,</span> t4 y<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>            <span style="color: #808080; font-style: italic;">-- 4,294,967,296,    A lot</span><br />
Tally <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">ROW_NUMBER</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">OVER</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> N<br />
<span style="color: #993333; font-weight: bold;">FROM</span> t3 x<span style="color: #66cc66;">,</span> t3 y<span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">-- Change the t3's to one of the other numbers above for more/less rows</span></div></div>
]]></content:encoded>
			<wfw:commentRss>http://phelabaum.com/archive/2010/03/tally-table-cte/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tally Table &#8211; Delimited list to Table</title>
		<link>http://phelabaum.com/archive/2010/02/tally-table-delimited-list-to-table/</link>
		<comments>http://phelabaum.com/archive/2010/02/tally-table-delimited-list-to-table/#comments</comments>
		<pubDate>Sat, 27 Feb 2010 20:49:27 +0000</pubDate>
		<dc:creator>Seth Phelabaum</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Scripts]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Tally Table]]></category>

		<guid isPermaLink="false">http://phelabaum.com/archive/2010/02/tally-table-delimited-list-to-table/</guid>
		<description><![CDATA[Dealing with delimited lists (Usually separated by a comma) in SQL is a problem easily handled by a simple function and a Tally Table.  (Tally tables are also often referred to as Numbers tables or spt_values tables.  If you still don&#8217;t know what that is, please see this excellent article on Tally tables written by [...]]]></description>
			<content:encoded><![CDATA[<p>Dealing with delimited lists (Usually separated by a comma) in SQL is a problem easily handled by a simple function and a Tally Table.  (Tally tables are also often referred to as Numbers tables or spt_values tables.  If you still don&#8217;t know what that is, please see this <a href="http://www.sqlservercentral.com/articles/T-SQL/62867/" target="_blank"><span style="text-decoration: underline;">excellent article</span></a> on Tally tables written by my friend and SSC heavyweight <a href="http://www.sqlservercentral.com/Authors/Articles/Jeff_Moden/80567/" target="_blank">Jeff Moden</a>.)  This particular implementation is somewhat specific in nature but can give you an alternative to Dynamic SQL when you want to pass in a list as a parameter and do an IN in a Stored Procedure. The following function will take your delimiter and string and parse it into a table so you can do your IN.  (I&#8217;m leaving my standard header on the function in this case because there are some good notes in there.)</p>
<div class="codecolorer-container sql mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">/*<br />
=============================================================================================<br />
CREATE DATE:     02/27/2010<br />
LAST MODIFIED:    02/27/2010<br />
CREATED BY:        SETH PHELABAUM<br />
PURPOSE:        Splits a string based on a passed in delimiter and returns a table.<br />
ISSUES:            Strings with extra 's will break this function, handle that on the end that calls it.<br />
Notes:            To make it a simpler function, I removed the peice that trimmed spaces around commas.  Do<br />
this before or after calling it.<br />
Revision History:<br />
Date     By        Change Made<br />
-------- ---      -------------------------------------<br />
=============================================================================================<br />
GRANT SELECT ON TVF_TallySplit TO [Somebody]<br />
SELECT * FROM TVF_TallySplit(',','Orange,Apple,Banana,Pear,Watermelon,Grape')<br />
SELECT * FROM TVF_TallySplit('*','Orange*Apple*Banana*Pear*Watermelon*Grape')<br />
DROP FUNCTION TVF_TallySplit<br />
*/</span><br />
<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> TVF_TallySplit<span style="color: #66cc66;">&#40;</span><br />
@Delim            <span style="color: #993333; font-weight: bold;">CHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span>            <span style="color: #808080; font-style: italic;">-- List Delimiter</span><br />
@String            <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8000</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">RETURNS</span> <span style="color: #993333; font-weight: bold;">TABLE</span><br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
<br />
<span style="color: #993333; font-weight: bold;">RETURN</span><span style="color: #66cc66;">&#40;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">SUBSTRING</span><span style="color: #66cc66;">&#40;</span>@Delim <span style="color: #66cc66;">+</span> @String <span style="color: #66cc66;">+</span> @Delim<span style="color: #66cc66;">,</span>N<span style="color: #66cc66;">+</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span>CHARINDEX<span style="color: #66cc66;">&#40;</span>@Delim<span style="color: #66cc66;">,</span>@Delim <span style="color: #66cc66;">+</span> @String <span style="color: #66cc66;">+</span> @Delim<span style="color: #66cc66;">,</span>N<span style="color: #66cc66;">+</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">-</span>N<span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> ListValue<br />
<span style="color: #993333; font-weight: bold;">FROM</span> Tally<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> N &amp;lt; LEN<span style="color: #66cc66;">&#40;</span>@Delim <span style="color: #66cc66;">+</span> @String <span style="color: #66cc66;">+</span> @Delim<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">AND</span> <span style="color: #993333; font-weight: bold;">SUBSTRING</span><span style="color: #66cc66;">&#40;</span>@Delim <span style="color: #66cc66;">+</span> @String <span style="color: #66cc66;">+</span> @Delim<span style="color: #66cc66;">,</span>N<span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> @Delim <span style="color: #66cc66;">&#41;</span></div></div>
<p><strong><span style="text-decoration: underline;">What to do with this</span></strong><br />
Let&#8217;s say you have a table containing names of your favorite fruits.  (In case you were wondering&#8230; No, these aren&#8217;t my favorite fruits; they were just ones that immediately came to mind when writing this.  I don&#8217;t even like half of these.)</p>
<div class="codecolorer-container sql mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> Fruits<span style="color: #66cc66;">&#40;</span><br />
Name        <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">25</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<br />
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> Fruits<span style="color: #66cc66;">&#40;</span>Name<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'Apple'</span> <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'Banana'</span> <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'Grapefruit'</span> <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'Kiwi'</span> <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span> <span style="color: #993333; font-weight: bold;">SELECT</span><span style="color: #ff0000;">'Tomatoe'</span> <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'Grape'</span> <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'Orange'</span> <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'DragonFruit'</span> <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'Strawberry'</span></div></div>
<p>You then ask someone else what their favorite fruits are and want to see what fruits you have in common.  You might think you could just write a query for that like this:</p>
<div class="codecolorer-container sql mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">DECLARE</span> @YFFruits <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span><br />
<br />
<span style="color: #993333; font-weight: bold;">SET</span> @YFFruits <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Orange,Apple,Banana,Pear,Watermelon,Grape'</span><br />
<span style="color: #808080; font-style: italic;">-- OR SET @YFFruits = '''Orange'',''Apple'',''Banana'',''Pear'',''Watermelon'',''Grape'''</span><br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> Fruits <span style="color: #993333; font-weight: bold;">WHERE</span> Name <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #66cc66;">&#40;</span>@YFFruits<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #808080; font-style: italic;">-- OR SELECT * FROM Fruits where Name IN (@YFFruits)</span></div></div>
<p>However, this won&#8217;t work because in all these cases SQL is looking for a single fruit named : &#8216;Orange,Apple,Banana,Pear,Watermelon,Grape&#8217; not any fruit in what is really a list.</p>
<p>A common solution for this is to use Dynamic SQL which would make your query this:</p>
<div class="codecolorer-container sql mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">DECLARE</span> @YFFruits <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SET</span> @YFFruits <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">''</span><span style="color: #ff0000;">'Orange'</span><span style="color: #ff0000;">','</span><span style="color: #ff0000;">'Apple'</span><span style="color: #ff0000;">','</span><span style="color: #ff0000;">'Banana'</span><span style="color: #ff0000;">','</span><span style="color: #ff0000;">'Pear'</span><span style="color: #ff0000;">','</span><span style="color: #ff0000;">'Watermelon'</span><span style="color: #ff0000;">','</span><span style="color: #ff0000;">'Grape'</span><span style="color: #ff0000;">''</span><br />
<span style="color: #993333; font-weight: bold;">EXEC</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'SELECT * FROM Fruits WHERE Name IN ('</span> <span style="color: #66cc66;">+</span> @YFFruits <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">')'</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>This works and will properly match up your fruits.</p>
<p>The above function allows you to accomplish your goal without Dynamic SQL with a query that looks like this:</p>
<div class="codecolorer-container sql mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">DECLARE</span> @YFFruits <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SET</span> @YFFruits <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Orange,Apple,Banana,Pear,Watermelon,Grape'</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> Fruits <span style="color: #993333; font-weight: bold;">WHERE</span> Name <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> Util<span style="color: #66cc66;">.</span>dbo<span style="color: #66cc66;">.</span>TVF_TallySplit<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">','</span><span style="color: #66cc66;">,</span>@YFFruits<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>You can also simply join to the table instead of using the IN keyword which gives you more flexibility in your query writing.</p>
<div class="codecolorer-container sql mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">DECLARE</span> @YFFruits <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SET</span> @YFFruits <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Orange,Apple,Banana,Pear,Watermelon,Grape'</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> Fruits<span style="color: #66cc66;">.*</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> Fruits<br />
<span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> Util<span style="color: #66cc66;">.</span>dbo<span style="color: #66cc66;">.</span>TVF_TallySplit<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">','</span><span style="color: #66cc66;">,</span>@YFFruits<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> T <span style="color: #993333; font-weight: bold;">ON</span> Fruits<span style="color: #66cc66;">.</span>Name <span style="color: #66cc66;">=</span> T<span style="color: #66cc66;">.</span>ListValue</div></div>
<p>Note that because I wanted to keep the function somewhat simple, it does not handle extra spaces around the commas.  Single quotes within the string will also break it which limits its usage somewhat.  If this is a concern for your implementation, you either need to replace the single quotes on both sides or use a different method.  Despite the fact that the example above uses a list of strings, in real life situations I use this mainly for lists of uniqueidentifiers or numbers where single quotes/spaces are never an issue.</p>
]]></content:encoded>
			<wfw:commentRss>http://phelabaum.com/archive/2010/02/tally-table-delimited-list-to-table/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Data Type Precedence and Implicit Conversions</title>
		<link>http://phelabaum.com/archive/2010/02/data-type-precedence-and-implicit-conversions/</link>
		<comments>http://phelabaum.com/archive/2010/02/data-type-precedence-and-implicit-conversions/#comments</comments>
		<pubDate>Sun, 21 Feb 2010 17:49:18 +0000</pubDate>
		<dc:creator>Seth Phelabaum</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://phelabaum.com/archive/2010/02/data-type-precedence-and-implicit-conversions/</guid>
		<description><![CDATA[In my last post, I noted that one of the biggest differences between ISNULL and COALESCE was the fact that ISNULL attempted to convert the second parameter to the data type of the first parameter where as COALESCE converted according to the Data Type Precedence table.  A reader requested that I go into more detail [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://www.sqlservercentral.com/blogs/never_say_never/archive/2010/02/11/isnull_2D00_vs_2D00_coalesce.aspx" target="_blank">last post</a>, I noted that one of the biggest differences between ISNULL and COALESCE was the fact that ISNULL attempted to convert the second parameter to the data type of the first parameter where as COALESCE converted according to the Data Type Precedence table.  A reader requested that I go into more detail on what that means.  At first I wasn&#8217;t sure what I was going to explain, there didn&#8217;t seem like a lot to talk about once I linked the <a href="http://msdn.microsoft.com/en-us/library/ms190309.aspx" target="_blank">BOL article on Data Type Precedence</a>(which I meant to do in my initial post but apparently never did).  After thinking about it for a while, I realized that one thing that isn&#8217;t really pointed out in the BOL page is what these implicit conversions can do to performance if you aren&#8217;t paying attention.   This post got a bit long.</p>
<p>This isn&#8217;t a topic that I&#8217;m real familiar with, so I had to do some research / tests of my own to write this.  I&#8217;ve had to fix the varchar/nvarchar one several times, but others I can&#8217;t truly explain.  Why does a char-&gt;varchar comparison not trigger an implicit conversion?  Honestly, I&#8217;m not sure.  My guess would be that the optimizer is simply smart enough to not do it, but as I said, that&#8217;s just a guess.  While attempting to find the answer to this online, I stumbled across a <a href="http://sqlblog.com/blogs/jonathan_kehayias/archive/2010/01/08/finding-implicit-column-conversions-in-the-plan-cache.aspx" target="_blank"><span style="text-decoration: underline;">brilliant script</span></a> written by Jonathan Kehayias that focuses on finding implicit conversions in the plan cache.</p>
<p>The examples below focus on non-numeric conversions.  I did a good amount of testing on different numeric conversions, and although I&#8217;ve read that SQL 2000 had specific issues, I was not able to easily duplicate this with the numeric types in any compatibility level with my 2K8 installation (so I left those examples out).  If anyone has any good examples of this behavior with numeric data types, I’d be happy to add them.</p>
<p><strong><span style="text-decoration: underline;">Test Setup</span></strong>: (Note that because I am dropping/creating a real table and user defined types, you should be careful which database you execute this against.  I would suggest creating a new one and executing it there)</p>
<div class="codecolorer-container sql mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">--- Drop and Re-create User Defined Type</span><br />
<span style="color: #993333; font-weight: bold;">IF</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: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> sys<span style="color: #66cc66;">.</span>types <span style="color: #993333; font-weight: bold;">WHERE</span> name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'UDVC'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TYPE</span> UDVC<br />
<span style="color: #993333; font-weight: bold;">IF</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: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> sys<span style="color: #66cc66;">.</span>types <span style="color: #993333; font-weight: bold;">WHERE</span> name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'UDNVC'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TYPE</span> UDNVC<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TYPE</span> UDVC <span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TYPE</span> UDNVC <span style="color: #993333; font-weight: bold;">FROM</span> nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span><br />
<br />
<span style="color: #808080; font-style: italic;">--- Drop and Re-Create Test Table</span><br />
<span style="color: #993333; font-weight: bold;">IF</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: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> sys<span style="color: #66cc66;">.</span>objects <span style="color: #993333; font-weight: bold;">WHERE</span> name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'DTP'</span> <span style="color: #993333; font-weight: bold;">AND</span> <span style="color: #993333; font-weight: bold;">TYPE</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'U'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TABLE</span> DTP<br />
<span style="color: #993333; font-weight: bold;">SELECT</span>    TOP <span style="color: #cc66cc;">100000</span> <span style="color: #808080; font-style: italic;">-- If you use too few rows, the performance differences aren't as apparent.</span><br />
<span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>NEWID<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> nvarchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> NVCCol<span style="color: #66cc66;">,</span><br />
<span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>NEWID<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> VCCol<span style="color: #66cc66;">,</span><br />
<span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>NEWID<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">CHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> CCol<span style="color: #66cc66;">,</span><br />
<span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>NEWID<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> sql_variant<span style="color: #66cc66;">&#41;</span> SQLVCVarCol<br />
<span style="color: #993333; font-weight: bold;">INTO</span> DTP<br />
<span style="color: #993333; font-weight: bold;">FROM</span> Util<span style="color: #66cc66;">..</span>Tally <span style="color: #808080; font-style: italic;">--I keep a Tally table in a Utility Database named Util.</span><br />
<span style="color: #808080; font-style: italic;">--Either change to the location of your tally table or use the other FROM statement below.</span><br />
<span style="color: #808080; font-style: italic;">--FROM master..spt_values A CROSS JOIN master..spt_values B CROSS JOIN master..spt_values C</span><br />
<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">INDEX</span> IX_NVCCol <span style="color: #993333; font-weight: bold;">ON</span> DTP<span style="color: #66cc66;">&#40;</span>NVCCol<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">INDEX</span> IX_VCCol <span style="color: #993333; font-weight: bold;">ON</span> DTP<span style="color: #66cc66;">&#40;</span>VCCol<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">INDEX</span> IX_CCol <span style="color: #993333; font-weight: bold;">ON</span> DTP<span style="color: #66cc66;">&#40;</span>CCol<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">INDEX</span> IX_SQLVCVarCol <span style="color: #993333; font-weight: bold;">ON</span> DTP<span style="color: #66cc66;">&#40;</span>SQLVCVarCol<span style="color: #66cc66;">&#41;</span><span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p><strong><span style="text-decoration: underline;">Tests:</span></strong></p>
<div class="codecolorer-container sql mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">-- Test 1: Compare varchar and nvarchar against varchar column.</span><br />
<span style="color: #808080; font-style: italic;">-- These will show a massive difference because it must convert the varchar column VCCol to nvarchar to compare them</span><br />
<span style="color: #808080; font-style: italic;">-- In the execution Plan, you will see that the first uses an index scan and the second uses an index seek.</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> VCCol <span style="color: #993333; font-weight: bold;">FROM</span> DTP <span style="color: #993333; font-weight: bold;">WHERE</span> VCCol <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'A'</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> VCCol <span style="color: #993333; font-weight: bold;">FROM</span> DTP <span style="color: #993333; font-weight: bold;">WHERE</span> VCCol <span style="color: #66cc66;">=</span>  N<span style="color: #ff0000;">'A'</span></div></div>
<p><a href="http://phelabaum.com/wp-content/uploads/2010/02/Test1.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Test1" src="http://phelabaum.com/wp-content/uploads/2010/02/Test1_thumb.jpg" border="0" alt="Test1" width="308" height="176" /></a></p>
<div class="codecolorer-container sql mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">-- Test 2: Compare varchar and nvarchar against nvarchar column.</span><br />
<span style="color: #808080; font-style: italic;">-- These also show a very small difference because nvarchar is higher in the precedence list and so it only has to convert 'A'</span><br />
<span style="color: #808080; font-style: italic;">-- to an nvarchar rather than the entire column.</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> NVCCol <span style="color: #993333; font-weight: bold;">FROM</span> DTP <span style="color: #993333; font-weight: bold;">WHERE</span> NVCCol <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'A'</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> NVCCol <span style="color: #993333; font-weight: bold;">FROM</span> DTP <span style="color: #993333; font-weight: bold;">WHERE</span> NVCCol <span style="color: #66cc66;">=</span>  N<span style="color: #ff0000;">'A'</span></div></div>
<p><a href="http://phelabaum.com/wp-content/uploads/2010/02/Test2.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Test2" src="http://phelabaum.com/wp-content/uploads/2010/02/Test2_thumb.jpg" border="0" alt="Test2" width="312" height="173" /></a></p>
<div class="codecolorer-container sql mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">-- Test 3:  Compare varchar and nvarchar against SQLVariant column.</span><br />
<span style="color: #808080; font-style: italic;">-- Although this may seem very similar to Test 1, you won't see much of a difference here.  This is because sql_variant is not</span><br />
<span style="color: #808080; font-style: italic;">-- is near the top of the DTP table (higher than varchar/nvarchar), so you only have to convert the single value.</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> SQLVCVarCol <span style="color: #993333; font-weight: bold;">FROM</span> DTP <span style="color: #993333; font-weight: bold;">WHERE</span> SQLVCVarCol <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'A'</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> SQLVCVarCol <span style="color: #993333; font-weight: bold;">FROM</span> DTP <span style="color: #993333; font-weight: bold;">WHERE</span> SQLVCVarCol <span style="color: #66cc66;">=</span> N<span style="color: #ff0000;">'A'</span></div></div>
<p><a href="http://phelabaum.com/wp-content/uploads/2010/02/Test3.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Test3" src="http://phelabaum.com/wp-content/uploads/2010/02/Test3_thumb.jpg" border="0" alt="Test3" width="312" height="155" /></a></p>
<p>[/cc_sql]&#8211; Test 4:  Compare varchar and sql_variant against varchar column.<br />
&#8211; Here you get the massive difference you would expect because you must convert the entire column to a sql_variant.<br />
&#8211; This one actually has a different plan all together and not just an index scan.<br />
DECLARE @a sql_variant<br />
SET @a = &#8216;A&#8217;<br />
SELECT VCCol FROM DTP WHERE VCCol = &#8216;A&#8217;<br />
SELECT VCCol FROM DTP WHERE VCCol =  @a[/cc_sql]</p>
<p><a href="http://phelabaum.com/wp-content/uploads/2010/02/SQLVariantPar.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="SQLVariantPar" src="http://phelabaum.com/wp-content/uploads/2010/02/SQLVariantPar_thumb.jpg" border="0" alt="SQLVariantPar" width="317" height="141" /></a></p>
<div class="codecolorer-container sql mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">-- Test 5: Compare User Defined Types (with bases of varchar and nvarchar) against varchar column.</span><br />
<span style="color: #808080; font-style: italic;">-- This behaves exactly as Test 1 did.  Conversions seem to be handled like they would be if the data types were the base types.</span><br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @a UDVC<br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @b UDNVC<br />
<span style="color: #993333; font-weight: bold;">SET</span> @a <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'A'</span><br />
<span style="color: #993333; font-weight: bold;">SET</span> @b <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'B'</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> VCCol <span style="color: #993333; font-weight: bold;">FROM</span> DTP <span style="color: #993333; font-weight: bold;">WHERE</span> VCCol <span style="color: #66cc66;">=</span>@a<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> VCCol <span style="color: #993333; font-weight: bold;">FROM</span> DTP <span style="color: #993333; font-weight: bold;">WHERE</span> VCCol <span style="color: #66cc66;">=</span> @b</div></div>
<p><a href="http://phelabaum.com/wp-content/uploads/2010/02/Test5.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Test5" src="http://phelabaum.com/wp-content/uploads/2010/02/Test5_thumb.jpg" border="0" alt="Test5" width="318" height="177" /></a></p>
<div class="codecolorer-container sql mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">--Test 6:  Compare char and varchar against char column</span><br />
<span style="color: #808080; font-style: italic;">-- There is not any performance loss here.  It seems like there should be, but at least in my tests there is not.</span><br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @a <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @b <span style="color: #993333; font-weight: bold;">CHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">60</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SET</span> @a <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'A'</span><br />
<span style="color: #993333; font-weight: bold;">SET</span> @b <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'A'</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> CCol <span style="color: #993333; font-weight: bold;">FROM</span> DTP <span style="color: #993333; font-weight: bold;">WHERE</span> CCol <span style="color: #66cc66;">=</span> @b<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> CCol <span style="color: #993333; font-weight: bold;">FROM</span> DTP <span style="color: #993333; font-weight: bold;">WHERE</span> CCol <span style="color: #66cc66;">=</span> @a</div></div>
<p><a href="http://phelabaum.com/wp-content/uploads/2010/02/Test6.jpg"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Test6" src="http://phelabaum.com/wp-content/uploads/2010/02/Test6_thumb.jpg" border="0" alt="Test6" width="318" height="162" /></a></p>
<p>The varchar/nvarchar conversions can be especially painful / tricky when the code is being passed in from elsewhere.  One of the places that I&#8217;ve seen issues with this is LINQ to SQL.  It is entirely possible that it was just our setup that was mismatched (I wasn&#8217;t involved in that end of it), but I figured I&#8217;d throw it out there anyways.</p>
]]></content:encoded>
			<wfw:commentRss>http://phelabaum.com/archive/2010/02/data-type-precedence-and-implicit-conversions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ISNULL() VS. COALESCE()</title>
		<link>http://phelabaum.com/archive/2010/02/isnull-vs-coalesce-2/</link>
		<comments>http://phelabaum.com/archive/2010/02/isnull-vs-coalesce-2/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 13:47:50 +0000</pubDate>
		<dc:creator>Seth Phelabaum</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Functions]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://phelabaum.com/archive/2010/02/isnull-vs-coalesce-2/</guid>
		<description><![CDATA[There are a lot of arguments about which of these to use.&#160; In my opinion, they both have their place.&#160; Here are a few facts about the two functions. ISNULL can accept only two arguments where as COALESCE can accept any number of input parameters ISNULL is not ANSI standard and is proprietary to TSQL. [...]]]></description>
			<content:encoded><![CDATA[<p>There are a lot of arguments about which of these to use.&#160; In my opinion, they both have their place.&#160; Here are a few facts about the two functions. </p>
<ul>
<li>ISNULL can accept only two arguments where as COALESCE can accept any number of input parameters </li>
<li>ISNULL is not ANSI standard and is proprietary to TSQL. </li>
<li>With ISNULL, the datatype of the second argument is converted to equal the data type of the first argument where as COALESCE converts according to data type precedence.&#160; </li>
<li>ISNULL is generally accepted to be faster than COALESCE. </li>
<li>COALESCE is a much cooler word and will usually earn you either an impressed glance or a blank confused stare when slipped into casual conversation.&#160; </li>
</ul>
<p>Here are a few examples that demonstrate some of the functional differences between the two conversion methods. </p>
<pre class="csharpcode"><span class="kwrd">declare</span> @a <span class="kwrd">varchar</span>(5)
<span class="kwrd">select</span> ISNULL(@a, <span class="str">'ISNULL Test 1'</span>)        <span class="rem">-- Result: ISNUL</span>
<span class="kwrd">SELECT</span> <span class="kwrd">COALESCE</span>(@a, <span class="str">'COALESCE Test 1'</span>)    <span class="rem">-- Result: COALESCE Test 1</span>

<span class="kwrd">declare</span> @b tinyint
<span class="kwrd">SELECT</span> ISNULL(@b, 99999)                <span class="rem">-- Result: **Error**</span>
<span class="kwrd">SELECT</span> <span class="kwrd">COALESCE</span>(@b, 99999)                <span class="rem">-- Result: 9999</span>

<span class="kwrd">declare</span> @c <span class="kwrd">char</span>(5)
<span class="kwrd">SELECT</span> ISNULL(@c, <span class="str">'a'</span>) + <span class="str">'B'</span>            <span class="rem">-- Result: a    B</span>
<span class="kwrd">SELECT</span> <span class="kwrd">COALESCE</span>(@c, <span class="str">'a'</span>) + <span class="str">'B'</span>            -- <span class="kwrd">Result</span>: aB</pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
]]></content:encoded>
			<wfw:commentRss>http://phelabaum.com/archive/2010/02/isnull-vs-coalesce-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>IN and NOT IN</title>
		<link>http://phelabaum.com/archive/2010/01/in-and-not-in/</link>
		<comments>http://phelabaum.com/archive/2010/01/in-and-not-in/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 14:03:35 +0000</pubDate>
		<dc:creator>Seth Phelabaum</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[Common Problems]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://phelabaum.com/archive/2010/01/in-and-not-in/</guid>
		<description><![CDATA[One of the most common mistakes made in T-SQL is thinking that these behave identically.&#160; I&#8217;ve personally opened up a forum topic on it because I didn&#8217;t know what the difference was.&#160; This post will join a small army of other places on the net devoted to correcting this misunderstanding. They aren&#8217;t completely dissimilar; they [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most common mistakes made in T-SQL is thinking that these behave identically.&#160; I&#8217;ve personally opened up a forum topic on it because I didn&#8217;t know what the difference was.&#160; This post will join a small army of other places on the net devoted to correcting this misunderstanding. </p>
<p>They aren&#8217;t completely dissimilar; they behave exactly as you would expect them to&#8230; with the exception of NULL&#8217;s.&#160; Because nothing EQUALS NULL (Dependent upon settings, see below) the difference in the internal logic matters.&#160; Gail Shaw initially explained this to me when I asked the question on the forums and I wanted to use her explanation here, but I can’t seem to find it; so here&#8217;s my own version of an explanation: </p>
<p>When you use IN, you&#8217;re really saying &quot;WHERE myvalue = &#8216;A&#8217; OR myvalue = &#8216;B&#8217; OR myvalue = NULL&quot;    <br />Your NULLS won&#8217;t cause the entire statement to fail because it&#8217;s only an OR. </p>
<p>When you use NOT IN you&#8217;re really saying &#8216;WHERE myvalue &lt;&gt; &#8216;A&#8217; AND myvalue &lt;&gt; &#8216;B&#8217; AND myvalue &lt;&gt; NULL “    <br />This is where the problem arises.&#160; Since a NULL in SQL is an unknown value, you can&#8217;t test = or &lt;&gt; on it and you get no results.&#160; Without the NULL, you&#8217;d be fine. </p>
<p>Here&#8217;s a simple example to demonstrate. </p>
<pre class="csharpcode"><span class="kwrd">DECLARE</span> @T <span class="kwrd">TABLE</span>(
Val <span class="kwrd">varchar</span>(5)) 

<span class="kwrd">DECLARE</span> @T2 <span class="kwrd">TABLE</span>(
Val <span class="kwrd">varchar</span>(5)) 

INSERT <span class="kwrd">INTO</span> @T(Val)
<span class="kwrd">SELECT</span> <span class="str">'A'</span> <span class="kwrd">UNION</span> <span class="kwrd">ALL</span> <span class="kwrd">SELECT</span> <span class="str">'B'</span> <span class="kwrd">UNION</span> <span class="kwrd">ALL</span> <span class="kwrd">SELECT</span> <span class="str">'C'</span> <span class="kwrd">UNION</span> <span class="kwrd">ALL</span>
<span class="kwrd">SELECT</span> <span class="str">'D'</span> <span class="kwrd">UNION</span> <span class="kwrd">ALL</span> <span class="kwrd">SELECT</span> <span class="str">'E'</span> <span class="kwrd">UNION</span> <span class="kwrd">ALL</span> <span class="kwrd">SELECT</span> <span class="str">'F'</span> 

INSERT <span class="kwrd">INTO</span> @T2(Val)
<span class="kwrd">SELECT</span> <span class="str">'A'</span> <span class="kwrd">UNION</span> <span class="kwrd">ALL</span> <span class="kwrd">SELECT</span> <span class="str">'B'</span> <span class="kwrd">UNION</span> <span class="kwrd">ALL</span> <span class="kwrd">SELECT</span> <span class="str">'C'</span> <span class="kwrd">UNION</span> <span class="kwrd">ALL</span>
<span class="kwrd">SELECT</span> <span class="kwrd">NULL</span> 

<span class="kwrd">SET</span> ANSI_NULLS <span class="kwrd">ON</span>
<span class="kwrd">SELECT</span> * <span class="kwrd">FROM</span> @T <span class="kwrd">WHERE</span> Val <span class="kwrd">IN</span> (<span class="kwrd">SELECT</span> * <span class="kwrd">FROM</span> @T2)
<span class="kwrd">SELECT</span> * <span class="kwrd">FROM</span> @T <span class="kwrd">WHERE</span> Val <span class="kwrd">NOT</span> <span class="kwrd">IN</span> (<span class="kwrd">SELECT</span> * <span class="kwrd">FROM</span> @T2)
<span class="kwrd">SELECT</span> * <span class="kwrd">FROM</span> @T <span class="kwrd">WHERE</span> Val <span class="kwrd">NOT</span> <span class="kwrd">IN</span> (<span class="kwrd">SELECT</span> * <span class="kwrd">FROM</span> @T2 <span class="kwrd">WHERE</span> Val <span class="kwrd">IS</span> <span class="kwrd">NOT</span> <span class="kwrd">NULL</span>) </pre>
<p>This issue is further complicated by the ANSI_NULLS setting.&#160; While I believe most people have this turned ON, the fact that it is an option introduces another variable into the mix.&#160; NOT IN will not fail in the same way if you have ANSI_NULLS set to OFF.&#160; (Try the above example again after changing ON to OFF)</p>
]]></content:encoded>
			<wfw:commentRss>http://phelabaum.com/archive/2010/01/in-and-not-in/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

