<?xml version="1.0" encoding="iso-8859-1"?><!-- generator="b2evolution/4.0.3" -->
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>Data Management - Author(s): Axel Achten (axel8s)</title>
		<link>http://blogs.lessthandot.com/index.php/DataMgmt/</link>
		<atom:link rel="self" type="application/rss+xml" href="http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2" />
		<description></description>
		<language>en-GB</language>
		<docs>http://blogs.law.harvard.edu/tech/rss</docs>
		<admin:generatorAgent rdf:resource="http://b2evolution.net/?v=4.0.3"/>
		<ttl>60</ttl>
				<item>
			<title>Why there should be SQL Content at Techdays.be</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/why-there-should-be-sql</link>
			<pubDate>Tue, 29 Jan 2013 07:32:00 +0000</pubDate>			<dc:creator>Axel Achten (axel8s)</dc:creator>
			<category domain="alt">Database Programming</category>
<category domain="main">Database Administration</category>			<guid isPermaLink="false">2056@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;On 5, 6 and 7 March 2013 Microsoft Belux is organizing &lt;a href=&quot;http://www.microsoft.com/belux/techdays/2013/home.aspx&quot;&gt;Techdays 2013&lt;/a&gt;, 3 days of sessions for IT Pro&#039;s and developers. But when checking the calendar there are no sessions about SQL Server.&lt;br /&gt;
The reason is that the &lt;a href=&quot;http://sqlug.be/&quot;&gt;Belgian SQL Server User Group&lt;/a&gt; organizes yearly a 2 day event about SQL Server the &lt;a href=&quot;http://www.sqlserverdays.be&quot;&gt;SQL Server Days&lt;/a&gt; and this is where the SQL Server enthusiasts should get their knowledge. &lt;/p&gt;&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://it-tna.com/&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/NoSQL.jpg?mtime=1359451359&quot; width=&quot;300&quot; height=&quot;276&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;But I disagree, as a full-time SQL Server consultant, I can&#039;t justify going to an event where there is no SQL content at all. I know there are some other interesting sessions about Windows Server, SCOM, etc. but they aren&#039;t the core of my job so I can&#039;t register for this event and thus I will miss the opportunity to:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;learn about new features in Windows server, SCOM, PowerShell...;&lt;/li&gt;
  &lt;li&gt;network with current and former co-workers outside the office;&lt;/li&gt;
  &lt;li&gt;network with other SQL Server DBA&#039;s and developers;&lt;/li&gt;
  &lt;li&gt;enthusiasm young developers and system administrators to learn more about SQL Server;&lt;/li&gt;
  &lt;li&gt;visit the partner booths, to learn what&#039;s new in the hardware world;&lt;/li&gt;
  &lt;li&gt;bring home a bag of goodies to make the kids happy and win some awesome prices.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And how about young, all-round system administrators and developers who can&#039;t justify a visit to Techdays AND SQL Server Days because their main focus is not on SQL Server, wouldn&#039;t they like to discover the wonderful world of SQL Server and learn:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;how they can tame the never ending growth of the T-log;&lt;/li&gt;
  &lt;li&gt;the developers don&#039;t need SSMS anymore&lt;/li&gt;
  &lt;li&gt;SQL Server supports Server Core&lt;/li&gt;
  &lt;li&gt;cursors are bad for SQL Server&lt;/li&gt;
  &lt;li&gt;there such things as graphical execution plans&lt;/li&gt;
  &lt;li&gt;...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Anyways that&#039;s my vision on the subject, feel free to agree or disagree in the comments, discussing this with me at the Techdays will probably be a no show.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/why-there-should-be-sql&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>On 5, 6 and 7 March 2013 Microsoft Belux is organizing <a href="http://www.microsoft.com/belux/techdays/2013/home.aspx">Techdays 2013</a>, 3 days of sessions for IT Pro's and developers. But when checking the calendar there are no sessions about SQL Server.<br />
The reason is that the <a href="http://sqlug.be/">Belgian SQL Server User Group</a> organizes yearly a 2 day event about SQL Server the <a href="http://www.sqlserverdays.be">SQL Server Days</a> and this is where the SQL Server enthusiasts should get their knowledge. </p><div class="image_block"><a href="http://it-tna.com/"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/NoSQL.jpg?mtime=1359451359" width="300" height="276" /></a></div><p>But I disagree, as a full-time SQL Server consultant, I can't justify going to an event where there is no SQL content at all. I know there are some other interesting sessions about Windows Server, SCOM, etc. but they aren't the core of my job so I can't register for this event and thus I will miss the opportunity to:</p>
<ul>
  <li>learn about new features in Windows server, SCOM, PowerShell...;</li>
  <li>network with current and former co-workers outside the office;</li>
  <li>network with other SQL Server DBA's and developers;</li>
  <li>enthusiasm young developers and system administrators to learn more about SQL Server;</li>
  <li>visit the partner booths, to learn what's new in the hardware world;</li>
  <li>bring home a bag of goodies to make the kids happy and win some awesome prices.</li>
</ul>
<p>And how about young, all-round system administrators and developers who can't justify a visit to Techdays AND SQL Server Days because their main focus is not on SQL Server, wouldn't they like to discover the wonderful world of SQL Server and learn:</p>
<ul>
  <li>how they can tame the never ending growth of the T-log;</li>
  <li>the developers don't need SSMS anymore</li>
  <li>SQL Server supports Server Core</li>
  <li>cursors are bad for SQL Server</li>
  <li>there such things as graphical execution plans</li>
  <li>...</li>
</ul>

<p>Anyways that's my vision on the subject, feel free to agree or disagree in the comments, discussing this with me at the Techdays will probably be a no show.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/why-there-should-be-sql">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/why-there-should-be-sql#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=2056</wfw:commentRss>
		</item>
				<item>
			<title>WHERE not to use FUNCTIONS</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/where-not-to-use-functions</link>
			<pubDate>Thu, 24 Jan 2013 12:02:00 +0000</pubDate>			<dc:creator>Axel Achten (axel8s)</dc:creator>
			<category domain="main">Database Programming</category>
<category domain="alt">Microsoft SQL Server Admin</category>			<guid isPermaLink="false">2045@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;Functions can be very powerful, but used in the wrong place in a query they can show some unexpected behavior.&lt;br /&gt;
In this post I will be using the AdventureWorks2008R2 database and I will query the Sales.SalesOrderHeader to get all the 2006 OrderDates. A query that doesn&amp;#8217;t make much sense but will return some interesting results.&lt;br /&gt;
Take the following query:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb54510&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;USE&lt;/span&gt; AdventureWorks2008R2;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; OrderDate &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderHeader&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; OrderDate BETWEEN &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20060101&#039;&lt;/span&gt; AND &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20061231&#039;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb92669&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;If we take a look at the execution plan:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/WhereFunction1.png?mtime=1359036029&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/WhereFunction1.png?mtime=1359036029&quot; width=&quot;767&quot; height=&quot;153&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;We see that there is a complete scan of the Clustered Index which makes sense since there is no index on the OrderDate column.&lt;br /&gt;
As the Missing Index Hint suggests, create an index on the OrderDate column:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb85302&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INDEX&lt;/span&gt; IX_SalesOrderHeader_OrderDate&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderHeader&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;OrderDate&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb10346&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Executing our first query again in another form:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb37571&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; OrderDate &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderHeader&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; OrderDate &amp;gt;= &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20060101&#039;&lt;/span&gt; AND OrderDate &amp;lt;=&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20061231&#039;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb96946&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Results in the following Execution Plan:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/WhereFunction2.png?mtime=1359036029&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/WhereFunction2.png?mtime=1359036029&quot; width=&quot;771&quot; height=&quot;143&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;You see that the query is internally translated to &lt;code class=&quot;codespan&quot;&gt;SELECT [OrderDate] FROM [Sales].[SalesOrderHeader] WHERE [OrderDate]&amp;lt;=@1 AND [OrderDate]&amp;lt;=@2&amp;gt;&lt;/code&gt; just like the first query using the BETWEEN keyword. The big change is in the execution plan. Since we created an index on the OrderDate column, SQL Server is now using an Index Seek on our index to fetch the results.&lt;/p&gt;

&lt;p&gt;Since we are looking for all the dates in 2006, you might want to consider using the YEAR function. The YEAR function returns only the YEAR part of a date(time) value:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb23185&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;YEAR&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20060127 02:15:59&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb12699&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Results in &lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/WhereFunction3.png?mtime=1359036029&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/WhereFunction3.png?mtime=1359036029&quot; width=&quot;159&quot; height=&quot;66&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;So the following query should make sense and is more readable then the former 2:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb31067&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; OrderDate &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderHeader&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;YEAR&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;OrderDate&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; = &lt;span style=&quot;color: #000;&quot;&gt;2006&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb25069&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;And looking at the result set it makes sense:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/WhereFunction4.png?mtime=1359036029&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/WhereFunction4.png?mtime=1359036029&quot; width=&quot;980&quot; height=&quot;300&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;But when we look at the execution plan:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/WhereFunction5.png?mtime=1359036029&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/WhereFunction5.png?mtime=1359036029&quot; width=&quot;622&quot; height=&quot;149&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;We see that our index isn&amp;#8217;t seeked anymore but gets a complete scan. So instead of searching in some 4K rows, SQL Server is scanning more than 30K of rows. This is because SQL Server is applying the function to all of the rows in our Sales.SalesOrderHeader table before it&amp;#8217;s compared to our desired value.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;br /&gt;
Be careful when using functions in the WHERE clause of a query. It&amp;#8217;s possible that the function will be applied to all the rows before the filter is applied. Resulting in scans, non used indexes, more I/O, memory consumption and a poor performing query.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/where-not-to-use-functions&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>Functions can be very powerful, but used in the wrong place in a query they can show some unexpected behavior.<br />
In this post I will be using the AdventureWorks2008R2 database and I will query the Sales.SalesOrderHeader to get all the 2006 OrderDates. A query that doesn&#8217;t make much sense but will return some interesting results.<br />
Take the following query:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb49734'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb49734','cb29173'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb49734" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">USE</span> AdventureWorks2008R2;</li><li style="" class="li2"><span style="color: #0000FF;">GO</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">SELECT</span> OrderDate <span style="color: #0000FF;">FROM</span> Sales.<span style="color: #202020;">SalesOrderHeader</span></li><li style="" class="li1"><span style="color: #0000FF;">WHERE</span> OrderDate BETWEEN <span style="color: #FF0000;">'20060101'</span> AND <span style="color: #FF0000;">'20061231'</span>;</li><li style="" class="li2"><span style="color: #0000FF;">GO</span></li></ol></div><div id="cb29173" style="display: none; color: red;"></div></div></div>
<p>If we take a look at the execution plan:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/WhereFunction1.png?mtime=1359036029"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/WhereFunction1.png?mtime=1359036029" width="767" height="153" /></a></div>
<p>We see that there is a complete scan of the Clustered Index which makes sense since there is no index on the OrderDate column.<br />
As the Missing Index Hint suggests, create an index on the OrderDate column:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb53347'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb53347','cb34503'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb53347" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">INDEX</span> IX_SalesOrderHeader_OrderDate</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">ON</span> Sales.<span style="color: #202020;">SalesOrderHeader</span><span style="color: #808080;">&#40;</span>OrderDate<span style="color: #808080;">&#41;</span>;</li><li style="" class="li1"><span style="color: #0000FF;">GO</span></li></ol></div><div id="cb34503" style="display: none; color: red;"></div></div></div>
<p>Executing our first query again in another form:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb16503'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb16503','cb53607'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb16503" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> OrderDate <span style="color: #0000FF;">FROM</span> Sales.<span style="color: #202020;">SalesOrderHeader</span></li><li style="" class="li2"><span style="color: #0000FF;">WHERE</span> OrderDate &gt;= <span style="color: #FF0000;">'20060101'</span> AND OrderDate &lt;=<span style="color: #FF0000;">'20061231'</span>;</li><li style="" class="li1"><span style="color: #0000FF;">GO</span></li></ol></div><div id="cb53607" style="display: none; color: red;"></div></div></div>
<p>Results in the following Execution Plan:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/WhereFunction2.png?mtime=1359036029"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/WhereFunction2.png?mtime=1359036029" width="771" height="143" /></a></div>
<p>You see that the query is internally translated to <code class="codespan">SELECT [OrderDate] FROM [Sales].[SalesOrderHeader] WHERE [OrderDate]&lt;=@1 AND [OrderDate]&lt;=@2&gt;</code> just like the first query using the BETWEEN keyword. The big change is in the execution plan. Since we created an index on the OrderDate column, SQL Server is now using an Index Seek on our index to fetch the results.</p>

<p>Since we are looking for all the dates in 2006, you might want to consider using the YEAR function. The YEAR function returns only the YEAR part of a date(time) value:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb91156'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb91156','cb85700'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb91156" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">YEAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'20060127 02:15:59'</span><span style="color: #808080;">&#41;</span></li></ol></div><div id="cb85700" style="display: none; color: red;"></div></div></div>
<p>Results in </p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/WhereFunction3.png?mtime=1359036029"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/WhereFunction3.png?mtime=1359036029" width="159" height="66" /></a></div>
<p>So the following query should make sense and is more readable then the former 2:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb12986'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb12986','cb85584'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb12986" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> OrderDate <span style="color: #0000FF;">FROM</span> Sales.<span style="color: #202020;">SalesOrderHeader</span></li><li style="" class="li2"><span style="color: #0000FF;">WHERE</span> <span style="color: #FF00FF;">YEAR</span><span style="color: #808080;">&#40;</span>OrderDate<span style="color: #808080;">&#41;</span> = <span style="color: #000;">2006</span>;</li><li style="" class="li1"><span style="color: #0000FF;">GO</span></li></ol></div><div id="cb85584" style="display: none; color: red;"></div></div></div>
<p>And looking at the result set it makes sense:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/WhereFunction4.png?mtime=1359036029"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/WhereFunction4.png?mtime=1359036029" width="980" height="300" /></a></div>
<p>But when we look at the execution plan:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/WhereFunction5.png?mtime=1359036029"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/WhereFunction5.png?mtime=1359036029" width="622" height="149" /></a></div>
<p>We see that our index isn&#8217;t seeked anymore but gets a complete scan. So instead of searching in some 4K rows, SQL Server is scanning more than 30K of rows. This is because SQL Server is applying the function to all of the rows in our Sales.SalesOrderHeader table before it&#8217;s compared to our desired value.</p>

<p><b>Conclusion</b><br />
Be careful when using functions in the WHERE clause of a query. It&#8217;s possible that the function will be applied to all the rows before the filter is applied. Resulting in scans, non used indexes, more I/O, memory consumption and a poor performing query.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/where-not-to-use-functions">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/where-not-to-use-functions#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=2045</wfw:commentRss>
		</item>
				<item>
			<title>Why avoiding multiple code blocks in a Stored Procedure</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/why-avoiding-multiple-code-blocks</link>
			<pubDate>Mon, 21 Jan 2013 11:45:00 +0000</pubDate>			<dc:creator>Axel Achten (axel8s)</dc:creator>
			<category domain="main">Database Programming</category>
<category domain="alt">Microsoft SQL Server Admin</category>
<category domain="alt">Microsoft SQL Server</category>			<guid isPermaLink="false">2038@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;It was a little remark from Bob Beauchemin (&lt;a href=&quot;http://www.sqlskills.com/blogs/bobb&quot;&gt;B&lt;/a&gt;|&lt;a href=&quot;https://twitter.com/bobbeauch&quot;&gt;T&lt;/a&gt;) during the &lt;a href=&quot;http://www.sqlserverdays.be&quot;&gt;Belgian SQL Server Days&lt;/a&gt; that started me writing this post.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Showing cached Execution Plans&lt;/b&gt;&lt;br /&gt;
In this post I&amp;#8217;m going to use information from the following Dynamic Management Views and Functions to show some information about the cached Execution Plans of the queries used in this post:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;sys.dm_exec_cached_plans shows types, usage, size&amp;#8230; of the Execution Plans;&lt;/li&gt;

  &lt;li&gt;sys.dm_exec_sql_text shows the actual code of the query;&lt;/li&gt;

  &lt;li&gt;sys.dm_exec_query_plan is used to get the XML Execution Plan itself.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In fact I use following query to get the results:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb18819&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; cp.&lt;span style=&quot;color: #202020;&quot;&gt;objtype&lt;/span&gt;, cp.&lt;span style=&quot;color: #202020;&quot;&gt;cacheobjtype&lt;/span&gt;, cp.&lt;span style=&quot;color: #202020;&quot;&gt;usecounts&lt;/span&gt;, cp.&lt;span style=&quot;color: #202020;&quot;&gt;size_in_bytes&lt;/span&gt;, st.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, qp.&lt;span style=&quot;color: #202020;&quot;&gt;query_plan&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; &lt;span style=&quot;color: #00AF00;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: #00AF00;&quot;&gt;dm_exec_cached_plans&lt;/span&gt; cp&lt;br /&gt;&amp;nbsp; &amp;nbsp; CROSS APPLY &lt;span style=&quot;color: #00AF00;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: #00AF00;&quot;&gt;dm_exec_sql_text&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;cp.&lt;span style=&quot;color: #202020;&quot;&gt;plan_handle&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; st&lt;br /&gt;&amp;nbsp; &amp;nbsp; CROSS APPLY &lt;span style=&quot;color: #00AF00;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: #00AF00;&quot;&gt;dm_exec_query_plan&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;cp.&lt;span style=&quot;color: #202020;&quot;&gt;plan_handle&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;qp;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb47099&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The result set of this query looks like this:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB1.JPG?mtime=1358775444&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB1.JPG?mtime=1358775444&quot; width=&quot;933&quot; height=&quot;427&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;The result set can contain thousands of rows, depending on the uptime of the server, the number of queries and their corresponding execution plans, the available buffer memory&amp;#8230;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;IF using 2 code blocks&lt;/b&gt;&lt;br /&gt;
So let&amp;#8217;s create a simple Stored Procedure that executes against the AdventureWorks2008R2 database:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb44747&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;USE&lt;/span&gt; AdventureWorks2008R2;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;PROCEDURE&lt;/span&gt; TwoPlans&lt;br /&gt;&amp;nbsp; &amp;nbsp; @IfParameter &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;NOCOUNT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;IF&lt;/span&gt; @IfParameter = &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; a.&lt;span style=&quot;color: #202020;&quot;&gt;City&lt;/span&gt;, &lt;span style=&quot;color: #FF00FF;&quot;&gt;COUNT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;bea.&lt;span style=&quot;color: #202020;&quot;&gt;AddressID&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; EmployeeCount&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Person.&lt;span style=&quot;color: #202020;&quot;&gt;BusinessEntityAddress&lt;/span&gt; bea &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INNER&lt;/span&gt; JOIN Person.&lt;span style=&quot;color: #0000FF;&quot;&gt;Address&lt;/span&gt; a&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; bea.&lt;span style=&quot;color: #202020;&quot;&gt;AddressID&lt;/span&gt; = a.&lt;span style=&quot;color: #202020;&quot;&gt;AddressID&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;GROUP&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; a.&lt;span style=&quot;color: #202020;&quot;&gt;City&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;ORDER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; a.&lt;span style=&quot;color: #202020;&quot;&gt;City&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;ELSE&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;DATEPART&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;yyyy,OrderDate&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Year&#039;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ,&lt;span style=&quot;color: #FF00FF;&quot;&gt;SUM&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;TotalDue&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Total Order Amount&#039;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderHeader&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;GROUP&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;DATEPART&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;yyyy,OrderDate&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;ORDER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;DATEPART&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;yyyy,OrderDate&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb5257&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The results of the query aren&amp;#8217;t important what is important is to see what happens in the Procedure Cache. To be able to see what&amp;#8217;s happening we are going to empty the procedure cache:&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;MT_red&quot;&gt;WARNING: executing the following code deletes all cached plans from the Procedure Cache. All Execution Plans need to be recompiled. This can result in a slow or unresponsive server.&lt;br /&gt;
DON&amp;#8217;T EXECUTE THE FOLLOWING CODE ON A PRODUCTION SERVER!!!&lt;br /&gt;
&lt;/span&gt;&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb54302&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DBCC&lt;/span&gt; FREEPROCCACHE;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb46042&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;When you execute our first query again:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb54266&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; cp.&lt;span style=&quot;color: #202020;&quot;&gt;objtype&lt;/span&gt;, cp.&lt;span style=&quot;color: #202020;&quot;&gt;cacheobjtype&lt;/span&gt;, cp.&lt;span style=&quot;color: #202020;&quot;&gt;usecounts&lt;/span&gt;, cp.&lt;span style=&quot;color: #202020;&quot;&gt;size_in_bytes&lt;/span&gt;, st.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, qp.&lt;span style=&quot;color: #202020;&quot;&gt;query_plan&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; &lt;span style=&quot;color: #00AF00;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: #00AF00;&quot;&gt;dm_exec_cached_plans&lt;/span&gt; cp&lt;br /&gt;&amp;nbsp; &amp;nbsp; CROSS APPLY &lt;span style=&quot;color: #00AF00;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: #00AF00;&quot;&gt;dm_exec_sql_text&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;cp.&lt;span style=&quot;color: #202020;&quot;&gt;plan_handle&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; st&lt;br /&gt;&amp;nbsp; &amp;nbsp; CROSS APPLY &lt;span style=&quot;color: #00AF00;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: #00AF00;&quot;&gt;dm_exec_query_plan&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;cp.&lt;span style=&quot;color: #202020;&quot;&gt;plan_handle&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;qp;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb97208&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;You can see in the result set that only the Execution Plan from the above query is stored in the Procedure Cache.&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB2.JPG?mtime=1358775444&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB2.JPG?mtime=1358775444&quot; width=&quot;871&quot; height=&quot;67&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;Now execute the stored procedure:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb13024&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;EXEC&lt;/span&gt; TwoPlans &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb41368&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Execute the query against the Procedure Cache again:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb43591&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; cp.&lt;span style=&quot;color: #202020;&quot;&gt;objtype&lt;/span&gt;, cp.&lt;span style=&quot;color: #202020;&quot;&gt;cacheobjtype&lt;/span&gt;, cp.&lt;span style=&quot;color: #202020;&quot;&gt;usecounts&lt;/span&gt;, cp.&lt;span style=&quot;color: #202020;&quot;&gt;size_in_bytes&lt;/span&gt;, st.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, qp.&lt;span style=&quot;color: #202020;&quot;&gt;query_plan&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; &lt;span style=&quot;color: #00AF00;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: #00AF00;&quot;&gt;dm_exec_cached_plans&lt;/span&gt; cp&lt;br /&gt;&amp;nbsp; &amp;nbsp; CROSS APPLY &lt;span style=&quot;color: #00AF00;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: #00AF00;&quot;&gt;dm_exec_sql_text&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;cp.&lt;span style=&quot;color: #202020;&quot;&gt;plan_handle&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; st&lt;br /&gt;&amp;nbsp; &amp;nbsp; CROSS APPLY &lt;span style=&quot;color: #00AF00;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: #00AF00;&quot;&gt;dm_exec_query_plan&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;cp.&lt;span style=&quot;color: #202020;&quot;&gt;plan_handle&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;qp;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb37432&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;The result set shows us the execution of the above query with a usecount of 2 and the Execution Plan of our Stored Procedure:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB3.JPG?mtime=1358775444&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB3.JPG?mtime=1358775444&quot; width=&quot;871&quot; height=&quot;85&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;Now click the XML link in the query_plan column. A new tab will open in SSMS showing you the XML Execution Plan:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB4.JPG?mtime=1358775444&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB4.JPG?mtime=1358775444&quot; width=&quot;1434&quot; height=&quot;865&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;You can now read the XML plan and you will find both the statements in the query plan:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;xml&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;xml&quot; id=&quot;cb73990&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;ShowPlanXML xmlns=&amp;quot;http://schemas.microsoft.com/sqlserver/2004/07/showplan&amp;quot; Version=&amp;quot;1.1&amp;quot; Build=&amp;quot;10.50.2500.0&amp;quot;&amp;gt;&lt;br /&gt;&amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;BatchSequence&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;Batch&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;Statements&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;StmtSimple&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;StatementText&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;CREATE procedure TwoPlans&amp;amp;#xD;&amp;amp;#xA;&amp;amp;#x9;@IfParameter int&amp;amp;#xD;&amp;amp;#xA;as&amp;amp;#xD;&amp;amp;#xA;&amp;amp;#x9;SET NOCOUNT ON;&amp;amp;#xD;&amp;amp;#xA;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;StatementId&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;1&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;StatementCompId&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;3&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;StatementType&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;SET ON/OFF&amp;quot;&lt;/span&gt; &lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;StmtCond&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;StatementText&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&amp;amp;#x9;If @IfParameter = 1&amp;amp;#xD;&amp;amp;#xA;&amp;amp;#x9;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;StatementId&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;2&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;StatementCompId&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;4&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;StatementType&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;COND&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;Condition&lt;/span&gt; &lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;Then&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;Statements&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;StmtSimple&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;StatementText&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&amp;amp;#x9;SELECT a.City, COUNT(bea.AddressID) EmployeeCount&amp;amp;#xD;&amp;amp;#xA;&amp;amp;#x9;&amp;amp;#x9;FROM Person.BusinessEntityAddress bea &amp;amp;#xD;&amp;amp;#xA;&amp;amp;#x9;&amp;amp;#x9;&amp;amp;#x9;INNER JOIN Person.Address a&amp;amp;#xD;&amp;amp;#xA;&amp;amp;#x9;&amp;amp;#x9;&amp;amp;#x9;&amp;amp;#x9;ON bea.AddressID = a.AddressID&amp;amp;#xD;&amp;amp;#xA;&amp;amp;#x9;&amp;amp;#x9;GROUP BY a.City&amp;amp;#xD;&amp;amp;#xA;&amp;amp;#x9;&amp;amp;#x9;ORDER BY a.City&amp;amp;#xD;&amp;amp;#xA;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;StatementId&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;3&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;StatementCompId&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;5&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;StatementType&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;SELECT&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;StatementSubTreeCost&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;0.604708&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;StatementEstRows&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;574.696&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;StatementOptmLevel&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;FULL&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;QueryHash&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;0x03E92D79FC617C86&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;QueryPlanHash&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;0xED13B89036D1A5E6&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;StatementOptmEarlyAbortReason&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;GoodEnoughPlanFound&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;StatementSetOptions&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;QUOTED_IDENTIFIER&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;true&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;ARITHABORT&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;true&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;CONCAT_NULL_YIELDS_NULL&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;true&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;ANSI_NULLS&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;true&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;ANSI_PADDING&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;true&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;ANSI_WARNINGS&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;true&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;NUMERIC_ROUNDABORT&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;false&amp;quot;&lt;/span&gt; &lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;QueryPlan&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;CachedPlanSize&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;32&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;CompileTime&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;7&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;CompileCPU&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;7&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;CompileMemory&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;344&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&amp;#8230;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/StmtSimple&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/Statements&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/Then&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;Else&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;Statements&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;StmtSimple&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;StatementText&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;&amp;amp;#x9;Else&amp;amp;#xD;&amp;amp;#xA;&amp;amp;#x9;&amp;amp;#x9;SELECT DATEPART(yyyy,OrderDate) AS N&#039;Year&#039;&amp;amp;#xD;&amp;amp;#xA;&amp;amp;#x9;&amp;amp;#x9;&amp;amp;#x9;,SUM(TotalDue) AS N&#039;Total Order Amount&#039;&amp;amp;#xD;&amp;amp;#xA;&amp;amp;#x9;&amp;amp;#x9;FROM Sales.SalesOrderHeader&amp;amp;#xD;&amp;amp;#xA;&amp;amp;#x9;&amp;amp;#x9;GROUP BY DATEPART(yyyy,OrderDate)&amp;amp;#xD;&amp;amp;#xA;&amp;amp;#x9;&amp;amp;#x9;ORDER BY DATEPART(yyyy,OrderDate);&amp;amp;#xD;&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;StatementId&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;4&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;StatementCompId&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;8&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;StatementType&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;SELECT&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;StatementSubTreeCost&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;0.780189&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;StatementEstRows&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;4&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;StatementOptmLevel&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;FULL&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;QueryHash&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;0xA73814A2D4649412&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;QueryPlanHash&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;0x7A5BDE1102728DAA&amp;quot;&lt;/span&gt; &lt;span style=&quot;color: #000066;&quot;&gt;StatementOptmEarlyAbortReason&lt;/span&gt;=&lt;span style=&quot;color: #ff0000;&quot;&gt;&amp;quot;GoodEnoughPlanFound&amp;quot;&lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&amp;#8230;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/Statements&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/Else&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/StmtCond&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/Statements&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/Batch&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/BatchSequence&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #009900;&quot;&gt;&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;lt;/ShowPlanXML&lt;span style=&quot;font-weight: bold; color: black;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb50167&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;An easier trick to see the Graphical Execution Plans is to open &lt;a href=&quot;http://www.sqlsentry.com/plan-explorer/sql-server-query-view.asp&quot;&gt;SQL Sentry Plan Explorer&lt;/a&gt; and copy the XML into the Plan XML tab:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB5.JPG?mtime=1358775444&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB5.JPG?mtime=1358775444&quot; width=&quot;652&quot; height=&quot;298&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;After doing this SQL Sentry Plan Explorer will give you all the details about the Execution Plan and you&amp;#8217;ll see in the Plan Diagram that SQL Server created an Execution Plan for both the queries altough only the first one was used:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB6.JPG?mtime=1358775445&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB6.JPG?mtime=1358775445&quot; width=&quot;1347&quot; height=&quot;765&quot; /&gt;&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;&lt;b&gt;IF calling 2 Stored Procedures&lt;/b&gt;&lt;br /&gt;
Now let&amp;#8217;s create 2 Stored Procedures that each will execute 1 part of the code from the previous query:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb95049&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;PROCEDURE&lt;/span&gt; EmpCntCity&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;NOCOUNT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; a.&lt;span style=&quot;color: #202020;&quot;&gt;City&lt;/span&gt;, &lt;span style=&quot;color: #FF00FF;&quot;&gt;COUNT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;bea.&lt;span style=&quot;color: #202020;&quot;&gt;AddressID&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; EmployeeCount&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Person.&lt;span style=&quot;color: #202020;&quot;&gt;BusinessEntityAddress&lt;/span&gt; bea &lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INNER&lt;/span&gt; JOIN Person.&lt;span style=&quot;color: #0000FF;&quot;&gt;Address&lt;/span&gt; a&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; bea.&lt;span style=&quot;color: #202020;&quot;&gt;AddressID&lt;/span&gt; = a.&lt;span style=&quot;color: #202020;&quot;&gt;AddressID&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;GROUP&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; a.&lt;span style=&quot;color: #202020;&quot;&gt;City&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;ORDER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; a.&lt;span style=&quot;color: #202020;&quot;&gt;City&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;PROCEDURE&lt;/span&gt; OrderAmountYear&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;NOCOUNT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;DATEPART&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;yyyy,OrderDate&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Year&#039;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ,&lt;span style=&quot;color: #FF00FF;&quot;&gt;SUM&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;TotalDue&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; N&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Total Order Amount&#039;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;SalesOrderHeader&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;GROUP&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;DATEPART&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;yyyy,OrderDate&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;ORDER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;DATEPART&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;yyyy,OrderDate&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb29456&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Next we create a Stored Procedure that will contain the IF&amp;#8230;ELSE logic and based on the input parameter will execute one of the above Stored Procedures:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb50014&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;PROCEDURE&lt;/span&gt; TwoProcs&lt;br /&gt;&amp;nbsp; &amp;nbsp; @IfParameter &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;NOCOUNT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt;;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;IF&lt;/span&gt; @IfParameter = &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;EXEC&lt;/span&gt; EmpCntCity&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;ELSE&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;EXEC&lt;/span&gt; OrderAmountYear;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb95452&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Now we can call our Stored Procedure and execute 1 of the Stored Procedures:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb71386&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;EXEC&lt;/span&gt; TwoProcs &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb53115&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Let&amp;#8217;s again query the Procedure Cache:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb48937&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; cp.&lt;span style=&quot;color: #202020;&quot;&gt;objtype&lt;/span&gt;, cp.&lt;span style=&quot;color: #202020;&quot;&gt;cacheobjtype&lt;/span&gt;, cp.&lt;span style=&quot;color: #202020;&quot;&gt;usecounts&lt;/span&gt;, cp.&lt;span style=&quot;color: #202020;&quot;&gt;size_in_bytes&lt;/span&gt;, st.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;text&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, qp.&lt;span style=&quot;color: #202020;&quot;&gt;query_plan&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; &lt;span style=&quot;color: #00AF00;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: #00AF00;&quot;&gt;dm_exec_cached_plans&lt;/span&gt; cp&lt;br /&gt;&amp;nbsp; &amp;nbsp; CROSS APPLY &lt;span style=&quot;color: #00AF00;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: #00AF00;&quot;&gt;dm_exec_sql_text&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;cp.&lt;span style=&quot;color: #202020;&quot;&gt;plan_handle&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; st&lt;br /&gt;&amp;nbsp; &amp;nbsp; CROSS APPLY &lt;span style=&quot;color: #00AF00;&quot;&gt;sys&lt;/span&gt;.&lt;span style=&quot;color: #00AF00;&quot;&gt;dm_exec_query_plan&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;cp.&lt;span style=&quot;color: #202020;&quot;&gt;plan_handle&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;qp;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb24696&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;And have a look at the result:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB7.JPG?mtime=1358775445&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB7.JPG?mtime=1358775445&quot; width=&quot;919&quot; height=&quot;125&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;Now we see that executing the second Stored Procedure also created 2 Executions Plans: 1 for the Stored Procedure &amp;#8220;TwoProcs&amp;#8221; and one for the Stored Procedure &amp;#8220;EmpCntCity&amp;#8221; that was effectively executed. But we see that there was no Execution Plan created for the Stored Procedure &amp;#8220;OrderAmountYear&amp;#8221;. And this Execution Plan was more complex than the Execution Plan of our &amp;#8220;TwoProcs&amp;#8221; Stored Procedure. We can also see that the sum of the sizes of the 2 Execution Plans (65536 + 16384 = 81920) is still smaller than the Execution Plan of the &amp;#8220;TwoPlans&amp;#8221; Stored Procedure (98304).&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;br /&gt;
Avoid Stored Procedures that contain complete code blocks encapsulated in IF...ELSE or CASE blocks. It will result in SQL Server creating Execution Plans for all possibilities, consuming more Buffer Cache (memory) and in the end slow down the execution of the code.&lt;br /&gt;
As a bonus, troubleshooting the individual Stored Procedures will be much easier and there is a bigger chance that you can reuse the Stored Procedures.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/why-avoiding-multiple-code-blocks&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>It was a little remark from Bob Beauchemin (<a href="http://www.sqlskills.com/blogs/bobb">B</a>|<a href="https://twitter.com/bobbeauch">T</a>) during the <a href="http://www.sqlserverdays.be">Belgian SQL Server Days</a> that started me writing this post.</p>

<p><b>Showing cached Execution Plans</b><br />
In this post I&#8217;m going to use information from the following Dynamic Management Views and Functions to show some information about the cached Execution Plans of the queries used in this post:</p>
<ul>
  <li>sys.dm_exec_cached_plans shows types, usage, size&#8230; of the Execution Plans;</li>

  <li>sys.dm_exec_sql_text shows the actual code of the query;</li>

  <li>sys.dm_exec_query_plan is used to get the XML Execution Plan itself.</li>
</ul>

<p>In fact I use following query to get the results:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb94188'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb94188','cb45048'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb94188" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> cp.<span style="color: #202020;">objtype</span>, cp.<span style="color: #202020;">cacheobjtype</span>, cp.<span style="color: #202020;">usecounts</span>, cp.<span style="color: #202020;">size_in_bytes</span>, st.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">text</span><span style="color: #808080;">&#93;</span>, qp.<span style="color: #202020;">query_plan</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">FROM</span> <span style="color: #00AF00;">sys</span>.<span style="color: #00AF00;">dm_exec_cached_plans</span> cp</li><li style="" class="li1">&nbsp; &nbsp; CROSS APPLY <span style="color: #00AF00;">sys</span>.<span style="color: #00AF00;">dm_exec_sql_text</span><span style="color: #808080;">&#40;</span>cp.<span style="color: #202020;">plan_handle</span><span style="color: #808080;">&#41;</span> st</li><li style="" class="li2">&nbsp; &nbsp; CROSS APPLY <span style="color: #00AF00;">sys</span>.<span style="color: #00AF00;">dm_exec_query_plan</span> <span style="color: #808080;">&#40;</span>cp.<span style="color: #202020;">plan_handle</span><span style="color: #808080;">&#41;</span>qp;</li></ol></div><div id="cb45048" style="display: none; color: red;"></div></div></div>
<p>The result set of this query looks like this:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB1.JPG?mtime=1358775444"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB1.JPG?mtime=1358775444" width="933" height="427" /></a></div>
<p>The result set can contain thousands of rows, depending on the uptime of the server, the number of queries and their corresponding execution plans, the available buffer memory&#8230;</p>

<p><b>IF using 2 code blocks</b><br />
So let&#8217;s create a simple Stored Procedure that executes against the AdventureWorks2008R2 database:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb29908'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb29908','cb80065'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb29908" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">USE</span> AdventureWorks2008R2;</li><li style="" class="li2"><span style="color: #0000FF;">GO</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">PROCEDURE</span> TwoPlans</li><li style="" class="li1">&nbsp; &nbsp; @IfParameter <span style="color: #0000FF;">int</span></li><li style="" class="li2"><span style="color: #0000FF;">AS</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">NOCOUNT</span> <span style="color: #0000FF;">ON</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">IF</span> @IfParameter = <span style="color: #000;">1</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> a.<span style="color: #202020;">City</span>, <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span>bea.<span style="color: #202020;">AddressID</span><span style="color: #808080;">&#41;</span> EmployeeCount</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FROM</span> Person.<span style="color: #202020;">BusinessEntityAddress</span> bea </li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">INNER</span> JOIN Person.<span style="color: #0000FF;">Address</span> a</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ON</span> bea.<span style="color: #202020;">AddressID</span> = a.<span style="color: #202020;">AddressID</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> a.<span style="color: #202020;">City</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> a.<span style="color: #202020;">City</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">ELSE</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">DATEPART</span><span style="color: #808080;">&#40;</span>yyyy,OrderDate<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> N<span style="color: #FF0000;">'Year'</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ,<span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>TotalDue<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> N<span style="color: #FF0000;">'Total Order Amount'</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FROM</span> Sales.<span style="color: #202020;">SalesOrderHeader</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> <span style="color: #FF00FF;">DATEPART</span><span style="color: #808080;">&#40;</span>yyyy,OrderDate<span style="color: #808080;">&#41;</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> <span style="color: #FF00FF;">DATEPART</span><span style="color: #808080;">&#40;</span>yyyy,OrderDate<span style="color: #808080;">&#41;</span>;</li><li style="" class="li1"><span style="color: #0000FF;">GO</span></li></ol></div><div id="cb80065" style="display: none; color: red;"></div></div></div>
<p>The results of the query aren&#8217;t important what is important is to see what happens in the Procedure Cache. To be able to see what&#8217;s happening we are going to empty the procedure cache:</p>

<p><span class="MT_red">WARNING: executing the following code deletes all cached plans from the Procedure Cache. All Execution Plans need to be recompiled. This can result in a slow or unresponsive server.<br />
DON&#8217;T EXECUTE THE FOLLOWING CODE ON A PRODUCTION SERVER!!!<br />
</span></p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb79014'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb79014','cb66963'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb79014" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">DBCC</span> FREEPROCCACHE;</li><li style="" class="li2"><span style="color: #0000FF;">GO</span></li></ol></div><div id="cb66963" style="display: none; color: red;"></div></div></div>
<p>When you execute our first query again:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb35631'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb35631','cb83987'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb35631" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> cp.<span style="color: #202020;">objtype</span>, cp.<span style="color: #202020;">cacheobjtype</span>, cp.<span style="color: #202020;">usecounts</span>, cp.<span style="color: #202020;">size_in_bytes</span>, st.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">text</span><span style="color: #808080;">&#93;</span>, qp.<span style="color: #202020;">query_plan</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">FROM</span> <span style="color: #00AF00;">sys</span>.<span style="color: #00AF00;">dm_exec_cached_plans</span> cp</li><li style="" class="li1">&nbsp; &nbsp; CROSS APPLY <span style="color: #00AF00;">sys</span>.<span style="color: #00AF00;">dm_exec_sql_text</span><span style="color: #808080;">&#40;</span>cp.<span style="color: #202020;">plan_handle</span><span style="color: #808080;">&#41;</span> st</li><li style="" class="li2">&nbsp; &nbsp; CROSS APPLY <span style="color: #00AF00;">sys</span>.<span style="color: #00AF00;">dm_exec_query_plan</span> <span style="color: #808080;">&#40;</span>cp.<span style="color: #202020;">plan_handle</span><span style="color: #808080;">&#41;</span>qp;</li></ol></div><div id="cb83987" style="display: none; color: red;"></div></div></div>
<p>You can see in the result set that only the Execution Plan from the above query is stored in the Procedure Cache.</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB2.JPG?mtime=1358775444"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB2.JPG?mtime=1358775444" width="871" height="67" /></a></div>
<p>Now execute the stored procedure:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb64262'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb64262','cb6669'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb64262" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">EXEC</span> TwoPlans <span style="color: #000;">1</span>;</li><li style="" class="li2"><span style="color: #0000FF;">GO</span></li></ol></div><div id="cb6669" style="display: none; color: red;"></div></div></div>
<p>Execute the query against the Procedure Cache again:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb53303'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb53303','cb54718'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb53303" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> cp.<span style="color: #202020;">objtype</span>, cp.<span style="color: #202020;">cacheobjtype</span>, cp.<span style="color: #202020;">usecounts</span>, cp.<span style="color: #202020;">size_in_bytes</span>, st.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">text</span><span style="color: #808080;">&#93;</span>, qp.<span style="color: #202020;">query_plan</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">FROM</span> <span style="color: #00AF00;">sys</span>.<span style="color: #00AF00;">dm_exec_cached_plans</span> cp</li><li style="" class="li1">&nbsp; &nbsp; CROSS APPLY <span style="color: #00AF00;">sys</span>.<span style="color: #00AF00;">dm_exec_sql_text</span><span style="color: #808080;">&#40;</span>cp.<span style="color: #202020;">plan_handle</span><span style="color: #808080;">&#41;</span> st</li><li style="" class="li2">&nbsp; &nbsp; CROSS APPLY <span style="color: #00AF00;">sys</span>.<span style="color: #00AF00;">dm_exec_query_plan</span> <span style="color: #808080;">&#40;</span>cp.<span style="color: #202020;">plan_handle</span><span style="color: #808080;">&#41;</span>qp;</li></ol></div><div id="cb54718" style="display: none; color: red;"></div></div></div>
<p>The result set shows us the execution of the above query with a usecount of 2 and the Execution Plan of our Stored Procedure:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB3.JPG?mtime=1358775444"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB3.JPG?mtime=1358775444" width="871" height="85" /></a></div>
<p>Now click the XML link in the query_plan column. A new tab will open in SSMS showing you the XML Execution Plan:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB4.JPG?mtime=1358775444"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB4.JPG?mtime=1358775444" width="1434" height="865" /></a></div>
<p>You can now read the XML plan and you will find both the statements in the query plan:</p>
<div class="codebox"><div class="codeheader"><span>xml</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb45614'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb45614','cb9165'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="xml" id="cb45614" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1">ShowPlanXML xmlns=&quot;http://schemas.microsoft.com/sqlserver/2004/07/showplan&quot; Version=&quot;1.1&quot; Build=&quot;10.50.2500.0&quot;&gt;</li><li style="" class="li2">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;BatchSequence<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Batch<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Statements<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;StmtSimple</span> <span style="color: #000066;">StatementText</span>=<span style="color: #ff0000;">&quot;CREATE procedure TwoPlans&amp;#xD;&amp;#xA;&amp;#x9;@IfParameter int&amp;#xD;&amp;#xA;as&amp;#xD;&amp;#xA;&amp;#x9;SET NOCOUNT ON;&amp;#xD;&amp;#xA;&quot;</span> <span style="color: #000066;">StatementId</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">StatementCompId</span>=<span style="color: #ff0000;">&quot;3&quot;</span> <span style="color: #000066;">StatementType</span>=<span style="color: #ff0000;">&quot;SET ON/OFF&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;StmtCond</span> <span style="color: #000066;">StatementText</span>=<span style="color: #ff0000;">&quot;&amp;#x9;If @IfParameter = 1&amp;#xD;&amp;#xA;&amp;#x9;&quot;</span> <span style="color: #000066;">StatementId</span>=<span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000066;">StatementCompId</span>=<span style="color: #ff0000;">&quot;4&quot;</span> <span style="color: #000066;">StatementType</span>=<span style="color: #ff0000;">&quot;COND&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Condition</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Then<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Statements<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;StmtSimple</span> <span style="color: #000066;">StatementText</span>=<span style="color: #ff0000;">&quot;&amp;#x9;SELECT a.City, COUNT(bea.AddressID) EmployeeCount&amp;#xD;&amp;#xA;&amp;#x9;&amp;#x9;FROM Person.BusinessEntityAddress bea &amp;#xD;&amp;#xA;&amp;#x9;&amp;#x9;&amp;#x9;INNER JOIN Person.Address a&amp;#xD;&amp;#xA;&amp;#x9;&amp;#x9;&amp;#x9;&amp;#x9;ON bea.AddressID = a.AddressID&amp;#xD;&amp;#xA;&amp;#x9;&amp;#x9;GROUP BY a.City&amp;#xD;&amp;#xA;&amp;#x9;&amp;#x9;ORDER BY a.City&amp;#xD;&amp;#xA;&quot;</span> <span style="color: #000066;">StatementId</span>=<span style="color: #ff0000;">&quot;3&quot;</span> <span style="color: #000066;">StatementCompId</span>=<span style="color: #ff0000;">&quot;5&quot;</span> <span style="color: #000066;">StatementType</span>=<span style="color: #ff0000;">&quot;SELECT&quot;</span> <span style="color: #000066;">StatementSubTreeCost</span>=<span style="color: #ff0000;">&quot;0.604708&quot;</span> <span style="color: #000066;">StatementEstRows</span>=<span style="color: #ff0000;">&quot;574.696&quot;</span> <span style="color: #000066;">StatementOptmLevel</span>=<span style="color: #ff0000;">&quot;FULL&quot;</span> <span style="color: #000066;">QueryHash</span>=<span style="color: #ff0000;">&quot;0x03E92D79FC617C86&quot;</span> <span style="color: #000066;">QueryPlanHash</span>=<span style="color: #ff0000;">&quot;0xED13B89036D1A5E6&quot;</span> <span style="color: #000066;">StatementOptmEarlyAbortReason</span>=<span style="color: #ff0000;">&quot;GoodEnoughPlanFound&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;StatementSetOptions</span> <span style="color: #000066;">QUOTED_IDENTIFIER</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">ARITHABORT</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">CONCAT_NULL_YIELDS_NULL</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">ANSI_NULLS</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">ANSI_PADDING</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">ANSI_WARNINGS</span>=<span style="color: #ff0000;">&quot;true&quot;</span> <span style="color: #000066;">NUMERIC_ROUNDABORT</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="font-weight: bold; color: black;">/&gt;</span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;QueryPlan</span> <span style="color: #000066;">CachedPlanSize</span>=<span style="color: #ff0000;">&quot;32&quot;</span> <span style="color: #000066;">CompileTime</span>=<span style="color: #ff0000;">&quot;7&quot;</span> <span style="color: #000066;">CompileCPU</span>=<span style="color: #ff0000;">&quot;7&quot;</span> <span style="color: #000066;">CompileMemory</span>=<span style="color: #ff0000;">&quot;344&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>&#8230;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/StmtSimple<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Statements<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Then<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Else<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;Statements<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;StmtSimple</span> <span style="color: #000066;">StatementText</span>=<span style="color: #ff0000;">&quot;&amp;#x9;Else&amp;#xD;&amp;#xA;&amp;#x9;&amp;#x9;SELECT DATEPART(yyyy,OrderDate) AS N'Year'&amp;#xD;&amp;#xA;&amp;#x9;&amp;#x9;&amp;#x9;,SUM(TotalDue) AS N'Total Order Amount'&amp;#xD;&amp;#xA;&amp;#x9;&amp;#x9;FROM Sales.SalesOrderHeader&amp;#xD;&amp;#xA;&amp;#x9;&amp;#x9;GROUP BY DATEPART(yyyy,OrderDate)&amp;#xD;&amp;#xA;&amp;#x9;&amp;#x9;ORDER BY DATEPART(yyyy,OrderDate);&amp;#xD;&quot;</span> <span style="color: #000066;">StatementId</span>=<span style="color: #ff0000;">&quot;4&quot;</span> <span style="color: #000066;">StatementCompId</span>=<span style="color: #ff0000;">&quot;8&quot;</span> <span style="color: #000066;">StatementType</span>=<span style="color: #ff0000;">&quot;SELECT&quot;</span> <span style="color: #000066;">StatementSubTreeCost</span>=<span style="color: #ff0000;">&quot;0.780189&quot;</span> <span style="color: #000066;">StatementEstRows</span>=<span style="color: #ff0000;">&quot;4&quot;</span> <span style="color: #000066;">StatementOptmLevel</span>=<span style="color: #ff0000;">&quot;FULL&quot;</span> <span style="color: #000066;">QueryHash</span>=<span style="color: #ff0000;">&quot;0xA73814A2D4649412&quot;</span> <span style="color: #000066;">QueryPlanHash</span>=<span style="color: #ff0000;">&quot;0x7A5BDE1102728DAA&quot;</span> <span style="color: #000066;">StatementOptmEarlyAbortReason</span>=<span style="color: #ff0000;">&quot;GoodEnoughPlanFound&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>&#8230;</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Statements<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Else<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/StmtCond<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Statements<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/Batch<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li2">&nbsp; <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/BatchSequence<span style="font-weight: bold; color: black;">&gt;</span></span></span></li><li style="" class="li1"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/ShowPlanXML<span style="font-weight: bold; color: black;">&gt;</span></span></span></li></ol></div><div id="cb9165" style="display: none; color: red;"></div></div></div>
<p>An easier trick to see the Graphical Execution Plans is to open <a href="http://www.sqlsentry.com/plan-explorer/sql-server-query-view.asp">SQL Sentry Plan Explorer</a> and copy the XML into the Plan XML tab:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB5.JPG?mtime=1358775444"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB5.JPG?mtime=1358775444" width="652" height="298" /></a></div>
<p>After doing this SQL Sentry Plan Explorer will give you all the details about the Execution Plan and you&#8217;ll see in the Plan Diagram that SQL Server created an Execution Plan for both the queries altough only the first one was used:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB6.JPG?mtime=1358775445"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB6.JPG?mtime=1358775445" width="1347" height="765" /></a></div>

<p><b>IF calling 2 Stored Procedures</b><br />
Now let&#8217;s create 2 Stored Procedures that each will execute 1 part of the code from the previous query:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb9434'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb9434','cb17426'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb9434" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">PROCEDURE</span> EmpCntCity</li><li style="" class="li2"><span style="color: #0000FF;">AS</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">NOCOUNT</span> <span style="color: #0000FF;">ON</span>;</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> a.<span style="color: #202020;">City</span>, <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span>bea.<span style="color: #202020;">AddressID</span><span style="color: #808080;">&#41;</span> EmployeeCount</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">FROM</span> Person.<span style="color: #202020;">BusinessEntityAddress</span> bea </li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">INNER</span> JOIN Person.<span style="color: #0000FF;">Address</span> a</li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ON</span> bea.<span style="color: #202020;">AddressID</span> = a.<span style="color: #202020;">AddressID</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> a.<span style="color: #202020;">City</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> a.<span style="color: #202020;">City</span>;</li><li style="" class="li2"><span style="color: #0000FF;">GO</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">PROCEDURE</span> OrderAmountYear</li><li style="" class="li1"><span style="color: #0000FF;">AS</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">NOCOUNT</span> <span style="color: #0000FF;">ON</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">DATEPART</span><span style="color: #808080;">&#40;</span>yyyy,OrderDate<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> N<span style="color: #FF0000;">'Year'</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; ,<span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>TotalDue<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> N<span style="color: #FF0000;">'Total Order Amount'</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">FROM</span> Sales.<span style="color: #202020;">SalesOrderHeader</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> <span style="color: #FF00FF;">DATEPART</span><span style="color: #808080;">&#40;</span>yyyy,OrderDate<span style="color: #808080;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> <span style="color: #FF00FF;">DATEPART</span><span style="color: #808080;">&#40;</span>yyyy,OrderDate<span style="color: #808080;">&#41;</span>;</li><li style="" class="li2"><span style="color: #0000FF;">GO</span></li></ol></div><div id="cb17426" style="display: none; color: red;"></div></div></div>
<p>Next we create a Stored Procedure that will contain the IF&#8230;ELSE logic and based on the input parameter will execute one of the above Stored Procedures:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb42705'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb42705','cb87671'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb42705" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">PROCEDURE</span> TwoProcs</li><li style="" class="li2">&nbsp; &nbsp; @IfParameter <span style="color: #0000FF;">int</span></li><li style="" class="li1"><span style="color: #0000FF;">AS</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">NOCOUNT</span> <span style="color: #0000FF;">ON</span>;</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">IF</span> @IfParameter = <span style="color: #000;">1</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">EXEC</span> EmpCntCity</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">ELSE</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">EXEC</span> OrderAmountYear;</li><li style="" class="li1"><span style="color: #0000FF;">GO</span></li></ol></div><div id="cb87671" style="display: none; color: red;"></div></div></div>
<p>Now we can call our Stored Procedure and execute 1 of the Stored Procedures:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb83297'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb83297','cb70770'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb83297" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">EXEC</span> TwoProcs <span style="color: #000;">1</span>;</li><li style="" class="li2"><span style="color: #0000FF;">GO</span></li></ol></div><div id="cb70770" style="display: none; color: red;"></div></div></div>
<p>Let&#8217;s again query the Procedure Cache:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb33145'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb33145','cb64690'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb33145" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> cp.<span style="color: #202020;">objtype</span>, cp.<span style="color: #202020;">cacheobjtype</span>, cp.<span style="color: #202020;">usecounts</span>, cp.<span style="color: #202020;">size_in_bytes</span>, st.<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">text</span><span style="color: #808080;">&#93;</span>, qp.<span style="color: #202020;">query_plan</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">FROM</span> <span style="color: #00AF00;">sys</span>.<span style="color: #00AF00;">dm_exec_cached_plans</span> cp</li><li style="" class="li1">&nbsp; &nbsp; CROSS APPLY <span style="color: #00AF00;">sys</span>.<span style="color: #00AF00;">dm_exec_sql_text</span><span style="color: #808080;">&#40;</span>cp.<span style="color: #202020;">plan_handle</span><span style="color: #808080;">&#41;</span> st</li><li style="" class="li2">&nbsp; &nbsp; CROSS APPLY <span style="color: #00AF00;">sys</span>.<span style="color: #00AF00;">dm_exec_query_plan</span> <span style="color: #808080;">&#40;</span>cp.<span style="color: #202020;">plan_handle</span><span style="color: #808080;">&#41;</span>qp;</li></ol></div><div id="cb64690" style="display: none; color: red;"></div></div></div>
<p>And have a look at the result:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB7.JPG?mtime=1358775445"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/IFCB7.JPG?mtime=1358775445" width="919" height="125" /></a></div>
<p>Now we see that executing the second Stored Procedure also created 2 Executions Plans: 1 for the Stored Procedure &#8220;TwoProcs&#8221; and one for the Stored Procedure &#8220;EmpCntCity&#8221; that was effectively executed. But we see that there was no Execution Plan created for the Stored Procedure &#8220;OrderAmountYear&#8221;. And this Execution Plan was more complex than the Execution Plan of our &#8220;TwoProcs&#8221; Stored Procedure. We can also see that the sum of the sizes of the 2 Execution Plans (65536 + 16384 = 81920) is still smaller than the Execution Plan of the &#8220;TwoPlans&#8221; Stored Procedure (98304).</p>

<p><b>Conclusion</b><br />
Avoid Stored Procedures that contain complete code blocks encapsulated in IF...ELSE or CASE blocks. It will result in SQL Server creating Execution Plans for all possibilities, consuming more Buffer Cache (memory) and in the end slow down the execution of the code.<br />
As a bonus, troubleshooting the individual Stored Procedures will be much easier and there is a bigger chance that you can reuse the Stored Procedures.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/why-avoiding-multiple-code-blocks">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/why-avoiding-multiple-code-blocks#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=2038</wfw:commentRss>
		</item>
				<item>
			<title>Getting started with Execution Plans</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/getting-started-with-execution-plans</link>
			<pubDate>Thu, 10 Jan 2013 07:52:00 +0000</pubDate>			<dc:creator>Axel Achten (axel8s)</dc:creator>
			<category domain="alt">Database Programming</category>
<category domain="main">Microsoft SQL Server Admin</category>
<category domain="alt">Microsoft SQL Server</category>			<guid isPermaLink="false">2017@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;Another document for the customer&#039;s DBA checklist. This time we are going to look how we find and read an Execution Plan from a query.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Showing an Execution Plan&lt;/b&gt;&lt;br /&gt;
I will only focus on the graphical Execution Plans and in the examples the AdventureWorks2008R2 OLTP database is used which you can download here.&lt;br /&gt;
To see a Query Execution plan, you need to create a query:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb35922&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; * &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;vStoreWithContacts&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb11792&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;And then you click the &quot;Display Estimated Execution Plan&quot; or the &quot;Include Actual Execution Plan&quot; button in the SQL Editor Toolbar of SQL Server Management Studio.&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP1.JPG?mtime=1357810925&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP1.JPG?mtime=1357810925&quot; width=&quot;730&quot; height=&quot;129&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;The difference between the Estimated and the Actual plan is the execution of the query itself. When you hit the &quot;Display Estimated Execution Plan&quot; button, SQL Server Management Studio will immediately show you an Execution plan, however it&#039;s an estimated plan and the query itself is NOT executed. When you hit the &quot;Include Actual Execution Plan&quot; nothing will happen until you execute the query. After execution of the query a third tab will appear in SSMS showing the Execution Plan that was actually used:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP2.JPG?mtime=1357810925&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP2.JPG?mtime=1357810925&quot; width=&quot;1651&quot; height=&quot;711&quot; /&gt;&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;&lt;b&gt;Reading Execution Plans&lt;/b&gt;&lt;br /&gt;
When you want to read the above Execution Plan you have to read from right to left and from bottom to top. So SQL Server will start with&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP3.JPG?mtime=1357810926&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP3.JPG?mtime=1357810926&quot; width=&quot;232&quot; height=&quot;200&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;And end with&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP4.JPG?mtime=1357810926&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP4.JPG?mtime=1357810926&quot; width=&quot;91&quot; height=&quot;86&quot; /&gt;&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;Other information you directly can see is the most expensive operation and based on the thickness of the connecting arrows you can see the amount of data that&amp;#239;&amp;#191;&amp;#189;s streaming through them:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP5.JPG?mtime=1357810926&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP5.JPG?mtime=1357810926&quot; width=&quot;783&quot; height=&quot;226&quot; /&gt;&lt;/a&gt;&lt;/div&gt;


&lt;p&gt;&lt;b&gt;Finding more information&lt;/b&gt;&lt;br /&gt;
When you hover your mouse over the arrows or operations in an Execution Plan a pop-up will appear giving you more detailed information about the rows or operation:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP6.JPG?mtime=1357810926&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP6.JPG?mtime=1357810926&quot; width=&quot;225&quot; height=&quot;270&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;Needless to say that the Actual Number of Rows and some other information will not be available in an Estimated Execution Plan. In the example above we see a big difference between the Estimated and the Actual Number of Rows which might indicate that our statistics are out of date.&lt;br /&gt;
For even more information select an operation and open the Properties Window (F4):&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP7.JPG?mtime=1357810926&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP7.JPG?mtime=1357810926&quot; width=&quot;400&quot; height=&quot;225&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;Another option to get more out of your Execution Plans is to download &lt;a href=&quot;http://www.sqlsentry.com/plan-explorer/sql-server-query-view.asp&quot;&gt;SQL Sentry Plan Explorer&lt;/a&gt; more information is found at their site but here is a screenshot from the same Execution Plan in the Free Plan Explorer:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP8.JPG?mtime=1357810926&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP8.JPG?mtime=1357810926&quot; width=&quot;1267&quot; height=&quot;502&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;As you can see, the most expensive operation is highlighted in red and the tool also highlights the difference between the estimated and actual rows.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Missing index information&lt;/b&gt;&lt;br /&gt;
When executing the following query:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb68349&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; * &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;vStoreWithContacts&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; Firstname = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Alan&#039;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb38448&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;You&amp;#8217;ll see some green text in the top of the Execution Plan:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP9.JPG?mtime=1357810926&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP9.JPG?mtime=1357810926&quot; width=&quot;1081&quot; height=&quot;77&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;This information comes from the Dynamic Management View: sys.dm_db_missing_index_details. More information on Indexes is out of scope for this post but be careful with just implanting these Missing Indexes. They might improve the performance of this query but since they need to be maintained they can also slow down other operations on the database.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Comparing Execution Plans&lt;/b&gt;&lt;br /&gt;
Are you rewriting a query and wondering which one should perform better? You can easily compare the Execution Plans of two queries. Just write these queries in one query window:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb74066&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; * &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;vStoreWithContacts&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; Firstname NOT LIKE &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;A%&#039;&lt;/span&gt;;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;select&lt;/span&gt; * &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;vStoreWithContacts&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; FirstName IN &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; FirstName &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Sales.&lt;span style=&quot;color: #202020;&quot;&gt;vStoreWithContacts&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; FirstName NOT LIKE &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;A%&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb2886&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;When you execute the queries you&amp;#8217;ll see they both return the same 70 rows but when you look at the header of the Execution Plans you&amp;#8217;ll see that the first query costs less than the second one in this batch:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/ep10.JPG?mtime=1357810927&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/ep10.JPG?mtime=1357810927&quot; width=&quot;275&quot; height=&quot;150&quot; /&gt;&lt;/a&gt;&lt;/div&gt;

&lt;p&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;br /&gt;
Execution Plans gives us valuable information and a deep insight in how SQL Server executes the queries we write. Reading and understanding Execution Plans are essential for writing well performing queries.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/getting-started-with-execution-plans&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>Another document for the customer's DBA checklist. This time we are going to look how we find and read an Execution Plan from a query.</p>

<p><b>Showing an Execution Plan</b><br />
I will only focus on the graphical Execution Plans and in the examples the AdventureWorks2008R2 OLTP database is used which you can download here.<br />
To see a Query Execution plan, you need to create a query:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb40475'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb40475','cb58186'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb40475" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> * <span style="color: #0000FF;">FROM</span> Sales.<span style="color: #202020;">vStoreWithContacts</span></li></ol></div><div id="cb58186" style="display: none; color: red;"></div></div></div>
<p>And then you click the "Display Estimated Execution Plan" or the "Include Actual Execution Plan" button in the SQL Editor Toolbar of SQL Server Management Studio.</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP1.JPG?mtime=1357810925"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP1.JPG?mtime=1357810925" width="730" height="129" /></a></div>
<p>The difference between the Estimated and the Actual plan is the execution of the query itself. When you hit the "Display Estimated Execution Plan" button, SQL Server Management Studio will immediately show you an Execution plan, however it's an estimated plan and the query itself is NOT executed. When you hit the "Include Actual Execution Plan" nothing will happen until you execute the query. After execution of the query a third tab will appear in SSMS showing the Execution Plan that was actually used:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP2.JPG?mtime=1357810925"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP2.JPG?mtime=1357810925" width="1651" height="711" /></a></div>

<p><b>Reading Execution Plans</b><br />
When you want to read the above Execution Plan you have to read from right to left and from bottom to top. So SQL Server will start with</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP3.JPG?mtime=1357810926"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP3.JPG?mtime=1357810926" width="232" height="200" /></a></div>
<p>And end with</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP4.JPG?mtime=1357810926"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP4.JPG?mtime=1357810926" width="91" height="86" /></a></div>

<p>Other information you directly can see is the most expensive operation and based on the thickness of the connecting arrows you can see the amount of data that&#239;&#191;&#189;s streaming through them:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP5.JPG?mtime=1357810926"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP5.JPG?mtime=1357810926" width="783" height="226" /></a></div>


<p><b>Finding more information</b><br />
When you hover your mouse over the arrows or operations in an Execution Plan a pop-up will appear giving you more detailed information about the rows or operation:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP6.JPG?mtime=1357810926"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP6.JPG?mtime=1357810926" width="225" height="270" /></a></div>
<p>Needless to say that the Actual Number of Rows and some other information will not be available in an Estimated Execution Plan. In the example above we see a big difference between the Estimated and the Actual Number of Rows which might indicate that our statistics are out of date.<br />
For even more information select an operation and open the Properties Window (F4):</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP7.JPG?mtime=1357810926"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP7.JPG?mtime=1357810926" width="400" height="225" /></a></div>
<p>Another option to get more out of your Execution Plans is to download <a href="http://www.sqlsentry.com/plan-explorer/sql-server-query-view.asp">SQL Sentry Plan Explorer</a> more information is found at their site but here is a screenshot from the same Execution Plan in the Free Plan Explorer:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP8.JPG?mtime=1357810926"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP8.JPG?mtime=1357810926" width="1267" height="502" /></a></div>
<p>As you can see, the most expensive operation is highlighted in red and the tool also highlights the difference between the estimated and actual rows.</p>

<p><b>Missing index information</b><br />
When executing the following query:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb25999'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb25999','cb5664'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb25999" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> * <span style="color: #0000FF;">FROM</span> Sales.<span style="color: #202020;">vStoreWithContacts</span></li><li style="" class="li2"><span style="color: #0000FF;">WHERE</span> Firstname = <span style="color: #FF0000;">'Alan'</span></li></ol></div><div id="cb5664" style="display: none; color: red;"></div></div></div>
<p>You&#8217;ll see some green text in the top of the Execution Plan:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP9.JPG?mtime=1357810926"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/EP9.JPG?mtime=1357810926" width="1081" height="77" /></a></div>
<p>This information comes from the Dynamic Management View: sys.dm_db_missing_index_details. More information on Indexes is out of scope for this post but be careful with just implanting these Missing Indexes. They might improve the performance of this query but since they need to be maintained they can also slow down other operations on the database.</p>

<p><b>Comparing Execution Plans</b><br />
Are you rewriting a query and wondering which one should perform better? You can easily compare the Execution Plans of two queries. Just write these queries in one query window:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb13455'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb13455','cb33420'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb13455" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> * <span style="color: #0000FF;">FROM</span> Sales.<span style="color: #202020;">vStoreWithContacts</span></li><li style="" class="li2"><span style="color: #0000FF;">WHERE</span> Firstname NOT LIKE <span style="color: #FF0000;">'A%'</span>;</li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">select</span> * <span style="color: #0000FF;">FROM</span> Sales.<span style="color: #202020;">vStoreWithContacts</span></li><li style="" class="li1"><span style="color: #0000FF;">WHERE</span> FirstName IN <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> FirstName <span style="color: #0000FF;">FROM</span> Sales.<span style="color: #202020;">vStoreWithContacts</span></li><li style="" class="li2"><span style="color: #0000FF;">WHERE</span> FirstName NOT LIKE <span style="color: #FF0000;">'A%'</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1"><span style="color: #0000FF;">GO</span></li></ol></div><div id="cb33420" style="display: none; color: red;"></div></div></div>
<p>When you execute the queries you&#8217;ll see they both return the same 70 rows but when you look at the header of the Execution Plans you&#8217;ll see that the first query costs less than the second one in this batch:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/ep10.JPG?mtime=1357810927"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/ep10.JPG?mtime=1357810927" width="275" height="150" /></a></div>

<p><b>Conclusion</b><br />
Execution Plans gives us valuable information and a deep insight in how SQL Server executes the queries we write. Reading and understanding Execution Plans are essential for writing well performing queries.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/getting-started-with-execution-plans">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/getting-started-with-execution-plans#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=2017</wfw:commentRss>
		</item>
				<item>
			<title>Database Projects - Installing the Database Project Template</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/database-projects-installing-the-database-project-template</link>
			<pubDate>Tue, 08 Jan 2013 12:10:00 +0000</pubDate>			<dc:creator>Axel Achten (axel8s)</dc:creator>
			<category domain="alt">Database Programming</category>
<category domain="main">Database Administration</category>
<category domain="alt">Microsoft SQL Server Admin</category>
<category domain="alt">Microsoft SQL Server</category>			<guid isPermaLink="false">2015@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;This is the second post in the Database Projects series. The first post was &lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/database-projects-setting-up-source&quot;&gt;Setting up Source Control&lt;/a&gt;.&lt;br /&gt;
So now we have Source Control in place we need to install the Database Projects Template. To do this launch SQL Server Data Tools 2012 (SSDT). In the Start Page, choose for a New Project:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate1.png?mtime=1357652727&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate1.png?mtime=1357652727&quot; width=&quot;150&quot; height=&quot;150&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;When you want to create a Database Project for the first time, you&#039;ll see a Web Install in the New Project Template screen when you select SQL Server:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate2.png?mtime=1357652946&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate2.png?mtime=1357652946&quot; width=&quot;450&quot; height=&quot;300&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;When you click the Web Install, a new pop-up screen will appear telling you you have to install Microsoft SQL Server Data Tools. Off course this is what you want so hit install:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate3.png?mtime=1357652946&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate3.png?mtime=1357652946&quot; width=&quot;200&quot; height=&quot;100&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;A web page will open where you can download SQL Server Data Tools for Visual Studio 2010 or for Visual Studio 2012. Since I&#039;m using the SQL Server Data Tools delivered with SQL Server 2012 and this is a Visual Studio 2010 Shell I will download the Visual Studio 2010 version:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate4.png?mtime=1357652946&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate4.png?mtime=1357652946&quot; width=&quot;400&quot; height=&quot;200&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;Another screen will show, giving you more details about what&#039;s going to happen and give you more options if you need to install SQL Server Data Tools on a machine with no internet access or with other language needs. Since Dutch is not supported and my machine has internet access I just start the download:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate5.png?mtime=1357652946&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate5.png?mtime=1357652946&quot; width=&quot;200&quot; height=&quot;300&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;Now you can choose to Save or Run the setup. I&#039;m just going to run it:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate6.png?mtime=1357652946&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate6.png?mtime=1357652946&quot; width=&quot;200&quot; height=&quot;100&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;Don&#039;t know if you can trust Microsoft Corporation as a Publisher but hitting Don&#039;t Run will not help you on getting the software installed. So hit Run:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate7.png?mtime=1357652946&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate7.png?mtime=1357652946&quot; width=&quot;200&quot; height=&quot;100&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;Now we need to accept the License Terms to install:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate8.png?mtime=1357652946&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate8.png?mtime=1357652946&quot; width=&quot;300&quot; height=&quot;200&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;Now this is why you have to admire sysadmins, allow the software to make changes to your computer:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate9.png?mtime=1357652946&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate9.png?mtime=1357652946&quot; width=&quot;200&quot; height=&quot;100&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;Finally you can take a nap while the download runs and the installer starts installing.&lt;br /&gt;
After the Setup finish you can Close the installer:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate10.png?mtime=1357652947&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate10.png?mtime=1357652947&quot; width=&quot;300&quot; height=&quot;200&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;Close SQL Server Data Tools and start it again. Because of the new version you might get the question what Default Environment Settings you want. I chose SQL Server Development Settings before starting Visual Studio.&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate11.png?mtime=1357652947&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate11.png?mtime=1357652947&quot; width=&quot;200&quot; height=&quot;200&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p&gt;.&lt;br /&gt;
I still don&#039;t understand why the SQL Server Database Project Template isn&#039;t delivered with the initial setup of the SQL Server Data Tools but we&#039;re almost ready to create our first project.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/database-projects-installing-the-database-project-template&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>This is the second post in the Database Projects series. The first post was <a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/database-projects-setting-up-source">Setting up Source Control</a>.<br />
So now we have Source Control in place we need to install the Database Projects Template. To do this launch SQL Server Data Tools 2012 (SSDT). In the Start Page, choose for a New Project:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate1.png?mtime=1357652727"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate1.png?mtime=1357652727" width="150" height="150" /></a></div>
<p>When you want to create a Database Project for the first time, you'll see a Web Install in the New Project Template screen when you select SQL Server:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate2.png?mtime=1357652946"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate2.png?mtime=1357652946" width="450" height="300" /></a></div>
<p>When you click the Web Install, a new pop-up screen will appear telling you you have to install Microsoft SQL Server Data Tools. Off course this is what you want so hit install:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate3.png?mtime=1357652946"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate3.png?mtime=1357652946" width="200" height="100" /></a></div>
<p>A web page will open where you can download SQL Server Data Tools for Visual Studio 2010 or for Visual Studio 2012. Since I'm using the SQL Server Data Tools delivered with SQL Server 2012 and this is a Visual Studio 2010 Shell I will download the Visual Studio 2010 version:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate4.png?mtime=1357652946"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate4.png?mtime=1357652946" width="400" height="200" /></a></div>
<p>Another screen will show, giving you more details about what's going to happen and give you more options if you need to install SQL Server Data Tools on a machine with no internet access or with other language needs. Since Dutch is not supported and my machine has internet access I just start the download:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate5.png?mtime=1357652946"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate5.png?mtime=1357652946" width="200" height="300" /></a></div>
<p>Now you can choose to Save or Run the setup. I'm just going to run it:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate6.png?mtime=1357652946"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate6.png?mtime=1357652946" width="200" height="100" /></a></div>
<p>Don't know if you can trust Microsoft Corporation as a Publisher but hitting Don't Run will not help you on getting the software installed. So hit Run:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate7.png?mtime=1357652946"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate7.png?mtime=1357652946" width="200" height="100" /></a></div>
<p>Now we need to accept the License Terms to install:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate8.png?mtime=1357652946"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate8.png?mtime=1357652946" width="300" height="200" /></a></div>
<p>Now this is why you have to admire sysadmins, allow the software to make changes to your computer:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate9.png?mtime=1357652946"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate9.png?mtime=1357652946" width="200" height="100" /></a></div>
<p>Finally you can take a nap while the download runs and the installer starts installing.<br />
After the Setup finish you can Close the installer:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate10.png?mtime=1357652947"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate10.png?mtime=1357652947" width="300" height="200" /></a></div>
<p>Close SQL Server Data Tools and start it again. Because of the new version you might get the question what Default Environment Settings you want. I chose SQL Server Development Settings before starting Visual Studio.</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate11.png?mtime=1357652947"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPCreate11.png?mtime=1357652947" width="200" height="200" /></a></div><p>.<br />
I still don't understand why the SQL Server Database Project Template isn't delivered with the initial setup of the SQL Server Data Tools but we're almost ready to create our first project.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/database-projects-installing-the-database-project-template">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/database-projects-installing-the-database-project-template#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=2015</wfw:commentRss>
		</item>
				<item>
			<title>Why you shouldn't use SELECT *</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/why-you-shouldn-t-use</link>
			<pubDate>Wed, 02 Jan 2013 11:28:00 +0000</pubDate>			<dc:creator>Axel Achten (axel8s)</dc:creator>
			<category domain="main">Database Programming</category>
<category domain="alt">Microsoft SQL Server Admin</category>
<category domain="alt">Microsoft SQL Server</category>			<guid isPermaLink="false">2004@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;A customer&#039;s DBA team created a checklist for the development teams with some best practices for writing proper T-SQL and asked me to write some contributions for their tips document library. So if I do the research and write the documents I might as well post them here. The content may not be all sparkling and new but since there is a demand from customers, there are still people out there having trouble finding the correct information.&lt;br /&gt;
So let&#039;s get started. The first document is why you should avoid SELECT * in your queries:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;For performance reasons&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;To be honest this part of the post is based on Ted&#039;s post: &lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/select-vs-select-1-with&quot;&gt;SELECT * vs SELECT 1 with EXISTS&lt;/a&gt;.&lt;br /&gt;
To check the performance impact I use a tool called &lt;a href=&quot;http://www.datamanipulation.net/sqlquerystress/&quot;&gt;SQLQueryStress&lt;/a&gt; to execute my queries and see how much time elapsed.&lt;br /&gt;
To get started I need a table with a large number of columns, data is not necessary for this test so I use this script to generate a table with 127 columns:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb54465&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; @colnumber &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; = &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; @command &lt;span style=&quot;color: #0000FF;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;4000&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; =&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;&#039;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHILE&lt;/span&gt; @colnumber &amp;lt;= &lt;span style=&quot;color: #000;&quot;&gt;125&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;BEGIN&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @command = @command + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; col&#039;&lt;/span&gt; + &lt;span style=&quot;color: #0000FF;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@colnumber &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; int, &#039;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @colnumber += &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;END&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @command = &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;CREATE TABLE StarPerform (PerfId int, &#039;&lt;/span&gt; + @command + &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039; Lastcolumn int)&#039;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;EXEC&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@command&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb5659&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Now I query the table using the SQLQueryStress tool and I choose a high Number of Iterations to get a meaningful average:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform1.JPG?mtime=1357132527&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform1.JPG?mtime=1357132527&quot; width=&quot;1361&quot; height=&quot;430&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;In the Client Seconds/Iteration (Avg) you see that the performance difference for an individual query is negligible but a similar query often executed on a busy server can result in 15 extra seconds on 5000 Iterations.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Broken code&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Using SELECT * in Views is also a bad practice because changes to the underlying table will return unexpected results or fail completely.&lt;br /&gt;
First I create a table and insert some data:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb91862&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;--Create the table&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; StarBreak&lt;br /&gt;&amp;nbsp;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; ID &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IDENTITY&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; Name &lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; DateFirstPost &lt;span style=&quot;color: #0000FF;&quot;&gt;date&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; DateLastPost &lt;span style=&quot;color: #0000FF;&quot;&gt;date&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;--Insert some values&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; StarBreak &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Name, DateFirstPost, DateLastPost&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Denis&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20080207&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20130101&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Ted&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20081107&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20121231&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Koen&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20121123&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20121227&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Jes&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20101210&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20121221&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb16321&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Now I create and query a View to return all the columns:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb44256&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;VIEW&lt;/span&gt; GetStarFromStarBreak&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; * &lt;span style=&quot;color: #0000FF;&quot;&gt;from&lt;/span&gt; Starbreak;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb81865&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;And I get this result back:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform2.JPG?mtime=1357132538&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform2.JPG?mtime=1357132538&quot; width=&quot;261&quot; height=&quot;95&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;Now let&amp;#8217;s drop the table and recreate it but switch the position of the two datecolumns:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb90453&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;--Drop the table&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DROP&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; StarBreak;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;--Create the table&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; StarBreak&lt;br /&gt;&amp;nbsp;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; ID &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IDENTITY&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; Name &lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; DateLastPost &lt;span style=&quot;color: #0000FF;&quot;&gt;date&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; DateFirstPost &lt;span style=&quot;color: #0000FF;&quot;&gt;date&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;--Insert some values&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; StarBreak &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Name, DateFirstPost, DateLastPost&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Denis&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20080207&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20130101&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Ted&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20081107&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20121231&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Koen&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20121123&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20121227&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Jes&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20101210&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20121221&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb89438&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;When I query the table again I get the correct result:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform3.JPG?mtime=1357132547&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform3.JPG?mtime=1357132547&quot; width=&quot;262&quot; height=&quot;97&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;But when I query my view again I get the following result:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform4.JPG?mtime=1357132561&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform4.JPG?mtime=1357132561&quot; width=&quot;261&quot; height=&quot;95&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;You see that the column headers are in the same order as in the initial table but the data reflects the column order of the second table.&lt;br /&gt;
Dropping the table, and adding a column in the middle will also result in the above behavior:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb96080&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;--Drop the table&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DROP&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; StarBreak;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;--Create the table&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; StarBreak&lt;br /&gt;&amp;nbsp;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; ID &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IDENTITY&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; Name &lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; Gender &lt;span style=&quot;color: #0000FF;&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; DateLastPost &lt;span style=&quot;color: #0000FF;&quot;&gt;date&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; DateFirstPost &lt;span style=&quot;color: #0000FF;&quot;&gt;date&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;--Insert some values&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; StarBreak &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Name, DateFirstPost, DateLastPost&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Denis&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20080207&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20130101&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Ted&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20081107&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20121231&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Koen&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20121123&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20121227&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Jes&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20101210&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20121221&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb44940&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform5.JPG?mtime=1357132571&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform5.JPG?mtime=1357132571&quot; width=&quot;258&quot; height=&quot;96&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;What happens when I recreate the table but only 3 instead of 4 columns? Let&amp;#8217;s try:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb28531&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;--Drop the table&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DROP&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; StarBreak;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;--Create the table&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; StarBreak&lt;br /&gt;&amp;nbsp;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; ID &lt;span style=&quot;color: #0000FF;&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IDENTITY&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; Name &lt;span style=&quot;color: #0000FF;&quot;&gt;varchar&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; DateLastPost &lt;span style=&quot;color: #0000FF;&quot;&gt;date&lt;/span&gt;,&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #00AF00;&quot;&gt;--Insert some values&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; StarBreak &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Name, DateLastPost&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Denis&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20130101&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Ted&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20121231&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Koen&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20121227&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Jes&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;20121221&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb14314&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Now query the view again but use this query to make sure you only select the 3 existing columns:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb89079&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; ID, Name, DateLastPost &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; GetStarFromStarBreak&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb16268&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;And the result is:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform6.JPG?mtime=1357132583&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform6.JPG?mtime=1357132583&quot; width=&quot;672&quot; height=&quot;66&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;You see the code breaks because the view still expects 4 columns although you specified you only query 3 columns from the view.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;We have seen several reasons to stay away from the SELECT * statement but we do understand the pain in typing in 127 column names. So here is a last tip:&lt;br /&gt;
When you drag the Columns folder of your table in Object Explorer to the Query Pane, SQL Server Management Studio will automatically list the column names separated with commas for you.&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform7.JPG?mtime=1357132592&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform7.JPG?mtime=1357132592&quot; width=&quot;608&quot; height=&quot;233&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/why-you-shouldn-t-use&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>A customer's DBA team created a checklist for the development teams with some best practices for writing proper T-SQL and asked me to write some contributions for their tips document library. So if I do the research and write the documents I might as well post them here. The content may not be all sparkling and new but since there is a demand from customers, there are still people out there having trouble finding the correct information.<br />
So let's get started. The first document is why you should avoid SELECT * in your queries:</p>

<p><b>For performance reasons</b></p>

<p>To be honest this part of the post is based on Ted's post: <a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/select-vs-select-1-with">SELECT * vs SELECT 1 with EXISTS</a>.<br />
To check the performance impact I use a tool called <a href="http://www.datamanipulation.net/sqlquerystress/">SQLQueryStress</a> to execute my queries and see how much time elapsed.<br />
To get started I need a table with a large number of columns, data is not necessary for this test so I use this script to generate a table with 127 columns:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb85313'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb85313','cb42981'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb85313" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">DECLARE</span> @colnumber <span style="color: #0000FF;">int</span> = <span style="color: #000;">1</span></li><li style="" class="li2"><span style="color: #0000FF;">DECLARE</span> @command <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">4000</span><span style="color: #808080;">&#41;</span> =<span style="color: #FF0000;">''</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #0000FF;">WHILE</span> @colnumber &lt;= <span style="color: #000;">125</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</span></li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @command = @command + <span style="color: #FF0000;">' col'</span> + <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>@colnumber <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">3</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> + <span style="color: #FF0000;">' int, '</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @colnumber += <span style="color: #000;">1</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">END</span></li><li style="" class="li1"><span style="color: #0000FF;">SET</span> @command = <span style="color: #FF0000;">'CREATE TABLE StarPerform (PerfId int, '</span> + @command + <span style="color: #FF0000;">' Lastcolumn int)'</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #0000FF;">EXEC</span> <span style="color: #808080;">&#40;</span>@command<span style="color: #808080;">&#41;</span></li></ol></div><div id="cb42981" style="display: none; color: red;"></div></div></div>
<p>Now I query the table using the SQLQueryStress tool and I choose a high Number of Iterations to get a meaningful average:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform1.JPG?mtime=1357132527"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform1.JPG?mtime=1357132527" width="1361" height="430" /></a></div>
<p>In the Client Seconds/Iteration (Avg) you see that the performance difference for an individual query is negligible but a similar query often executed on a busy server can result in 15 extra seconds on 5000 Iterations.</p>

<p><b>Broken code</b></p>

<p>Using SELECT * in Views is also a bad practice because changes to the underlying table will return unexpected results or fail completely.<br />
First I create a table and insert some data:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb37733'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb37733','cb21572'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb37733" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #00AF00;">--Create the table</span></li><li style="" class="li2"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> StarBreak</li><li style="" class="li1">&nbsp;<span style="color: #808080;">&#40;</span></li><li style="" class="li2">&nbsp; &nbsp; ID <span style="color: #0000FF;">int</span> <span style="color: #0000FF;">IDENTITY</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>,</li><li style="" class="li1">&nbsp; &nbsp; Name <span style="color: #0000FF;">varchar</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span>,</li><li style="" class="li2">&nbsp; &nbsp; DateFirstPost <span style="color: #0000FF;">date</span>,</li><li style="" class="li1">&nbsp; &nbsp; DateLastPost <span style="color: #0000FF;">date</span></li><li style="" class="li2"><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1"><span style="color: #0000FF;">GO</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #00AF00;">--Insert some values</span></li><li style="" class="li2"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> StarBreak <span style="color: #808080;">&#40;</span>Name, DateFirstPost, DateLastPost<span style="color: #808080;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Denis'</span>,<span style="color: #FF0000;">'20080207'</span>,<span style="color: #FF0000;">'20130101'</span><span style="color: #808080;">&#41;</span>,<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Ted'</span>,<span style="color: #FF0000;">'20081107'</span>,<span style="color: #FF0000;">'20121231'</span><span style="color: #808080;">&#41;</span>,<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Koen'</span>,<span style="color: #FF0000;">'20121123'</span>,<span style="color: #FF0000;">'20121227'</span><span style="color: #808080;">&#41;</span>,<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Jes'</span>,<span style="color: #FF0000;">'20101210'</span>,<span style="color: #FF0000;">'20121221'</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2"><span style="color: #0000FF;">GO</span></li></ol></div><div id="cb21572" style="display: none; color: red;"></div></div></div>
<p>Now I create and query a View to return all the columns:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb6526'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb6526','cb44175'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb6526" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">VIEW</span> GetStarFromStarBreak</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">AS</span></li><li style="" class="li1">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> * <span style="color: #0000FF;">from</span> Starbreak;</li><li style="" class="li2">&nbsp; &nbsp; &nbsp; &nbsp; </li><li style="" class="li1"><span style="color: #0000FF;">GO</span></li></ol></div><div id="cb44175" style="display: none; color: red;"></div></div></div>
<p>And I get this result back:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform2.JPG?mtime=1357132538"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform2.JPG?mtime=1357132538" width="261" height="95" /></a></div>
<p>Now let&#8217;s drop the table and recreate it but switch the position of the two datecolumns:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb37446'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb37446','cb89405'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb37446" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #00AF00;">--Drop the table</span></li><li style="" class="li2"><span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> StarBreak;</li><li style="" class="li1"><span style="color: #0000FF;">GO</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #00AF00;">--Create the table</span></li><li style="" class="li2"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> StarBreak</li><li style="" class="li1">&nbsp;<span style="color: #808080;">&#40;</span></li><li style="" class="li2">&nbsp; &nbsp; ID <span style="color: #0000FF;">int</span> <span style="color: #0000FF;">IDENTITY</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>,</li><li style="" class="li1">&nbsp; &nbsp; Name <span style="color: #0000FF;">varchar</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span>,</li><li style="" class="li2">&nbsp; &nbsp; DateLastPost <span style="color: #0000FF;">date</span>,</li><li style="" class="li1">&nbsp; &nbsp; DateFirstPost <span style="color: #0000FF;">date</span></li><li style="" class="li2"><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1"><span style="color: #0000FF;">GO</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #00AF00;">--Insert some values</span></li><li style="" class="li2"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> StarBreak <span style="color: #808080;">&#40;</span>Name, DateFirstPost, DateLastPost<span style="color: #808080;">&#41;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Denis'</span>,<span style="color: #FF0000;">'20080207'</span>,<span style="color: #FF0000;">'20130101'</span><span style="color: #808080;">&#41;</span>,<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Ted'</span>,<span style="color: #FF0000;">'20081107'</span>,<span style="color: #FF0000;">'20121231'</span><span style="color: #808080;">&#41;</span>,<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Koen'</span>,<span style="color: #FF0000;">'20121123'</span>,<span style="color: #FF0000;">'20121227'</span><span style="color: #808080;">&#41;</span>,<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Jes'</span>,<span style="color: #FF0000;">'20101210'</span>,<span style="color: #FF0000;">'20121221'</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2"><span style="color: #0000FF;">GO</span></li></ol></div><div id="cb89405" style="display: none; color: red;"></div></div></div>
<p>When I query the table again I get the correct result:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform3.JPG?mtime=1357132547"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform3.JPG?mtime=1357132547" width="262" height="97" /></a></div>
<p>But when I query my view again I get the following result:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform4.JPG?mtime=1357132561"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform4.JPG?mtime=1357132561" width="261" height="95" /></a></div>
<p>You see that the column headers are in the same order as in the initial table but the data reflects the column order of the second table.<br />
Dropping the table, and adding a column in the middle will also result in the above behavior:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb54272'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb54272','cb4194'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb54272" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #00AF00;">--Drop the table</span></li><li style="" class="li2"><span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> StarBreak;</li><li style="" class="li1"><span style="color: #0000FF;">GO</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #00AF00;">--Create the table</span></li><li style="" class="li2"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> StarBreak</li><li style="" class="li1">&nbsp;<span style="color: #808080;">&#40;</span></li><li style="" class="li2">&nbsp; &nbsp; ID <span style="color: #0000FF;">int</span> <span style="color: #0000FF;">IDENTITY</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>,</li><li style="" class="li1">&nbsp; &nbsp; Name <span style="color: #0000FF;">varchar</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span>,</li><li style="" class="li2">&nbsp; &nbsp; Gender <span style="color: #0000FF;">char</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>,</li><li style="" class="li1">&nbsp; &nbsp; DateLastPost <span style="color: #0000FF;">date</span>,</li><li style="" class="li2">&nbsp; &nbsp; DateFirstPost <span style="color: #0000FF;">date</span></li><li style="" class="li1"><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2"><span style="color: #0000FF;">GO</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #00AF00;">--Insert some values</span></li><li style="" class="li1"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> StarBreak <span style="color: #808080;">&#40;</span>Name, DateFirstPost, DateLastPost<span style="color: #808080;">&#41;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Denis'</span>,<span style="color: #FF0000;">'20080207'</span>,<span style="color: #FF0000;">'20130101'</span><span style="color: #808080;">&#41;</span>,<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Ted'</span>,<span style="color: #FF0000;">'20081107'</span>,<span style="color: #FF0000;">'20121231'</span><span style="color: #808080;">&#41;</span>,<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Koen'</span>,<span style="color: #FF0000;">'20121123'</span>,<span style="color: #FF0000;">'20121227'</span><span style="color: #808080;">&#41;</span>,<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Jes'</span>,<span style="color: #FF0000;">'20101210'</span>,<span style="color: #FF0000;">'20121221'</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1"><span style="color: #0000FF;">GO</span></li></ol></div><div id="cb4194" style="display: none; color: red;"></div></div></div>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform5.JPG?mtime=1357132571"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform5.JPG?mtime=1357132571" width="258" height="96" /></a></div>
<p>What happens when I recreate the table but only 3 instead of 4 columns? Let&#8217;s try:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb66965'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb66965','cb44301'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb66965" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #00AF00;">--Drop the table</span></li><li style="" class="li2"><span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> StarBreak;</li><li style="" class="li1"><span style="color: #0000FF;">GO</span></li><li style="" class="li2">&nbsp;</li><li style="" class="li1"><span style="color: #00AF00;">--Create the table</span></li><li style="" class="li2"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> StarBreak</li><li style="" class="li1">&nbsp;<span style="color: #808080;">&#40;</span></li><li style="" class="li2">&nbsp; &nbsp; ID <span style="color: #0000FF;">int</span> <span style="color: #0000FF;">IDENTITY</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>,</li><li style="" class="li1">&nbsp; &nbsp; Name <span style="color: #0000FF;">varchar</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span>,</li><li style="" class="li2">&nbsp; &nbsp; DateLastPost <span style="color: #0000FF;">date</span>,</li><li style="" class="li1"><span style="color: #808080;">&#41;</span>;</li><li style="" class="li2"><span style="color: #0000FF;">GO</span></li><li style="" class="li1">&nbsp;</li><li style="" class="li2"><span style="color: #00AF00;">--Insert some values</span></li><li style="" class="li1"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> StarBreak <span style="color: #808080;">&#40;</span>Name, DateLastPost<span style="color: #808080;">&#41;</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Denis'</span>,<span style="color: #FF0000;">'20130101'</span><span style="color: #808080;">&#41;</span>,<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Ted'</span>,<span style="color: #FF0000;">'20121231'</span><span style="color: #808080;">&#41;</span>,<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Koen'</span>,<span style="color: #FF0000;">'20121227'</span><span style="color: #808080;">&#41;</span>,<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Jes'</span>,<span style="color: #FF0000;">'20121221'</span><span style="color: #808080;">&#41;</span>;</li><li style="" class="li1"><span style="color: #0000FF;">GO</span></li></ol></div><div id="cb44301" style="display: none; color: red;"></div></div></div>
<p>Now query the view again but use this query to make sure you only select the 3 existing columns:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb23369'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb23369','cb17135'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb23369" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> ID, Name, DateLastPost <span style="color: #0000FF;">FROM</span> GetStarFromStarBreak</li></ol></div><div id="cb17135" style="display: none; color: red;"></div></div></div>
<p>And the result is:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform6.JPG?mtime=1357132583"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform6.JPG?mtime=1357132583" width="672" height="66" /></a></div>
<p>You see the code breaks because the view still expects 4 columns although you specified you only query 3 columns from the view.</p>

<p><b>Conclusion</b></p>

<p>We have seen several reasons to stay away from the SELECT * statement but we do understand the pain in typing in 127 column names. So here is a last tip:<br />
When you drag the Columns folder of your table in Object Explorer to the Query Pane, SQL Server Management Studio will automatically list the column names separated with commas for you.</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform7.JPG?mtime=1357132592"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/StarPerform7.JPG?mtime=1357132592" width="608" height="233" /></a></div><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/why-you-shouldn-t-use">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/why-you-shouldn-t-use#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=2004</wfw:commentRss>
		</item>
				<item>
			<title>Database Projects - Setting up Source Control</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/database-projects-setting-up-source</link>
			<pubDate>Tue, 11 Dec 2012 12:38:00 +0000</pubDate>			<dc:creator>Axel Achten (axel8s)</dc:creator>
			<category domain="main">Database Administration</category>
<category domain="alt">Microsoft SQL Server Admin</category>
<category domain="alt">Microsoft SQL Server</category>
<category domain="alt">Business Intelligence</category>			<guid isPermaLink="false">1933@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;For some time I wanted to document the possibilities of the SQL Server Database Project and how developers and DBA&#039;s can benefit from it.&lt;br /&gt;
&lt;b&gt;Problem Statement&lt;/b&gt;&lt;br /&gt;
Many companies I&#039;ve worked for struggle with their database lifecycle management:&lt;br /&gt;
  &lt;/p&gt;&lt;ul&gt;
&lt;li&gt;Production databases are copied to Test, Staging, UAT... environments by the DBA&#039;s with production (sensitive) data&lt;/li&gt;
  &lt;li&gt;DBA&#039;s spend too much time doing the copy operations&lt;/li&gt;
  &lt;li&gt;Backup chains are broken because of the lack of use of the WITH COPY_ONLY option&lt;/li&gt;
  &lt;li&gt;Changes are made by the DBA executing individual scripts, sometimes breaking because the development database and production database were out of sync.&lt;/li&gt;
  &lt;li&gt;Bugs are &quot;Emergency&quot; fixed in the production database so the schemas of the development and production database are out of sync&lt;/li&gt;
  &lt;li&gt;...&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;b&gt;Possible solution&lt;/b&gt;&lt;br /&gt;
In general there is not much to learn from a developer (just kidding!) but normally they have some software lifecycle management and typically use a Source Control Provider to keep version information and the production version of their software. With SQL Server Data Tools and using a Database Project we should be able to setup a similar lifecycle management for our databases. In my next series of posts I will explore the possibilities of the Database Project and share them with you.&lt;br /&gt;
&lt;b&gt;Getting started - Choosing a Source Control Provider&lt;/b&gt;&lt;br /&gt;
Again as a DBA I did not know what Source Control Provider to choose, I worked with Visual Source Safe a decade ago and had to do a setup of Team Foundation Server once and didn&#039;t want to go through this pain again. A quick poll on twitter on which Source Control Provider to use was depressing. Probably because our American friends were sleeping, the Belgian Developers were still hitting the snooze button and Christiaan was walking his dog. Lucky for me Dave Dustin (&lt;a href=&quot;https://twitter.com/venzann&quot;&gt;twitter&lt;/a&gt;) from the other side of the world gave me some possibilities. I the next example I will use AnkhSVN, Subversion with Support for Visual Studio. You can download the setup from &lt;a href=&quot;http://visualstudiogallery.msdn.microsoft.com/E721D830-7664-4E02-8D03-933C3F1477F2&quot;&gt;here&lt;/a&gt;.&lt;br /&gt;
&lt;b&gt;The setup&lt;/b&gt;&lt;br /&gt;
Launch the installer, accept the license agreement and hit Install:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPSetup1.png?mtime=1355235876&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPSetup1.png?mtime=1355235876&quot; width=&quot;250&quot; height=&quot;194&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;Off course you have to allow the program to install software:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPSetup2.png?mtime=1355235894&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPSetup2.png?mtime=1355235894&quot; width=&quot;232&quot; height=&quot;121&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;Et voila, the setup is finished:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPSetup3.png?mtime=1355236334&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPSetup3.png?mtime=1355236334&quot; width=&quot;250&quot; height=&quot;194&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;When you open SQL Server Data Tools and open the Options pane in the Tools menu you should see that AnkhSVN - Subversion Support for Visual Studio is selected as the Current Source Control plug in, if not you can select it from the list:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPSetup4.png?mtime=1355235963&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPSetup4.png?mtime=1355235963&quot; width=&quot;373&quot; height=&quot;215&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;Now that we have a Source Control provider we can get started with our database project but this will be the subject of my next post.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/database-projects-setting-up-source&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>For some time I wanted to document the possibilities of the SQL Server Database Project and how developers and DBA's can benefit from it.<br />
<b>Problem Statement</b><br />
Many companies I've worked for struggle with their database lifecycle management:<br />
  </p><ul>
<li>Production databases are copied to Test, Staging, UAT... environments by the DBA's with production (sensitive) data</li>
  <li>DBA's spend too much time doing the copy operations</li>
  <li>Backup chains are broken because of the lack of use of the WITH COPY_ONLY option</li>
  <li>Changes are made by the DBA executing individual scripts, sometimes breaking because the development database and production database were out of sync.</li>
  <li>Bugs are "Emergency" fixed in the production database so the schemas of the development and production database are out of sync</li>
  <li>...</li>
</ul>
<p><b>Possible solution</b><br />
In general there is not much to learn from a developer (just kidding!) but normally they have some software lifecycle management and typically use a Source Control Provider to keep version information and the production version of their software. With SQL Server Data Tools and using a Database Project we should be able to setup a similar lifecycle management for our databases. In my next series of posts I will explore the possibilities of the Database Project and share them with you.<br />
<b>Getting started - Choosing a Source Control Provider</b><br />
Again as a DBA I did not know what Source Control Provider to choose, I worked with Visual Source Safe a decade ago and had to do a setup of Team Foundation Server once and didn't want to go through this pain again. A quick poll on twitter on which Source Control Provider to use was depressing. Probably because our American friends were sleeping, the Belgian Developers were still hitting the snooze button and Christiaan was walking his dog. Lucky for me Dave Dustin (<a href="https://twitter.com/venzann">twitter</a>) from the other side of the world gave me some possibilities. I the next example I will use AnkhSVN, Subversion with Support for Visual Studio. You can download the setup from <a href="http://visualstudiogallery.msdn.microsoft.com/E721D830-7664-4E02-8D03-933C3F1477F2">here</a>.<br />
<b>The setup</b><br />
Launch the installer, accept the license agreement and hit Install:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPSetup1.png?mtime=1355235876"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPSetup1.png?mtime=1355235876" width="250" height="194" /></a></div>
<p>Off course you have to allow the program to install software:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPSetup2.png?mtime=1355235894"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPSetup2.png?mtime=1355235894" width="232" height="121" /></a></div>
<p>Et voila, the setup is finished:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPSetup3.png?mtime=1355236334"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPSetup3.png?mtime=1355236334" width="250" height="194" /></a></div>
<p>When you open SQL Server Data Tools and open the Options pane in the Tools menu you should see that AnkhSVN - Subversion Support for Visual Studio is selected as the Current Source Control plug in, if not you can select it from the list:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPSetup4.png?mtime=1355235963"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/DBPSetup4.png?mtime=1355235963" width="373" height="215" /></a></div>
<p>Now that we have a Source Control provider we can get started with our database project but this will be the subject of my next post.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/database-projects-setting-up-source">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/database-projects-setting-up-source#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=1933</wfw:commentRss>
		</item>
				<item>
			<title>Book Review: Microsoft SQL Server 2012 T-SQL Fundamentals</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/book-review-microsoft-sql-server-1</link>
			<pubDate>Tue, 27 Nov 2012 17:37:00 +0000</pubDate>			<dc:creator>Axel Achten (axel8s)</dc:creator>
			<category domain="main">Database Programming</category>
<category domain="alt">Microsoft SQL Server</category>			<guid isPermaLink="false">1810@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;With a basis as a system engineer, I became a DBA. And DBA&#039;s don&#039;t often write real DML queries. I can read a query, understand what it&#039;s doing but when I need to read one I need BOL the help me out. Taking the &lt;a href=&quot;http://www.microsoft.com/learning/en/us/exam.aspx?ID=70-457&quot;&gt;070-457 Transition Your MCTS on SQL Server to MCSA: SQL Server 2012,Part 1&lt;/a&gt; exam showed me I&#039;m strong in database administration but there was room for improvement for my query skills. That&#039;s why I bought &lt;a href=&quot;http://shop.oreilly.com/product/0790145321978.do&quot;&gt;Microsoft SQL Server 2012 T-SQL Fundamentals&lt;/a&gt; by Itzik Ben-Gan (&lt;a href=&quot;http://tsql.solidq.com/&quot;&gt;blog&lt;/a&gt; | &lt;a href=&quot;https://twitter.com/ItzikBenGan&quot;&gt;twitter&lt;/a&gt;).&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://shop.oreilly.com/product/0790145321978.do&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/BRTsql.jpg?mtime=1354044858&quot; width=&quot;175&quot; height=&quot;214&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;b&gt;Chapter 1: Background to T-SQL Querying and Programming&lt;/b&gt;&lt;br /&gt;
When you followed a bunch of trainings and read some books about SQL Server, you will possibly have seen some chapters on the origin and basics of relational databases and the SQL language. But I bet you have never seen a chapter like this one. In a straight and clear way Itzik guides you through the mathematics behind SQL, the flavors and architecture of SQL Server and the definitions of tables en data integrity. Now you&#039;re ready to stand between other professionals and at least know what they are talking about. For me, they should include these 25 pages in every SQL Server learning book.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Chapter 2: Single-Table Queries&lt;/b&gt;&lt;br /&gt;
In the largest chapter of this book, the writer explains every part of the SELECT statement and how these queries are processed by SQL Server. It&#039;s the basis a user needs to start understanding set-based logic. In the second part of the chapter predicates and operators, CASE expressions, NULL handling, character data, date and time data and even metadata is handled. It might seem overwhelming for starters but all the essentials for writing queries are in this chapter.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Chapter 3: Joins&lt;/b&gt;&lt;br /&gt;
I thought I knew how joins work, but after this chapter I know a lot more. All join types are explained in detail including a subchapter Beyond the Fundamentals. And again explained in such a clear way. My students will certainly benefit from this.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Chapter 4: Subqueries&lt;/b&gt;&lt;br /&gt;
The chapter starts with the self-contained subqueries, that are easy to understand and moves on to the correlated subqueries and shows the power of them. The EXISTS predicate is also well explained. In the beyond the fundamentals of the chapter some examples are shown on how to build windowing functionality without the windowing functions. The only disadvantage in the chapter is the fact that SOME, ANY and ALL are not covered because they are rarely used. From experience I know they might show up on Microsoft exams.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Chapter 5: Table Expressions&lt;/b&gt;&lt;br /&gt;
A beautiful chapter where you will learn to write real powerful queries using derived tables, Common Table Expressions, Views , Functions and the APPLY operator. You&#039;ll see the true value of these expressions when you see the complex definitions of the exercises and the simple queries you need in the solution.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Chapter 6: Set Operators&lt;/b&gt;&lt;br /&gt;
UNION, INTERSECT, EXCEPT will have no more secrets for you, the writer even shows you how to create queries to implement the INTERSECT and EXCEPT ALL operators that are supported in Standard SQL but are not implemented in T-SQL.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Chapter 7: Beyond the Fundamentals of Querying&lt;/b&gt;&lt;br /&gt;
Everybody has heard of the new Windowing functions in SQL 2012 but little people can explain it as clear and simple as it&#039;s written down in this book. Also pivoting is explained again and all the possibilities of groupings and grouping sets are written out in plain T-SQL and with use of the functions so you can really see what the different key words are doing. So the chapter goes indeed beyond the fundamentals, but I would mark it as one of the essentials you should learn about T-SQL&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Chapter 8: Data Modification&lt;/b&gt;&lt;br /&gt;
The basics of INSERT, UPDATE, DELETE and MERGE, BULK INSERT, Identities and Sequences all well explained. But then UPDATE based on a JOIN, DELETE with TOP and OFFSET-FETCH and off course the OUTPUT clause with all its possibilities. I think a lot of production code will be optimized after reading this chapter.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Chapter 9: Transactions and Concurrency&lt;/b&gt;&lt;br /&gt;
The chapter starts with the theory of transactions, locks, blocks and some copy and paste code to start troubleshooting blocking issues on your production servers. But the decent explanation of how the Isolation Levels work with simple examples makes it understandable for everyone.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;Chapter 10: Programmable Objects&lt;/b&gt;&lt;br /&gt;
The last chapter in the book is about variables, batches, cursors, procedures,etc. ... And because they are not considered as fundamentals you get an overview with decent examples to get you started. &lt;/p&gt;

&lt;p&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;br /&gt;
If you need to write queries with T-SQL, this is a book that will get you started. After every chapter you&#039;ll find also a number of exercises to test what you have learned.&lt;/p&gt;

&lt;p&gt;For me the book is decent value for money, now let&#039;s see when I can schedule my exam.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/book-review-microsoft-sql-server-1&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>With a basis as a system engineer, I became a DBA. And DBA's don't often write real DML queries. I can read a query, understand what it's doing but when I need to read one I need BOL the help me out. Taking the <a href="http://www.microsoft.com/learning/en/us/exam.aspx?ID=70-457">070-457 Transition Your MCTS on SQL Server to MCSA: SQL Server 2012,Part 1</a> exam showed me I'm strong in database administration but there was room for improvement for my query skills. That's why I bought <a href="http://shop.oreilly.com/product/0790145321978.do">Microsoft SQL Server 2012 T-SQL Fundamentals</a> by Itzik Ben-Gan (<a href="http://tsql.solidq.com/">blog</a> | <a href="https://twitter.com/ItzikBenGan">twitter</a>).</p>
<div class="image_block"><a href="http://shop.oreilly.com/product/0790145321978.do"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/BRTsql.jpg?mtime=1354044858" width="175" height="214" /></a></div>
<p><b>Chapter 1: Background to T-SQL Querying and Programming</b><br />
When you followed a bunch of trainings and read some books about SQL Server, you will possibly have seen some chapters on the origin and basics of relational databases and the SQL language. But I bet you have never seen a chapter like this one. In a straight and clear way Itzik guides you through the mathematics behind SQL, the flavors and architecture of SQL Server and the definitions of tables en data integrity. Now you're ready to stand between other professionals and at least know what they are talking about. For me, they should include these 25 pages in every SQL Server learning book.</p>

<p><b>Chapter 2: Single-Table Queries</b><br />
In the largest chapter of this book, the writer explains every part of the SELECT statement and how these queries are processed by SQL Server. It's the basis a user needs to start understanding set-based logic. In the second part of the chapter predicates and operators, CASE expressions, NULL handling, character data, date and time data and even metadata is handled. It might seem overwhelming for starters but all the essentials for writing queries are in this chapter.</p>

<p><b>Chapter 3: Joins</b><br />
I thought I knew how joins work, but after this chapter I know a lot more. All join types are explained in detail including a subchapter Beyond the Fundamentals. And again explained in such a clear way. My students will certainly benefit from this.</p>

<p><b>Chapter 4: Subqueries</b><br />
The chapter starts with the self-contained subqueries, that are easy to understand and moves on to the correlated subqueries and shows the power of them. The EXISTS predicate is also well explained. In the beyond the fundamentals of the chapter some examples are shown on how to build windowing functionality without the windowing functions. The only disadvantage in the chapter is the fact that SOME, ANY and ALL are not covered because they are rarely used. From experience I know they might show up on Microsoft exams.</p>

<p><b>Chapter 5: Table Expressions</b><br />
A beautiful chapter where you will learn to write real powerful queries using derived tables, Common Table Expressions, Views , Functions and the APPLY operator. You'll see the true value of these expressions when you see the complex definitions of the exercises and the simple queries you need in the solution.</p>

<p><b>Chapter 6: Set Operators</b><br />
UNION, INTERSECT, EXCEPT will have no more secrets for you, the writer even shows you how to create queries to implement the INTERSECT and EXCEPT ALL operators that are supported in Standard SQL but are not implemented in T-SQL.</p>

<p><b>Chapter 7: Beyond the Fundamentals of Querying</b><br />
Everybody has heard of the new Windowing functions in SQL 2012 but little people can explain it as clear and simple as it's written down in this book. Also pivoting is explained again and all the possibilities of groupings and grouping sets are written out in plain T-SQL and with use of the functions so you can really see what the different key words are doing. So the chapter goes indeed beyond the fundamentals, but I would mark it as one of the essentials you should learn about T-SQL</p>

<p><b>Chapter 8: Data Modification</b><br />
The basics of INSERT, UPDATE, DELETE and MERGE, BULK INSERT, Identities and Sequences all well explained. But then UPDATE based on a JOIN, DELETE with TOP and OFFSET-FETCH and off course the OUTPUT clause with all its possibilities. I think a lot of production code will be optimized after reading this chapter.</p>

<p><b>Chapter 9: Transactions and Concurrency</b><br />
The chapter starts with the theory of transactions, locks, blocks and some copy and paste code to start troubleshooting blocking issues on your production servers. But the decent explanation of how the Isolation Levels work with simple examples makes it understandable for everyone.</p>

<p><b>Chapter 10: Programmable Objects</b><br />
The last chapter in the book is about variables, batches, cursors, procedures,etc. ... And because they are not considered as fundamentals you get an overview with decent examples to get you started. </p>

<p><b>Conclusion</b><br />
If you need to write queries with T-SQL, this is a book that will get you started. After every chapter you'll find also a number of exercises to test what you have learned.</p>

<p>For me the book is decent value for money, now let's see when I can schedule my exam.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/book-review-microsoft-sql-server-1">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/book-review-microsoft-sql-server-1#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=1810</wfw:commentRss>
		</item>
				<item>
			<title>Why you should register for SQL Server Days 2012</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/MSSQLServer/why-you-should-register-for</link>
			<pubDate>Tue, 09 Oct 2012 11:54:00 +0000</pubDate>			<dc:creator>Axel Achten (axel8s)</dc:creator>
			<category domain="main">Microsoft SQL Server</category>			<guid isPermaLink="false">1853@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;On 19 and 20 November, the annual &lt;a href=&quot;http://www.sqlserverdays.be/2012/&quot;&gt;SQL Server Days&lt;/a&gt; will take place again. This 2 day event organized by the &lt;a href=&quot;http://sqlug.be/&quot;&gt;Belgian SQL Server User Group&lt;/a&gt; is the event for all people working with MS SQL Server in Belgium (others are welcome too). From beginner to expert, from developer to DBA, from Excel specialist to BI designer. &lt;br /&gt;
Why?&lt;br /&gt;
Because they provide a different track for all the profiles even for the Oracle DBA&#039;s.&lt;br /&gt;
Because some international top speakers are coming like :&lt;/p&gt;&lt;ul&gt;&lt;li&gt; Bob Beauchemin (&lt;a href=&quot;http://www.sqlskills.com/BLOGS/BOBB/&quot;&gt;Blog&lt;/a&gt; | &lt;a href=&quot;http://www.twitter.com/bobbeauch&quot;&gt;Twitter&lt;/a&gt;): Whenever you see someone from SQLskills on a speaker list you shouldn&#039;t doubt and just go. I&#039;ve seen Bob teaching in Barcelona twice and can assure you, listing to him is fun but be prepared for some deep learning&lt;/li&gt;
&lt;li&gt; Dandy Weyn (&lt;a href=&quot;http://www.ilikesql.com/&quot;&gt;Blog &lt;/a&gt;| &lt;a href=&quot;https://twitter.com/ilikesql):&quot;&gt;https://twitter.com/ilikesql):&lt;/a&gt; The Belgian who presented the American MS SQL Server 2012 roadshow. Who wants to miss that?&lt;/li&gt;
&lt;li&gt;Mladen Prajdic (&lt;a href=&quot;http://weblogs.sqlteam.com/mladenp/default.aspx&quot;&gt;Blog &lt;/a&gt;| &lt;a href=&quot;https://de.twitter.com/MladenPrajdic&quot;&gt;Twitter&lt;/a&gt;): When you&#039;re able to write a SSMS plugin like the SSMS Tools Pack, you can tell a lot more about the tricks nobody heard of.&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Because also Belgian top speakers will present some sessions :&lt;/p&gt;&lt;ul&gt;
&lt;li&gt;Karel Coenye (&lt;a href=&quot;https://twitter.com/Ryazame&quot;&gt;twitter&lt;/a&gt;) will be talking about the forgotten fill factor&lt;/li&gt;&lt;li&gt;Pieter Vanhove (&lt;a href=&quot;http://blogs.sqlug.be/pieter/&quot;&gt;blog&lt;/a&gt; | &lt;a href=&quot;http://twitter.com/pieter_vanhove&quot;&gt;twitter&lt;/a&gt;)will help you set your backup settings right&lt;/li&gt;&lt;li&gt;Rising SSIS star Koen Verbeeck (&lt;a href=&quot;http://twitter.com/Ko_Ver&quot;&gt;twitter&lt;/a&gt;) will celebrate the wedding between SSIS and CDC&lt;/li&gt;&lt;li&gt;O-Advisor Wesley Backelant (&lt;a href=&quot;https://twitter.com/WesleyBackelant&quot;&gt;twitter&lt;/a&gt;)will finally tell you what this OData thing is you keep hearing about.&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Because you just want to meet over 400 peers from Belgium who work with MS SQL Server.&lt;br /&gt;
So what are you waiting for?&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://www.sqlserverdays.be/2012/registration&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/SQLDays.gif?mtime=1349790379&quot; width=&quot;699&quot; height=&quot;198&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/MSSQLServer/why-you-should-register-for&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>On 19 and 20 November, the annual <a href="http://www.sqlserverdays.be/2012/">SQL Server Days</a> will take place again. This 2 day event organized by the <a href="http://sqlug.be/">Belgian SQL Server User Group</a> is the event for all people working with MS SQL Server in Belgium (others are welcome too). From beginner to expert, from developer to DBA, from Excel specialist to BI designer. <br />
Why?<br />
Because they provide a different track for all the profiles even for the Oracle DBA's.<br />
Because some international top speakers are coming like :</p><ul><li> Bob Beauchemin (<a href="http://www.sqlskills.com/BLOGS/BOBB/">Blog</a> | <a href="http://www.twitter.com/bobbeauch">Twitter</a>): Whenever you see someone from SQLskills on a speaker list you shouldn't doubt and just go. I've seen Bob teaching in Barcelona twice and can assure you, listing to him is fun but be prepared for some deep learning</li>
<li> Dandy Weyn (<a href="http://www.ilikesql.com/">Blog </a>| <a href="https://twitter.com/ilikesql):">https://twitter.com/ilikesql):</a> The Belgian who presented the American MS SQL Server 2012 roadshow. Who wants to miss that?</li>
<li>Mladen Prajdic (<a href="http://weblogs.sqlteam.com/mladenp/default.aspx">Blog </a>| <a href="https://de.twitter.com/MladenPrajdic">Twitter</a>): When you're able to write a SSMS plugin like the SSMS Tools Pack, you can tell a lot more about the tricks nobody heard of.</li></ul>
<p>Because also Belgian top speakers will present some sessions :</p><ul>
<li>Karel Coenye (<a href="https://twitter.com/Ryazame">twitter</a>) will be talking about the forgotten fill factor</li><li>Pieter Vanhove (<a href="http://blogs.sqlug.be/pieter/">blog</a> | <a href="http://twitter.com/pieter_vanhove">twitter</a>)will help you set your backup settings right</li><li>Rising SSIS star Koen Verbeeck (<a href="http://twitter.com/Ko_Ver">twitter</a>) will celebrate the wedding between SSIS and CDC</li><li>O-Advisor Wesley Backelant (<a href="https://twitter.com/WesleyBackelant">twitter</a>)will finally tell you what this OData thing is you keep hearing about.</li></ul>
<p>Because you just want to meet over 400 peers from Belgium who work with MS SQL Server.<br />
So what are you waiting for?</p>
<div class="image_block"><a href="http://www.sqlserverdays.be/2012/registration"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/SQLDays.gif?mtime=1349790379" width="699" height="198" /></a></div><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/MSSQLServer/why-you-should-register-for">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/MSSQLServer/why-you-should-register-for#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=1853</wfw:commentRss>
		</item>
				<item>
			<title>T-SQL CASE functions</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/MSSQLServer/t-sql-case-functions</link>
			<pubDate>Mon, 03 Sep 2012 10:12:00 +0000</pubDate>			<dc:creator>Axel Achten (axel8s)</dc:creator>
			<category domain="main">Microsoft SQL Server</category>			<guid isPermaLink="false">1815@http://blogs.lessthandot.com/</guid>
						<description>&lt;p&gt;With the CASE statement you can add conditional logic to your T-SQL code. T-SQL 2012 has certain functions that can be seen as CASE shortcuts. With these functions you can quickly use some CASE functionality and as a surplus, because they are functions you can use them everywhere where expressions are allowed. The examples are written against the AdventureWorks2012 database:&lt;/p&gt;

&lt;p&gt;&lt;b&gt;ISNULL&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;The ISNULL(&#039;Null_Expression&#039;,&#039;Replace_Value&#039;) function evaluates the value of the &#039;Null_Expression&#039; and if the result is NULL it returns the &#039;Replace_Value&#039; value. Otherwise the &#039;Null_Expression&#039; value is returned:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb76451&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;10&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;ISNULL&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;MiddleName,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;NA&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; NoNullMiddleName &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Person.&lt;span style=&quot;color: #202020;&quot;&gt;Person&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb32345&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;In the result we see that all the NULL values are replaced by the &#039;NA&#039; value. Values that were not NULL keep their value:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/CaseSC1.gif?mtime=1346673979&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/CaseSC1.gif?mtime=1346673979&quot; width=&quot;154&quot; height=&quot;235&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;b&gt;COALESCE&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;This function accepts 2 or more arguments and will return the first non NULL value. In the next example I use a common table expression and fill it with items with a weight and no size, items with a size and no weight and items with a size and weight:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb95724&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WITH&lt;/span&gt; Product_CTE &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;name, &lt;span style=&quot;color: #0000FF;&quot;&gt;size&lt;/span&gt;, weight&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;10&lt;/span&gt; name, &lt;span style=&quot;color: #0000FF;&quot;&gt;size&lt;/span&gt;, weight &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Production.&lt;span style=&quot;color: #202020;&quot;&gt;Product&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;size&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IS&lt;/span&gt; NOT NULL AND weight &lt;span style=&quot;color: #0000FF;&quot;&gt;IS&lt;/span&gt; NULL&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;UNION&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;10&lt;/span&gt; name, &lt;span style=&quot;color: #0000FF;&quot;&gt;size&lt;/span&gt;, weight &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Production.&lt;span style=&quot;color: #202020;&quot;&gt;Product&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; weight &lt;span style=&quot;color: #0000FF;&quot;&gt;IS&lt;/span&gt; NOT NULL AND &lt;span style=&quot;color: #0000FF;&quot;&gt;size&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IS&lt;/span&gt; NULL&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;UNION&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;10&lt;/span&gt; name, &lt;span style=&quot;color: #0000FF;&quot;&gt;size&lt;/span&gt;, weight &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Production.&lt;span style=&quot;color: #202020;&quot;&gt;Product&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; weight &lt;span style=&quot;color: #0000FF;&quot;&gt;IS&lt;/span&gt; NOT NULL AND &lt;span style=&quot;color: #0000FF;&quot;&gt;size&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IS&lt;/span&gt; NOT NULL&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;Select&lt;/span&gt; name, &lt;span style=&quot;color: #0000FF;&quot;&gt;size&lt;/span&gt;, weight, &lt;span style=&quot;color: #FF00FF;&quot;&gt;COALESCE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;size&lt;/span&gt;,&lt;span style=&quot;color: #0000FF;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;weight &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; coalesceresult&lt;br /&gt;&amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Product_CTE&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb57446&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;In the result you see that the size is shown in the coalesceresult column unless the original value was NULL. In that case the weight value is returned:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/CaseSC2.gif?mtime=1346673994&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/CaseSC2.gif?mtime=1346673994&quot; width=&quot;369&quot; height=&quot;269&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;b&gt;IIF&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Just like in SSIS and MS Access, SQL Server 2012 now supports the IIF(&#039;logical_expression&#039;,&#039;expression_if_true&#039;,&#039;expression_if_false&#039;) function. So if the &#039;logical_expression evaluates to TRUE the &#039;expression_if_true&#039; value is shown, otherwise the &#039;expression_if_false&#039; is executed:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb2435&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;IIF&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Title LiKE &lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;%s.&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Miss&#039;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;&#039;Mister&#039;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; Title, FirstName &lt;br /&gt;&amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Person.&lt;span style=&quot;color: #202020;&quot;&gt;Person&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; Title &lt;span style=&quot;color: #0000FF;&quot;&gt;IS&lt;/span&gt; NOT NULL&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb45828&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Since IIF supports the true/false expressions you can even put logical expressions in their place to create single line CASE scenarios to have more possibilities. For ease of use and general readability of your code, I would not recommend this. The result of the previous query:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/CaseSC3.gif?mtime=1346674010&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/CaseSC3.gif?mtime=1346674010&quot; width=&quot;154&quot; height=&quot;257&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;b&gt;CHOOSE&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;CHOOSE(&#039;index&#039;,&#039;expr1&#039;,&#039;expr2&#039;,...,&#039;exprn&#039;) is also known in MS Access and new in SQL Server 2012 and gives the possibility to use an index number and the result of the corresponding index expression:&lt;/p&gt;
&lt;div class=&quot;codebox&quot;&gt;&lt;div class=&quot;codeheader&quot;&gt;Code: &lt;span&gt;tsql&lt;/span&gt;&lt;/div&gt;&lt;div class=&quot;codeholder&quot;&gt;&lt;div class=&quot;tsql&quot; id=&quot;cb13493&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; FirstName, LastName, MiddleName, &lt;span style=&quot;color: #FF00FF;&quot;&gt;CHOOSE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;2&lt;/span&gt;,FirstName, LastName, MiddleName&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; Chosen&lt;br /&gt;&amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Person.&lt;span style=&quot;color: #202020;&quot;&gt;Person&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb53033&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;In normal coding you would dynamically assign a value to the index with a variable. The result of the above query is:&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;a href=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/CaseSC4.gif?mtime=1346674021&quot;&gt;&lt;img alt=&quot;&quot; src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/CaseSC4.gif?mtime=1346674021&quot; width=&quot;330&quot; height=&quot;256&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;p&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;The CASE shortcuts can be very useful in coding scenarios but note that only COALESCE is ANSI standard. IIF and CHOOSE are added to the T-SQL 2012 language to ease the migration from MS Access to SQL Server.&lt;/p&gt;&lt;div class=&quot;item_footer&quot;&gt;&lt;p&gt;&lt;small&gt;&lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/MSSQLServer/t-sql-case-functions&quot;&gt;Original post&lt;/a&gt; blogged on &lt;a href=&quot;http://lessthandot.com/&quot;&gt;LessThanDot&lt;/a&gt;.&lt;/small&gt;&lt;/p&gt;&lt;/div&gt;</description>
			<content:encoded><![CDATA[<p>With the CASE statement you can add conditional logic to your T-SQL code. T-SQL 2012 has certain functions that can be seen as CASE shortcuts. With these functions you can quickly use some CASE functionality and as a surplus, because they are functions you can use them everywhere where expressions are allowed. The examples are written against the AdventureWorks2012 database:</p>

<p><b>ISNULL</b></p>

<p>The ISNULL('Null_Expression','Replace_Value') function evaluates the value of the 'Null_Expression' and if the result is NULL it returns the 'Replace_Value' value. Otherwise the 'Null_Expression' value is returned:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb70891'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb70891','cb78020'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb70891" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">10</span> <span style="color: #FF00FF;">ISNULL</span><span style="color: #808080;">&#40;</span>MiddleName,<span style="color: #FF0000;">'NA'</span><span style="color: #808080;">&#41;</span><span style="color: #0000FF;">AS</span> NoNullMiddleName <span style="color: #0000FF;">FROM</span> Person.<span style="color: #202020;">Person</span></li></ol></div><div id="cb78020" style="display: none; color: red;"></div></div></div>
<p>In the result we see that all the NULL values are replaced by the 'NA' value. Values that were not NULL keep their value:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/CaseSC1.gif?mtime=1346673979"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/CaseSC1.gif?mtime=1346673979" width="154" height="235" /></a></div>
<p><b>COALESCE</b></p>

<p>This function accepts 2 or more arguments and will return the first non NULL value. In the next example I use a common table expression and fill it with items with a weight and no size, items with a size and no weight and items with a size and weight:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb39180'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb39180','cb48470'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb39180" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">WITH</span> Product_CTE <span style="color: #808080;">&#40;</span>name, <span style="color: #0000FF;">size</span>, weight<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span></li><li style="" class="li2"><span style="color: #808080;">&#40;</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">10</span> name, <span style="color: #0000FF;">size</span>, weight <span style="color: #0000FF;">FROM</span> Production.<span style="color: #202020;">Product</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> <span style="color: #0000FF;">size</span> <span style="color: #0000FF;">IS</span> NOT NULL AND weight <span style="color: #0000FF;">IS</span> NULL</li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">UNION</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">10</span> name, <span style="color: #0000FF;">size</span>, weight <span style="color: #0000FF;">FROM</span> Production.<span style="color: #202020;">Product</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> weight <span style="color: #0000FF;">IS</span> NOT NULL AND <span style="color: #0000FF;">size</span> <span style="color: #0000FF;">IS</span> NULL</li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">UNION</span></li><li style="" class="li1">&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">10</span> name, <span style="color: #0000FF;">size</span>, weight <span style="color: #0000FF;">FROM</span> Production.<span style="color: #202020;">Product</span></li><li style="" class="li2">&nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> weight <span style="color: #0000FF;">IS</span> NOT NULL AND <span style="color: #0000FF;">size</span> <span style="color: #0000FF;">IS</span> NOT NULL</li><li style="" class="li1"><span style="color: #808080;">&#41;</span></li><li style="" class="li2"><span style="color: #0000FF;">Select</span> name, <span style="color: #0000FF;">size</span>, weight, <span style="color: #FF00FF;">COALESCE</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">size</span>,<span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>weight <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> coalesceresult</li><li style="" class="li1">&nbsp;<span style="color: #0000FF;">FROM</span> Product_CTE</li></ol></div><div id="cb48470" style="display: none; color: red;"></div></div></div>
<p>In the result you see that the size is shown in the coalesceresult column unless the original value was NULL. In that case the weight value is returned:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/CaseSC2.gif?mtime=1346673994"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/CaseSC2.gif?mtime=1346673994" width="369" height="269" /></a></div>
<p><b>IIF</b></p>

<p>Just like in SSIS and MS Access, SQL Server 2012 now supports the IIF('logical_expression','expression_if_true','expression_if_false') function. So if the 'logical_expression evaluates to TRUE the 'expression_if_true' value is shown, otherwise the 'expression_if_false' is executed:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb21977'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb21977','cb48316'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb21977" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">IIF</span><span style="color: #808080;">&#40;</span>Title LiKE <span style="color: #FF0000;">'%s.'</span>,<span style="color: #FF0000;">'Miss'</span>,<span style="color: #FF0000;">'Mister'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> Title, FirstName </li><li style="" class="li2">&nbsp;<span style="color: #0000FF;">FROM</span> Person.<span style="color: #202020;">Person</span></li><li style="" class="li1">&nbsp;<span style="color: #0000FF;">WHERE</span> Title <span style="color: #0000FF;">IS</span> NOT NULL</li></ol></div><div id="cb48316" style="display: none; color: red;"></div></div></div>
<p>Since IIF supports the true/false expressions you can even put logical expressions in their place to create single line CASE scenarios to have more possibilities. For ease of use and general readability of your code, I would not recommend this. The result of the previous query:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/CaseSC3.gif?mtime=1346674010"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/CaseSC3.gif?mtime=1346674010" width="154" height="257" /></a></div>
<p><b>CHOOSE</b></p>

<p>CHOOSE('index','expr1','expr2',...,'exprn') is also known in MS Access and new in SQL Server 2012 and gives the possibility to use an index number and the result of the corresponding index expression:</p>
<div class="codebox"><div class="codeheader"><span>tsql</span><div class="codebox_javascript_links"><a href="http://blogs.lessthandot.com" onclick="linenumberOnOff('cb7963'); return false;">Line number Off</a> | <a href="http://blogs.lessthandot.com#" onclick="expandCode('cb7963','cb524'); return false;">Hide</a> | <a href="http://blogs.lessthandot.com#" onclick="selectCode(this); return false;">Select all</a></div></div><!-- we need this dummy div to fix a firefox bug when selecting code lines --><div class="codeholder"><div class="tsql" id="cb7963" style="display: block; color: rgb(0, 0, 0);"><ol><li style="" class="li1"><span style="color: #0000FF;">SELECT</span> FirstName, LastName, MiddleName, <span style="color: #FF00FF;">CHOOSE</span><span style="color: #808080;">&#40;</span><span style="color: #000;">2</span>,FirstName, LastName, MiddleName<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> Chosen</li><li style="" class="li2">&nbsp;<span style="color: #0000FF;">FROM</span> Person.<span style="color: #202020;">Person</span></li></ol></div><div id="cb524" style="display: none; color: red;"></div></div></div>
<p>In normal coding you would dynamically assign a value to the index with a variable. The result of the above query is:</p>
<div class="image_block"><a href="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/CaseSC4.gif?mtime=1346674021"><img alt="" src="http://blogs.lessthandot.com/media/blogs/DataMgmt/Axel8s/CaseSC4.gif?mtime=1346674021" width="330" height="256" /></a></div>
<p><b>Conclusion</b></p>

<p>The CASE shortcuts can be very useful in coding scenarios but note that only COALESCE is ANSI standard. IIF and CHOOSE are added to the T-SQL 2012 language to ease the migration from MS Access to SQL Server.</p><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/MSSQLServer/t-sql-case-functions">Original post</a> blogged on <a href="http://lessthandot.com/">LessThanDot</a>.</small></p></div>]]></content:encoded>
								<comments>http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/MSSQLServer/t-sql-case-functions#comments</comments>
			<wfw:commentRss>http://blogs.lessthandot.com/index.php/DataMgmt/?tempskin=_rss2&#38;disp=comments&#38;p=1815</wfw:commentRss>
		</item>
			</channel>
</rss>
