<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title> &#187; SQL 2005</title>
	<atom:link href="http://benchmarkitconsulting.com/tag/sql-2005/feed/" rel="self" type="application/rss+xml" />
	<link>http://benchmarkitconsulting.com</link>
	<description></description>
	<lastBuildDate>Tue, 17 Jan 2012 15:11:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Another Reason SQL 2000 should just go away!   :)</title>
		<link>http://benchmarkitconsulting.com/colin-stasiuk/2009/07/02/another-reason-sql-2000-should-just-go-away/</link>
		<comments>http://benchmarkitconsulting.com/colin-stasiuk/2009/07/02/another-reason-sql-2000-should-just-go-away/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 21:01:13 +0000</pubDate>
		<dc:creator>Colin Stasiuk</dc:creator>
				<category><![CDATA[Benchmark IT Consulting]]></category>
		<category><![CDATA[Colin Stasiuk]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[bitwise]]></category>
		<category><![CDATA[DMV]]></category>
		<category><![CDATA[log file]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL 2000]]></category>
		<category><![CDATA[SQL 2005]]></category>
		<category><![CDATA[SQL 2008]]></category>
		<category><![CDATA[Transaction Log]]></category>

		<guid isPermaLink="false">http://benchmarkitconsulting.com/?p=785</guid>
		<description><![CDATA[Quick without looking it up&#8230; what does this script do: SELECT * FROM   dbo.sysfiles  WHERE  (status &#38; 0x40) &#60;&#62; 0 So if you&#8217;re like me and don&#8217;t have all the bit compares memorized for all the tables you probably didn&#8217;t know that this is how (in SQL 2000) you would query the current database to find all the transaction log files.  Every now and [...]]]></description>
			<content:encoded><![CDATA[<p>Quick without looking it up&#8230; what does this script do:</p>
<p><code style="font-size: 12px;"><span style="color:blue">SELECT </span><span style="color:gray">*<br />
</span><span style="color:blue">FROM   </span><span style="color:black">dbo.sysfiles <br />
</span><span style="color:blue">WHERE  </span><span style="color:gray">(</span><span style="color:black">status </span><span style="color:gray">&amp; </span><span style="color:black">0x40</span><span style="color:gray">) &lt;&gt; </span><span style="color:black">0</span></code></p>
<p>So if you&#8217;re like me and don&#8217;t have all the bit compares memorized for all the tables you probably didn&#8217;t know that this is how (in SQL 2000) you would query the current database to find all the transaction log files.  </p>
<p>Every now and again I take for granted the major jump that was SQL 2000 to SQL 2005 and little things like this just show me how much happier I am working in a SQL 2005/2008 environment. </p>
<p>Let me channel my inner Naughty By Nature here:</p>
<p>You down with DMV&#8230;. yeah you know me<br />
You down with DMV&#8230; yeah you know me</p>
<p>(Sorry I&#8217;m still hurting from not winning <a href="http://sqlfool.com/2009/06/sql-rap-contest-results/" target="_blank">SQL Fool&#8217;s Rap Contest </a> )</p>
<p><span style="text-decoration: line-through;">DAMN YOU</span>  My humble congratulations to <a href="http://twitter.com/way0utwest" target="_blank">Steve &#8220;Flavor Flav&#8221; Jones</a></p>
<p><img class="alignnone size-full wp-image-786" title="stevejones" src="http://benchmarkitconsulting.com/wp-content/uploads/2009/07/stevejones.jpg" alt="stevejones" width="93" height="223" /></p>
<p>Now if you were to come across this code:</p>
<p><code style="font-size: 12px;"><span style="color:blue">SELECT&nbsp;</span><span style="color:gray">*&nbsp;<br />
<br /></span><span style="color:blue">FROM&nbsp;&nbsp;&nbsp;</span><span style="color:black">sys.database_files<br />
<br /></span><span style="color:blue">WHERE&nbsp;&nbsp;</span><span style="color:black">type_desc&nbsp;</span><span style="color:blue">=&nbsp;</span><span style="color:red">&#39;LOG&#39;<br />
<br /></span></code></p>
<p>Wouldn&#8217;t you have a much better idea as to what the query is trying to accomplish?</p>
<p>Anyways just another (albeit small) reason why I&#8217;m glad to be leaving SQL 2000 in the past.</p>
<p>Enjoy!!</p>
<p><span><a href="http://benchmarkitconsulting.com" target="_blank"><img class="alignnone size-full wp-image-402" title="benchmark_sm" src="http://benchmarkitconsulting.com/wp-content/uploads/2009/02/benchmark_sm.jpg" alt="" width="157" height="74" /></a><a href="http://sqlserverpedia.com/wiki/Editors#Colin_Stasiuk" target="_blank"><img src="http://sqlserverpedia.com/badges/SQLServerPedia_Badge_Blogger.jpg" alt="" width="120" height="60" /> </a></span></p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Another+Reason+SQL+2000+should+just+go+away%21+++%3A%29+http://tinyurl.com/m4nn42" title="Post to Twitter"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-twitter-big4.png" alt="Post to Twitter" /></a> <a class="tt" href="http://delicious.com/post?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/07/02/another-reason-sql-2000-should-just-go-away/&amp;title=Another+Reason+SQL+2000+should+just+go+away%21+++%3A%29" title="Post to Delicious"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-delicious-big4.png" alt="Post to Delicious" /></a> <a class="tt" href="http://digg.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/07/02/another-reason-sql-2000-should-just-go-away/&amp;title=Another+Reason+SQL+2000+should+just+go+away%21+++%3A%29" title="Post to Digg"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-digg-big4.png" alt="Post to Digg" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/07/02/another-reason-sql-2000-should-just-go-away/&amp;title=Another+Reason+SQL+2000+should+just+go+away%21+++%3A%29" title="Post to StumbleUpon"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-su-big4.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://benchmarkitconsulting.com/colin-stasiuk/2009/07/02/another-reason-sql-2000-should-just-go-away/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Server 2005 Edition Upgrade</title>
		<link>http://benchmarkitconsulting.com/colin-stasiuk/2009/06/03/sql-server-2005-edition-upgrade/</link>
		<comments>http://benchmarkitconsulting.com/colin-stasiuk/2009/06/03/sql-server-2005-edition-upgrade/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 16:30:35 +0000</pubDate>
		<dc:creator>Colin Stasiuk</dc:creator>
				<category><![CDATA[Benchmark IT Consulting]]></category>
		<category><![CDATA[Colin Stasiuk]]></category>
		<category><![CDATA[Maintenance]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Edition]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL 2005]]></category>
		<category><![CDATA[Upgrade]]></category>

		<guid isPermaLink="false">http://benchmarkitconsulting.com/?p=738</guid>
		<description><![CDATA[As most of my posts are inspired by stuff I&#8217;ve been working on or have worked on recently this is no different. A request came in to upgrade a SQL Server 2005 Standard Edition Instance to a SQL Server 2005 Enterprise Edition Instance.  Funny thing is that I&#8217;ve been working with SQL Server 2005 since [...]]]></description>
			<content:encoded><![CDATA[<p>As most of my posts are inspired by stuff I&#8217;ve been working on or have worked on recently this is no different.</p>
<p>A request came in to upgrade a SQL Server 2005 Standard Edition Instance to a SQL Server 2005 Enterprise Edition Instance.  Funny thing is that I&#8217;ve been working with SQL Server 2005 since it&#8217;s release and haven&#8217;t had to do this before&#8230; weird&#8230; so I setup a test box with 2005 STD to play around with and mounted myself a 2005 ENT Edition ISO and ran the GUI setup.</p>
<p>After a bit the wizard told me that in order to upgrade my edition of SQL Server I needed to install via command line and use the <strong>SKUUPGRADE</strong> switch.</p>
<p>So I googled myself some Books Online goodness cause installing SQL from the command line is not something I do everyday LOL and found this:</p>
<p><strong>How to: Install SQL Server 2005 from the Command Prompt<br />
</strong><a href="http://msdn.microsoft.com/en-us/library/ms144259(SQL.90).aspx" target="_blank">http://msdn.microsoft.com/en-us/library/ms144259(SQL.90).aspx</a></p>
<p> <span style="color: #0000ff;">[SKUUPGRADE]<br />
Use the SKUUPGRADE parameter when upgrading from one edition of SQL Server to another edition of SQL Server.</span></p>
<p><span style="color: #0000ff;"><strong>Important: <br />
If you use the SKUUPGRADE parameter, Setup will remove all hotfix and service pack updates from the SQL Server instance being upgraded. Once the edition upgrade is complete, you must reapply all hotfix and service pack updates.<br />
</strong> </span></p>
<p><span style="color: #0000ff;">To perform an edition upgrade of a default instance of SQL Server (SQL Engine only) via unattended Setup, run the following command using either /qb or /qn:</span></p>
<p><span style="color: #0000ff;">start /wait setup.exe ADDLOCAL=SQL_Engine INSTANCENAME=MSSQLSERVER UPGRADE=SQL_Engine SKUUPGRADE=1 /qb</span></p>
<p><span style="color: #0000ff;">The /qb switch allows display of basic Setup dialog boxes. Error messages are also displayed.</span></p>
<p><span style="color: #0000ff;">The /qn switch suppresses all Setup dialog boxes and error messages. If the /qn switch is specified, all Setup messages, including error messages, are written to Setup log files.</span></p>
<p> </p>
<p> So before doing this I took note of the Service Pack + Hotfix level so that I could reapply them after the edition upgrade was complete.</p>
<p>I ran the script as indicated (after a full backup of course) and it worked like a charm.  After reapplying the service pack + hotfixes required to get me back to the same version the users tested and all went as expected.</p>
<p>Not sure how I avoided doing an edition upgrade in the last 5 years or so but it was fun to do something with SQL that was new to me.</p>
<p>and just in case you missed it&#8230;</p>
<p><span style="color: #0000ff;"><strong>Important: <br />
If you use the SKUUPGRADE parameter, Setup will remove all hotfix and service pack updates from the SQL Server instance being upgraded. Once the edition upgrade is complete, you must reapply all hotfix and service pack updates.</strong></span></p>
<p> </p>
<p>Enjoy!!</p>
<p><span><a href="http://benchmarkitconsulting.com" target="_blank"><img class="alignnone size-full wp-image-402" title="benchmark_sm" src="http://benchmarkitconsulting.com/wp-content/uploads/2009/02/benchmark_sm.jpg" alt="" width="157" height="74" /></a><a href="http://sqlserverpedia.com/wiki/Editors#Colin_Stasiuk" target="_blank"><img src="http://sqlserverpedia.com/badges/SQLServerPedia_Badge_Blogger.jpg" alt="" width="120" height="60" /> </a></span></p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=SQL+Server+2005+Edition+Upgrade+http://tinyurl.com/ofg678" title="Post to Twitter"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-twitter-big4.png" alt="Post to Twitter" /></a> <a class="tt" href="http://delicious.com/post?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/06/03/sql-server-2005-edition-upgrade/&amp;title=SQL+Server+2005+Edition+Upgrade" title="Post to Delicious"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-delicious-big4.png" alt="Post to Delicious" /></a> <a class="tt" href="http://digg.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/06/03/sql-server-2005-edition-upgrade/&amp;title=SQL+Server+2005+Edition+Upgrade" title="Post to Digg"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-digg-big4.png" alt="Post to Digg" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/06/03/sql-server-2005-edition-upgrade/&amp;title=SQL+Server+2005+Edition+Upgrade" title="Post to StumbleUpon"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-su-big4.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://benchmarkitconsulting.com/colin-stasiuk/2009/06/03/sql-server-2005-edition-upgrade/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pivot&#8230;. I said PIVOT!!!!</title>
		<link>http://benchmarkitconsulting.com/colin-stasiuk/2009/01/28/pivot-i-said-pivot/</link>
		<comments>http://benchmarkitconsulting.com/colin-stasiuk/2009/01/28/pivot-i-said-pivot/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 14:55:15 +0000</pubDate>
		<dc:creator>Colin Stasiuk</dc:creator>
				<category><![CDATA[Benchmark IT Consulting]]></category>
		<category><![CDATA[Colin Stasiuk]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[PIVOT]]></category>
		<category><![CDATA[SQL 2005]]></category>

		<guid isPermaLink="false">http://benchmarkitconsulting.com/?p=359</guid>
		<description><![CDATA[Apparently I must of skipped this entry in BOL when I was going through and memorizing it LOL&#8230; I had a &#8220;brain fart&#8221; yesterday when I tried to use the PIVOT function and I just couldn&#39;t get the thing to work. I had used it in the past and thought maybe it was just late [...]]]></description>
			<content:encoded><![CDATA[<p>Apparently I must of skipped <a href="http://msdn.microsoft.com/en-us/library/ms177410.aspx" target="_blank">this entry </a>in BOL when I was going through and memorizing it LOL&#8230; I had a &#8220;brain fart&#8221; yesterday when I tried to use the PIVOT function and I just couldn&#39;t get the thing to work. I had used it in the past and thought maybe it was just late in the day and I was missing something in my format of my code. As it ends up it was nothing to do with my code and EVERYTHING to do with the compatibility level of my database (80). PIVOT is only available for databases that are at 90 compatibility mode or higher.</p>
<p>&#8220;When PIVOT and UNPIVOT are used against databases that are upgraded to SQL Server 2005 or later, the compatibility level of the database must be set to 90 or higher.&#8221;</p>
<p>Here is a good example (taken from <a href="http://jdixon.dotnetdevelopersjournal.com/pivot_table_data_in_sql_server_2000_and_2005.htm" target="_blank">Jerry Dixon&#39;s blog</a> about the same subject) of how to do a PIVOT if you&#39;re in 80 (SQL2000) compatibility mode or 90/100 (SQL2005/SQL2008) compatibility mode. </p>
<p><strong>SQL 2000</strong></p>
<p> <code style="font-size: 12px;"><span style="color: #0000ff;">SELECT<br />
     </span><span style="color: #ff00ff;">SUM</span><span style="color: #808080;">(</span><span style="color: #ff00ff;">CASE </span><span style="color: #0000ff;">WHEN </span><span style="color: #ff00ff;">MONTH</span><span style="color: #808080;">(</span><span style="color: #000000;">OrderDate</span><span style="color: #808080;">) </span><span style="color: #0000ff;">= </span><span style="color: #000000;">1 </span><span style="color: #0000ff;">THEN </span><span style="color: #000000;">1 </span><span style="color: #0000ff;">END</span><span style="color: #808080;">) </span><span style="color: #0000ff;">AS </span><span style="color: #ff0000;">&#39;January&#39;<br />
     </span><span style="color: #808080;">,</span><span style="color: #ff00ff;">SUM</span><span style="color: #808080;">(</span><span style="color: #ff00ff;">CASE </span><span style="color: #0000ff;">WHEN </span><span style="color: #ff00ff;">MONTH</span><span style="color: #808080;">(</span><span style="color: #000000;">OrderDate</span><span style="color: #808080;">) </span><span style="color: #0000ff;">= </span><span style="color: #000000;">2 </span><span style="color: #0000ff;">THEN </span><span style="color: #000000;">1 </span><span style="color: #0000ff;">END</span><span style="color: #808080;">) </span><span style="color: #0000ff;">AS </span><span style="color: #ff0000;">&#39;February&#39;<br />
     </span><span style="color: #808080;">,</span><span style="color: #ff00ff;">SUM</span><span style="color: #808080;">(</span><span style="color: #ff00ff;">CASE </span><span style="color: #0000ff;">WHEN </span><span style="color: #ff00ff;">MONTH</span><span style="color: #808080;">(</span><span style="color: #000000;">OrderDate</span><span style="color: #808080;">) </span><span style="color: #0000ff;">= </span><span style="color: #000000;">3 </span><span style="color: #0000ff;">THEN </span><span style="color: #000000;">1 </span><span style="color: #0000ff;">END</span><span style="color: #808080;">) </span><span style="color: #0000ff;">AS </span><span style="color: #ff0000;">&#39;March&#39;<br />
     </span><span style="color: #808080;">,</span><span style="color: #ff00ff;">SUM</span><span style="color: #808080;">(</span><span style="color: #ff00ff;">CASE </span><span style="color: #0000ff;">WHEN </span><span style="color: #ff00ff;">MONTH</span><span style="color: #808080;">(</span><span style="color: #000000;">OrderDate</span><span style="color: #808080;">) </span><span style="color: #0000ff;">= </span><span style="color: #000000;">4 </span><span style="color: #0000ff;">THEN </span><span style="color: #000000;">1 </span><span style="color: #0000ff;">END</span><span style="color: #808080;">) </span><span style="color: #0000ff;">AS </span><span style="color: #ff0000;">&#39;April&#39;<br />
     </span><span style="color: #808080;">,</span><span style="color: #ff00ff;">SUM</span><span style="color: #808080;">(</span><span style="color: #ff00ff;">CASE </span><span style="color: #0000ff;">WHEN </span><span style="color: #ff00ff;">MONTH</span><span style="color: #808080;">(</span><span style="color: #000000;">OrderDate</span><span style="color: #808080;">) </span><span style="color: #0000ff;">= </span><span style="color: #000000;">5 </span><span style="color: #0000ff;">THEN </span><span style="color: #000000;">1 </span><span style="color: #0000ff;">END</span><span style="color: #808080;">) </span><span style="color: #0000ff;">AS </span><span style="color: #ff0000;">&#39;May&#39;<br />
     </span><span style="color: #808080;">,</span><span style="color: #ff00ff;">SUM</span><span style="color: #808080;">(</span><span style="color: #ff00ff;">CASE </span><span style="color: #0000ff;">WHEN </span><span style="color: #ff00ff;">MONTH</span><span style="color: #808080;">(</span><span style="color: #000000;">OrderDate</span><span style="color: #808080;">) </span><span style="color: #0000ff;">= </span><span style="color: #000000;">6 </span><span style="color: #0000ff;">THEN </span><span style="color: #000000;">1 </span><span style="color: #0000ff;">END</span><span style="color: #808080;">) </span><span style="color: #0000ff;">AS </span><span style="color: #ff0000;">&#39;June&#39;<br />
     </span><span style="color: #808080;">,</span><span style="color: #ff00ff;">SUM</span><span style="color: #808080;">(</span><span style="color: #ff00ff;">CASE </span><span style="color: #0000ff;">WHEN </span><span style="color: #ff00ff;">MONTH</span><span style="color: #808080;">(</span><span style="color: #000000;">OrderDate</span><span style="color: #808080;">) </span><span style="color: #0000ff;">= </span><span style="color: #000000;">7 </span><span style="color: #0000ff;">THEN </span><span style="color: #000000;">1 </span><span style="color: #0000ff;">END</span><span style="color: #808080;">) </span><span style="color: #0000ff;">AS </span><span style="color: #ff0000;">&#39;July&#39;<br />
     </span><span style="color: #808080;">,</span><span style="color: #ff00ff;">SUM</span><span style="color: #808080;">(</span><span style="color: #ff00ff;">CASE </span><span style="color: #0000ff;">WHEN </span><span style="color: #ff00ff;">MONTH</span><span style="color: #808080;">(</span><span style="color: #000000;">OrderDate</span><span style="color: #808080;">) </span><span style="color: #0000ff;">= </span><span style="color: #000000;">8 </span><span style="color: #0000ff;">THEN </span><span style="color: #000000;">1 </span><span style="color: #0000ff;">END</span><span style="color: #808080;">) </span><span style="color: #0000ff;">AS </span><span style="color: #ff0000;">&#39;August&#39;<br />
     </span><span style="color: #808080;">,</span><span style="color: #ff00ff;">SUM</span><span style="color: #808080;">(</span><span style="color: #ff00ff;">CASE </span><span style="color: #0000ff;">WHEN </span><span style="color: #ff00ff;">MONTH</span><span style="color: #808080;">(</span><span style="color: #000000;">OrderDate</span><span style="color: #808080;">) </span><span style="color: #0000ff;">= </span><span style="color: #000000;">9 </span><span style="color: #0000ff;">THEN </span><span style="color: #000000;">1 </span><span style="color: #0000ff;">END</span><span style="color: #808080;">) </span><span style="color: #0000ff;">AS </span><span style="color: #ff0000;">&#39;September&#39;<br />
     </span><span style="color: #808080;">,</span><span style="color: #ff00ff;">SUM</span><span style="color: #808080;">(</span><span style="color: #ff00ff;">CASE </span><span style="color: #0000ff;">WHEN </span><span style="color: #ff00ff;">MONTH</span><span style="color: #808080;">(</span><span style="color: #000000;">OrderDate</span><span style="color: #808080;">) </span><span style="color: #0000ff;">= </span><span style="color: #000000;">10 </span><span style="color: #0000ff;">THEN </span><span style="color: #000000;">1 </span><span style="color: #0000ff;">END</span><span style="color: #808080;">) </span><span style="color: #0000ff;">AS </span><span style="color: #ff0000;">&#39;October&#39;<br />
     </span><span style="color: #808080;">,</span><span style="color: #ff00ff;">SUM</span><span style="color: #808080;">(</span><span style="color: #ff00ff;">CASE </span><span style="color: #0000ff;">WHEN </span><span style="color: #ff00ff;">MONTH</span><span style="color: #808080;">(</span><span style="color: #000000;">OrderDate</span><span style="color: #808080;">) </span><span style="color: #0000ff;">= </span><span style="color: #000000;">11 </span><span style="color: #0000ff;">THEN </span><span style="color: #000000;">1 </span><span style="color: #0000ff;">END</span><span style="color: #808080;">) </span><span style="color: #0000ff;">AS </span><span style="color: #ff0000;">&#39;November&#39;<br />
     </span><span style="color: #808080;">,</span><span style="color: #ff00ff;">SUM</span><span style="color: #808080;">(</span><span style="color: #ff00ff;">CASE </span><span style="color: #0000ff;">WHEN </span><span style="color: #ff00ff;">MONTH</span><span style="color: #808080;">(</span><span style="color: #000000;">OrderDate</span><span style="color: #808080;">) </span><span style="color: #0000ff;">= </span><span style="color: #000000;">12 </span><span style="color: #0000ff;">THEN </span><span style="color: #000000;">1 </span><span style="color: #0000ff;">END</span><span style="color: #808080;">) </span><span style="color: #0000ff;">AS </span><span style="color: #ff0000;">&#39;December&#39;<br />
</span><span style="color: #0000ff;">FROM </span><span style="color: #000000;">Sales.SalesOrderHeader<br />
</span><span style="color: #0000ff;">WHERE </span><span style="color: #ff00ff;">YEAR</span><span style="color: #808080;">(</span><span style="color: #000000;">OrderDate</span><span style="color: #808080;">) </span><span style="color: #0000ff;">= </span><span style="color: #000000;">2002<br />
</span></code></p>
<p><strong>SQL 2005+</strong></p>
<p><code style="font-size: 12px;"><span style="color:blue">SELECT <br />&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:black">[1] </span><span style="color:blue">AS </span><span style="color:red">&#39;January&#39;<br />&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:gray">,</span><span style="color:black">[2] </span><span style="color:blue">AS </span><span style="color:red">&#39;February&#39;<br />&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:gray">,</span><span style="color:black">[3] </span><span style="color:blue">AS </span><span style="color:red">&#39;March&#39;<br />&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:gray">,</span><span style="color:black">[4] </span><span style="color:blue">AS </span><span style="color:red">&#39;April&#39;<br />&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:gray">,</span><span style="color:black">[5] </span><span style="color:blue">AS </span><span style="color:red">&#39;May&#39;<br />&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:gray">,</span><span style="color:black">[6] </span><span style="color:blue">AS </span><span style="color:red">&#39;June&#39;<br />&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:gray">,</span><span style="color:black">[7] </span><span style="color:blue">AS </span><span style="color:red">&#39;July&#39;<br />&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:gray">,</span><span style="color:black">[8] </span><span style="color:blue">AS </span><span style="color:red">&#39;August&#39;<br />&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:gray">,</span><span style="color:black">[9] </span><span style="color:blue">AS </span><span style="color:red">&#39;September&#39;<br />&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:gray">,</span><span style="color:black">[10] </span><span style="color:blue">AS </span><span style="color:red">&#39;October&#39;<br />&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:gray">,</span><span style="color:black">[11] </span><span style="color:blue">AS </span><span style="color:red">&#39;November&#39;<br />&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:gray">,</span><span style="color:black">[12] </span><span style="color:blue">AS </span><span style="color:red">&#39;December&#39;<br /></span><span style="color:blue">FROM </span><span style="color:gray">(<br />&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue">SELECT <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:magenta">MONTH</span><span style="color:gray">(</span><span style="color:black">OrderDate</span><span style="color:gray">)&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue">AS </span><span style="color:red">&#39;MonthNumber&#39;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:gray">,</span><span style="color:black">OrderID<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:blue">FROM </span><span style="color:black">Sales.SalesOrderHeader<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:blue">WHERE </span><span style="color:magenta">YEAR</span><span style="color:gray">(</span><span style="color:black">OrderDate</span><span style="color:gray">) </span><span style="color:blue">= </span><span style="color:black">2002<br />&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:gray">) </span><span style="color:blue">AS </span><span style="color:black">Data<br />PIVOT</span><span style="color:gray">(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:magenta">COUNT</span><span style="color:gray">(</span><span style="color:black">OrderID</span><span style="color:gray">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:blue">FOR </span><span style="color:black">MonthNumber </span><span style="color:blue">IN</span><span style="color:gray">(</span><span style="color:black">[1]</span><span style="color:gray">,</span><span style="color:black">[2]</span><span style="color:gray">,</span><span style="color:black">[3]</span><span style="color:gray">,</span><span style="color:black">[4]</span><span style="color:gray">,</span><span style="color:black">[5]</span><span style="color:gray">,</span><span style="color:black">[6]</span><span style="color:gray">,</span><span style="color:black">[7]</span><span style="color:gray">,</span><span style="color:black">[8]</span><span style="color:gray">,</span><span style="color:black">[9]</span><span style="color:gray">,</span><span style="color:black">[10]</span><span style="color:gray">,</span><span style="color:black">[11]</span><span style="color:gray">,</span><span style="color:black">[12]</span><span style="color:gray">)<br />&nbsp;&nbsp;&nbsp;&nbsp; ) </span><span style="color:blue">AS </span><span style="color:black">PVT</p>
<p></span></code></p>
<p><a href="http://sqlserverpedia.com/wiki/Editors#Colin_Stasiuk"><br />
<img src="http://sqlserverpedia.com/badges/SQLServerPedia_Badge_Contributor.jpg"><br />
</a></p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=Pivot%26%238230%3B.+I+said+PIVOT%21%21%21%21+http://tinyurl.com/br5kbq" title="Post to Twitter"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-twitter-big4.png" alt="Post to Twitter" /></a> <a class="tt" href="http://delicious.com/post?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/01/28/pivot-i-said-pivot/&amp;title=Pivot%26%238230%3B.+I+said+PIVOT%21%21%21%21" title="Post to Delicious"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-delicious-big4.png" alt="Post to Delicious" /></a> <a class="tt" href="http://digg.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/01/28/pivot-i-said-pivot/&amp;title=Pivot%26%238230%3B.+I+said+PIVOT%21%21%21%21" title="Post to Digg"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-digg-big4.png" alt="Post to Digg" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/01/28/pivot-i-said-pivot/&amp;title=Pivot%26%238230%3B.+I+said+PIVOT%21%21%21%21" title="Post to StumbleUpon"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-su-big4.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://benchmarkitconsulting.com/colin-stasiuk/2009/01/28/pivot-i-said-pivot/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>db_Executor Role &#8211; updated</title>
		<link>http://benchmarkitconsulting.com/colin-stasiuk/2009/01/27/db_executor-role/</link>
		<comments>http://benchmarkitconsulting.com/colin-stasiuk/2009/01/27/db_executor-role/#comments</comments>
		<pubDate>Tue, 27 Jan 2009 14:06:33 +0000</pubDate>
		<dc:creator>Colin Stasiuk</dc:creator>
				<category><![CDATA[Benchmark IT Consulting]]></category>
		<category><![CDATA[Colin Stasiuk]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[db_executor]]></category>
		<category><![CDATA[SQL 2005]]></category>

		<guid isPermaLink="false">http://benchmarkitconsulting.com/?p=352</guid>
		<description><![CDATA[In a passing conversation the question about how you grant execute to all stored procedures to a user came up. In SQL 2005 the ability to grant the execute permission at the database level was introduced. Instead of having to build some &#39;smarts&#39; into a script to cycle through all your objects, etc you can [...]]]></description>
			<content:encoded><![CDATA[<p>In a passing conversation the question about how you grant execute to all stored procedures to a user came up.  In SQL 2005 the ability to grant the execute permission at the database level was introduced.  Instead of having to build some &#39;smarts&#39; into a script to cycle through all your objects, etc you can now solve this problem as easy as:</p>
<p><code style="font-size: 12px;"><span style="color:blue">CREATE </span><span style="color:black">ROLE db_executor<br /></span><span style="color:blue">GRANT EXECUTE TO </span><span style="color:black">db_executor<br /></span><span style="color:blue">EXEC </span><span style="color:darkred">sp_addrolemember </span><span style="color:red">&#39;db_executor&#39;</span><span style="color:gray">, </span><span style="color:red">&#39;username&#39;</p>
<p></span></code></p>
<p>So this is an &#8220;all or nothing&#8221; script so if you&#39;re wanting to be selective with which stored procedures you want to grant the permission to than this is not the solution for you&#8230; but head on over to <a href="http://facility9.com/2009/01/26/grant-execute-permissions-on-all-stored-procedures-to-a-single-user/" target="_blank">facility9</a> for a look at how you can grant execute on a &#8220;pattern&#8221; of stored procedures.</p>
<p>UPDATE:  As pointed out by <a href="http://www.truthsolutions.com/" target="_blank">K Brian Kelley</a> you might end up with more work in doing this if later down the road there is a stored procedure that you don&#39;t want the user to be able to execute. He suggests (and how can you NOT trust an MVP) granting execute not at the database level but at the schema level for more control over what the user can and cannot execute&#8230; very sound advice&#8230; Thanks!!</p>
<p><a href="http://sqlserverpedia.com/wiki/Editors#Colin_Stasiuk"><br />
<img src="http://sqlserverpedia.com/badges/SQLServerPedia_Badge_Contributor.jpg"><br />
</a></p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=db_Executor+Role+%26%238211%3B+updated+http://tinyurl.com/dloa4g" title="Post to Twitter"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-twitter-big4.png" alt="Post to Twitter" /></a> <a class="tt" href="http://delicious.com/post?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/01/27/db_executor-role/&amp;title=db_Executor+Role+%26%238211%3B+updated" title="Post to Delicious"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-delicious-big4.png" alt="Post to Delicious" /></a> <a class="tt" href="http://digg.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/01/27/db_executor-role/&amp;title=db_Executor+Role+%26%238211%3B+updated" title="Post to Digg"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-digg-big4.png" alt="Post to Digg" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2009/01/27/db_executor-role/&amp;title=db_Executor+Role+%26%238211%3B+updated" title="Post to StumbleUpon"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-su-big4.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://benchmarkitconsulting.com/colin-stasiuk/2009/01/27/db_executor-role/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Versions, Editions, and Features</title>
		<link>http://benchmarkitconsulting.com/colin-stasiuk/2008/11/07/sql-versions-editions-and-features/</link>
		<comments>http://benchmarkitconsulting.com/colin-stasiuk/2008/11/07/sql-versions-editions-and-features/#comments</comments>
		<pubDate>Fri, 07 Nov 2008 15:21:17 +0000</pubDate>
		<dc:creator>Colin Stasiuk</dc:creator>
				<category><![CDATA[Benchmark IT Consulting]]></category>
		<category><![CDATA[Colin Stasiuk]]></category>
		<category><![CDATA[Release Information]]></category>
		<category><![CDATA[Edition]]></category>
		<category><![CDATA[Feature]]></category>
		<category><![CDATA[SQL 2000]]></category>
		<category><![CDATA[SQL 2005]]></category>
		<category><![CDATA[SQL 2008]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Version]]></category>

		<guid isPermaLink="false">http://benchmarkitconsulting.com/?p=77</guid>
		<description><![CDATA[When putting forth a recommendation for a version and edition of SQL Server to install it is important that you understand what features are available in each version and edition. SQL 2000 &#8211; http://msdn.microsoft.com/en-us/library/aa175266.aspx SQL 2005 &#8211; http://msdn.microsoft.com/en-us/library/ms143761(SQL.90).aspx SQL 2008 &#8211; http://msdn.microsoft.com/en-us/library/cc645993.aspx These 3 websites give you a very detailed look into each version and [...]]]></description>
			<content:encoded><![CDATA[<p>When putting forth a recommendation for a version and edition of SQL Server to install it is important that you understand what features are available in each version and edition.</p>
<p>SQL 2000 &#8211; <a href="http://msdn.microsoft.com/en-us/library/aa175266.aspx">http://msdn.microsoft.com/en-us/library/aa175266.aspx</a><br />
SQL 2005 &#8211; <a href="http://msdn.microsoft.com/en-us/library/ms143761(SQL.90).aspx">http://msdn.microsoft.com/en-us/library/ms143761(SQL.90).aspx</a><br />
SQL 2008 &#8211; <a href="http://msdn.microsoft.com/en-us/library/cc645993.aspx">http://msdn.microsoft.com/en-us/library/cc645993.aspx</a></p>
<p>These 3 websites give you a very detailed look into each version and edition of SQL Server and the features avaliable for each. It always easiest to recommend the newest and hottest for your customers but without evaluating their needs and cross referencing that with the feature lists above they may be spending more money then they need.</p>
<p>(that being said I don&#8217;t think recommending a SQL Server 2000 solution would be a great idea either <img src='http://benchmarkitconsulting.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  hehehe )</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=SQL+Versions%2C+Editions%2C+and+Features+http://tinyurl.com/bm4oyk" title="Post to Twitter"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-twitter-big4.png" alt="Post to Twitter" /></a> <a class="tt" href="http://delicious.com/post?url=http://benchmarkitconsulting.com/colin-stasiuk/2008/11/07/sql-versions-editions-and-features/&amp;title=SQL+Versions%2C+Editions%2C+and+Features" title="Post to Delicious"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-delicious-big4.png" alt="Post to Delicious" /></a> <a class="tt" href="http://digg.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2008/11/07/sql-versions-editions-and-features/&amp;title=SQL+Versions%2C+Editions%2C+and+Features" title="Post to Digg"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-digg-big4.png" alt="Post to Digg" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2008/11/07/sql-versions-editions-and-features/&amp;title=SQL+Versions%2C+Editions%2C+and+Features" title="Post to StumbleUpon"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-su-big4.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://benchmarkitconsulting.com/colin-stasiuk/2008/11/07/sql-versions-editions-and-features/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Server 2005 Service Pack 3 BETA released</title>
		<link>http://benchmarkitconsulting.com/colin-stasiuk/2008/11/04/sql-server-2005-service-pack-3-beta-released/</link>
		<comments>http://benchmarkitconsulting.com/colin-stasiuk/2008/11/04/sql-server-2005-service-pack-3-beta-released/#comments</comments>
		<pubDate>Tue, 04 Nov 2008 23:30:33 +0000</pubDate>
		<dc:creator>Colin Stasiuk</dc:creator>
				<category><![CDATA[Benchmark IT Consulting]]></category>
		<category><![CDATA[Colin Stasiuk]]></category>
		<category><![CDATA[Release Information]]></category>
		<category><![CDATA[Beta]]></category>
		<category><![CDATA[Service Pack]]></category>
		<category><![CDATA[SQL 2005]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://benchmarkitconsulting.com/?p=25</guid>
		<description><![CDATA[SQL Server 2005 Service Pack 3 BETA is now available. THIS IS A BETA so use with caution. I would advise against applying it to any production servers until the official release of Service Pack 3 and you have fully tested it in your DEV and UAT environments. Download SQL 2005 SP3 BETA Release Notes]]></description>
			<content:encoded><![CDATA[<p>SQL Server 2005 Service Pack 3 BETA is now available.</p>
<p>THIS IS A BETA so use with caution. I would advise against applying it to any production servers until the official release of Service Pack 3 and you have fully tested it in your DEV and UAT environments.</p>
<p><a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=D22317E1-BC64-4936-A14B-7A632B50A4CA&amp;displaylang=en" target="_blank"><span style="color: #336699;">Download SQL 2005 SP3 BETA</span></a></p>
<p><a href="http://download.microsoft.com/download/0/e/5/0e5697be-028a-4da6-89a7-c18a9a1ac3c7/WhatsNewSQLServer2005SP3.htm" target="_blank"><span style="color: #336699;">Release Notes</span></a></p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=SQL+Server+2005+Service+Pack+3+BETA+released+http://tinyurl.com/bt752s" title="Post to Twitter"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-twitter-big4.png" alt="Post to Twitter" /></a> <a class="tt" href="http://delicious.com/post?url=http://benchmarkitconsulting.com/colin-stasiuk/2008/11/04/sql-server-2005-service-pack-3-beta-released/&amp;title=SQL+Server+2005+Service+Pack+3+BETA+released" title="Post to Delicious"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-delicious-big4.png" alt="Post to Delicious" /></a> <a class="tt" href="http://digg.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2008/11/04/sql-server-2005-service-pack-3-beta-released/&amp;title=SQL+Server+2005+Service+Pack+3+BETA+released" title="Post to Digg"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-digg-big4.png" alt="Post to Digg" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2008/11/04/sql-server-2005-service-pack-3-beta-released/&amp;title=SQL+Server+2005+Service+Pack+3+BETA+released" title="Post to StumbleUpon"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-su-big4.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://benchmarkitconsulting.com/colin-stasiuk/2008/11/04/sql-server-2005-service-pack-3-beta-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8220;Why can&#8217;t I see the script behind the stored procedure?&#8221;</title>
		<link>http://benchmarkitconsulting.com/colin-stasiuk/2008/11/04/why-cant-i-see-the-script-behind-the-stored-procedure/</link>
		<comments>http://benchmarkitconsulting.com/colin-stasiuk/2008/11/04/why-cant-i-see-the-script-behind-the-stored-procedure/#comments</comments>
		<pubDate>Tue, 04 Nov 2008 23:28:12 +0000</pubDate>
		<dc:creator>Colin Stasiuk</dc:creator>
				<category><![CDATA[Benchmark IT Consulting]]></category>
		<category><![CDATA[Colin Stasiuk]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Metadata]]></category>
		<category><![CDATA[permissions]]></category>
		<category><![CDATA[SQL 2005]]></category>
		<category><![CDATA[SQL 2008]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://benchmarkitconsulting.com/?p=23</guid>
		<description><![CDATA[ummm I dunno&#8230; why can&#8217;t you see the script behind the stored procedure? I can see it just fine&#8230;. then again I&#8217;m sysadmin so if I can&#8217;t see it then there is definitely a problem. Turns out&#8230; after all this time with working in SQL 2005 I still learn new things or at least things [...]]]></description>
			<content:encoded><![CDATA[<p>ummm I dunno&#8230; why can&#8217;t you see the script behind the stored procedure? I can see it just fine&#8230;. then again I&#8217;m sysadmin so if I can&#8217;t see it then there is definitely a problem.</p>
<p>Turns out&#8230; after all this time with working in SQL 2005 I still learn new things or at least things that I&#8217;ve long since forgotten. In production developers have db_datareader access and usually nothing more then that (it&#8217;s even more then I think it should be but I usually lose that battle depending on the sensitivity of the data). That being said why shouldn&#8217;t a developer have access to view the code or metadata behind the objects within a database? I can understand protecting the metadata from normal users but I can&#8217;t think of a good reason why developers should be locked out from this&#8230; Turns out that in SQL 2005 you need to actually grant access to do this.</p>
<p>If you want to mimick what was available in SQL 2000 the following 2 statements need to be ran:<br />
 <code style="font-size: 12px;"><span style="color: #0000ff;">GRANT VIEW </span><span style="color: #808080;">ANY </span><span style="color: #000000;">DEFINITION </span><span style="color: #0000ff;">TO PUBLIC</span></code></p>
<p>This allows all server logins to view any metadata in any database<br />
<code style="font-size: 12px;"><span style="color: #0000ff;">GRANT VIEW </span><span style="color: #000000;">SERVER STATE </span><span style="color: #0000ff;">TO PUBLIC</span></code></p>
<p>This allows all server logins the ability to see the DMV (Dynamic Management Views) which have replaced the SQL 2000 virtual tables like sysprocesses.</p>
<p>So access to metadata is a privilege by default in SQL 2005. I personally like the approach of meeting somewhere in the middle of SQL 2000 and SQL 2005. I think developers should have the same access and visibility they had in SQL 2000 but &#8220;public&#8221; shouldn&#8217;t be used. I would grant the access to a group something like this&#8230;</p>
<p><code style="font-size: 12px;"><span style="color: #0000ff;">GRANT VIEW </span><span style="color: #808080;">ANY </span><span style="color: #000000;">DEFINITION </span><span style="color: #0000ff;">TO DOMAIN\Group</span></code><br />
<code style="font-size: 12px;"><span style="color: #0000ff;">GRANT VIEW </span><span style="color: #000000;">SERVER STATE </span><span style="color: #0000ff;">TO DOMAIN\Group</span></code></p>
<p><a href="http://msdn.microsoft.com/en-us/library/ms175808.aspx" target="_blank"><span style="color: #336699;">Click here</span></a> for further information on View Definition Permissions</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=%26%238220%3BWhy+can%26%238217%3Bt+I+see+the+script+behind+the+stored+procedure%3F%26%238221%3B+http://tinyurl.com/c2ttmk" title="Post to Twitter"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-twitter-big4.png" alt="Post to Twitter" /></a> <a class="tt" href="http://delicious.com/post?url=http://benchmarkitconsulting.com/colin-stasiuk/2008/11/04/why-cant-i-see-the-script-behind-the-stored-procedure/&amp;title=%26%238220%3BWhy+can%26%238217%3Bt+I+see+the+script+behind+the+stored+procedure%3F%26%238221%3B" title="Post to Delicious"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-delicious-big4.png" alt="Post to Delicious" /></a> <a class="tt" href="http://digg.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2008/11/04/why-cant-i-see-the-script-behind-the-stored-procedure/&amp;title=%26%238220%3BWhy+can%26%238217%3Bt+I+see+the+script+behind+the+stored+procedure%3F%26%238221%3B" title="Post to Digg"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-digg-big4.png" alt="Post to Digg" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2008/11/04/why-cant-i-see-the-script-behind-the-stored-procedure/&amp;title=%26%238220%3BWhy+can%26%238217%3Bt+I+see+the+script+behind+the+stored+procedure%3F%26%238221%3B" title="Post to StumbleUpon"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-su-big4.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://benchmarkitconsulting.com/colin-stasiuk/2008/11/04/why-cant-i-see-the-script-behind-the-stored-procedure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8220;Smart&#8221; Reindexing (SQL Server 2005, 2008)</title>
		<link>http://benchmarkitconsulting.com/colin-stasiuk/2008/11/04/smart-reindexing-sql-server-2005-2008/</link>
		<comments>http://benchmarkitconsulting.com/colin-stasiuk/2008/11/04/smart-reindexing-sql-server-2005-2008/#comments</comments>
		<pubDate>Tue, 04 Nov 2008 23:07:04 +0000</pubDate>
		<dc:creator>Colin Stasiuk</dc:creator>
				<category><![CDATA[Benchmark IT Consulting]]></category>
		<category><![CDATA[Colin Stasiuk]]></category>
		<category><![CDATA[Maintenance]]></category>
		<category><![CDATA[Indexing]]></category>
		<category><![CDATA[SQL 2005]]></category>
		<category><![CDATA[SQL 2008]]></category>

		<guid isPermaLink="false">http://benchmarkitconsulting.com/?p=9</guid>
		<description><![CDATA[Something that still baffles me about SQL Server is that even in SQL Server 2008 there is no maintenance plan option to do reindexing properly. The key word in that sentence is properly. SQL Server gives you an all or nothing approach to indexing. You have 2 options: 1) Rebuild Indexes 2) Reorganize Indexes (Great [...]]]></description>
			<content:encoded><![CDATA[<p>Something that still baffles me about SQL Server is that even in SQL Server 2008 there is no maintenance plan option to do reindexing properly. The key word in that sentence is properly. SQL Server gives you an all or nothing approach to indexing. You have 2 options:</p>
<p>1) Rebuild Indexes</p>
<p>2) Reorganize Indexes</p>
<p>(Great article on the differences between the two : <a href="http://decipherinfosys.wordpress.com/2008/02/07/index-rebuilding-vs-index-reorganizing-in-sql-server-2005/" target="_blank"><span style="color: #336699;">http://decipherinfosys.wordpress.com/2008/02/07/index-rebuilding-vs-index-reorganizing-in-sql-server-2005/</span></a> )</p>
<p>If you were to do this through the wizard you can choose individual objects and there are plenty of options to customize but where this falls down is that it does not take into account whether or not an index needs maintenance performed on it.</p>
<p>In SQL Server 2005 DMVs (Dynamic Management Views) were introduced that gave instant access to alot of vital information that prior to SQL Server 2005 wasn&#8217;t available or required separate process to gather (anyone remember DBCC SHOWCONTIG?).</p>
<p>Let&#8217;s talk about the DMV <strong><a href="http://msdn.microsoft.com/en-us/library/ms188917.aspx" target="_blank"><span style="color: #336699;">sys.dm_db_index_physical_stats</span></a></strong>. &#8220;Returns size and fragmentation information for the data and indexes of the specified table or view.&#8221; So you would think that with this information readily available that the maintenance plan would have an option to do only the indexes that needed maintenance? nope.</p>
<p>Below is a script that I have been working with for SQL Server 2005 and SQL Server 2008. Because SQL Server 2000 did not have DMVs this script will not work (I have a different script for SQL Server 2000 that I&#8217;ll post another time)</p>
<p>The long and the short of what this script does is that it will utilize the sys.dm_db_index_physical_stats DMV and dynamically determine if an index requires maintenance and performs the maintenace.</p>
<p><strong><span style="text-decoration: underline;">Variables that require attention</span></strong><br />
<strong>@FragThreshold</strong> &#8211; This integer value is the minimum percentage that an index is fragmented before maintenance is done</p>
<p><strong>@DoOfflineToo</strong> &#8211; This bit value indicates whether or not indexes that need to be done offline are done at the same time. This is important because if an index needs to be maintained offline this requires significant locks to the object. (Also please be aware that online index operations are only available on Enterprise or Developer Edition)</p>
<p>This script needs to be run in the context of the database that you are looking to maintain. I usually create an SSIS package and wrap this script in a ForEach loop container that cycles through all the databases but for ease of &#8220;blogging&#8221; we&#8217;ll look at it from a more manual approach.</p>
<p>The &#8220;sizzle&#8221; of the script is when it decides what to do with the index that is currently being looked at.<br />
<code style="font-size: 12px;"><span style="color:blue">DECLARE </span><span style="color:#434343">@FragThreshold </span><span style="color:blue">INT<br />DECLARE </span><span style="color:#434343">@DoOfflineToo </span><span style="color:black">bit</p>
<p></span><span style="color:green">---------------------------------------------------------------<br />--				VARIABLES TO CHANGE							 --<br />---------------------------------------------------------------</p>
<p></span><span style="color:blue">SELECT </span><span style="color:#434343">@FragThreshold </span><span style="color:blue">= </span><span style="color:black">10<br /></span><span style="color:blue">SELECT&nbsp;&nbsp;</span><span style="color:#434343">@DoOfflineToo </span><span style="color:blue">= </span><span style="color:black">0</p>
<p></span><span style="color:green">---------------------------------------------------------------<br />---------------------------------------------------------------</p>
<p></span><span style="color:blue">DECLARE </span><span style="color:#434343">@objectid </span><span style="color:blue">INT<br />DECLARE </span><span style="color:#434343">@indexid </span><span style="color:blue">INT<br />DECLARE </span><span style="color:#434343">@partitioncount </span><span style="color:black">bigint<br /></span><span style="color:blue">DECLARE </span><span style="color:#434343">@schemaname </span><span style="color:blue">VARCHAR</span><span style="color:gray">(</span><span style="color:black">130</span><span style="color:gray">)<br /></span><span style="color:blue">DECLARE </span><span style="color:#434343">@objectname </span><span style="color:blue">VARCHAR</span><span style="color:gray">(</span><span style="color:black">130</span><span style="color:gray">)<br /></span><span style="color:blue">DECLARE </span><span style="color:#434343">@indexname </span><span style="color:blue">VARCHAR</span><span style="color:gray">(</span><span style="color:black">130</span><span style="color:gray">)<br /></span><span style="color:blue">DECLARE </span><span style="color:#434343">@indextype </span><span style="color:blue">INT<br />DECLARE </span><span style="color:#434343">@partitionnum </span><span style="color:black">bigint<br /></span><span style="color:blue">DECLARE </span><span style="color:#434343">@partitions </span><span style="color:black">bigint<br /></span><span style="color:blue">DECLARE </span><span style="color:#434343">@frag </span><span style="color:blue">INT<br />DECLARE </span><span style="color:#434343">@command </span><span style="color:blue">VARCHAR</span><span style="color:gray">(</span><span style="color:black">4000</span><span style="color:gray">)<br /></span><span style="color:blue">DECLARE </span><span style="color:#434343">@max </span><span style="color:blue">INT<br />DECLARE </span><span style="color:#434343">@min </span><span style="color:blue">INT<br />DECLARE </span><span style="color:#434343">@DB_ID </span><span style="color:blue">INT<br />DECLARE </span><span style="color:#434343">@DB_NAME </span><span style="color:blue">NVARCHAR</span><span style="color:gray">(</span><span style="color:black">128</span><span style="color:gray">)<br /></span><span style="color:blue">DECLARE </span><span style="color:#434343">@IndexListCount </span><span style="color:blue">INT<br />DECLARE </span><span style="color:#434343">@Edition </span><span style="color:blue">NVARCHAR</span><span style="color:gray">(</span><span style="color:black">100</span><span style="color:gray">)</p>
<p></span><span style="color:blue">DECLARE </span><span style="color:#434343">@LobCount </span><span style="color:blue">INT<br />SELECT </span><span style="color:#434343">@LobCount </span><span style="color:blue">= </span><span style="color:black">0<br /></span><span style="color:blue">SELECT </span><span style="color:#434343">@DB_ID </span><span style="color:blue">= </span><span style="color:magenta">DB_ID</span><span style="color:gray">()<br /></span><span style="color:blue">SELECT </span><span style="color:#434343">@DB_NAME </span><span style="color:blue">= </span><span style="color:magenta">DB_NAME</span><span style="color:gray">()<br /></span><span style="color:blue">SELECT&nbsp;&nbsp;</span><span style="color:#434343">@Edition </span><span style="color:blue">= </span><span style="color:magenta">CONVERT</span><span style="color:gray">(</span><span style="color:blue">NVARCHAR</span><span style="color:gray">(</span><span style="color:black">100</span><span style="color:gray">), </span><span style="color:magenta">SERVERPROPERTY </span><span style="color:gray">(</span><span style="color:red">&#39;edition&#39;</span><span style="color:gray">)) </p>
<p></span><span style="color:blue">CREATE TABLE </span><span style="color:#434343">#BenchmarkIndexList </span><span style="color:gray">(</span><span style="color:black">ID </span><span style="color:blue">INT </span><span style="color:#434343">IDENTITY</span><span style="color:gray">(</span><span style="color:black">1</span><span style="color:gray">,</span><span style="color:black">1</span><span style="color:gray">), <br />&nbsp;&nbsp; </span><span style="color:black">DBName </span><span style="color:blue">VARCHAR</span><span style="color:gray">(</span><span style="color:black">100</span><span style="color:gray">), <br />&nbsp;&nbsp; </span><span style="color:black">objectID </span><span style="color:blue">INT</span><span style="color:gray">,<br />&nbsp;&nbsp; </span><span style="color:black">indexID </span><span style="color:blue">INT</span><span style="color:gray">,<br />&nbsp;&nbsp; </span><span style="color:black">IndexType </span><span style="color:blue">VARCHAR</span><span style="color:gray">(</span><span style="color:black">30</span><span style="color:gray">),<br />&nbsp;&nbsp; </span><span style="color:black">frag </span><span style="color:blue">INT</span><span style="color:gray">,<br />&nbsp;&nbsp; </span><span style="color:black">avg_frag_size_pages </span><span style="color:blue">INT</span><span style="color:gray">,<br />&nbsp;&nbsp; </span><span style="color:black">page_count </span><span style="color:blue">INT</span><span style="color:gray">,<br />&nbsp;&nbsp; </span><span style="color:black">partition_number </span><span style="color:blue">INT</span><span style="color:gray">)</p>
<p></span><span style="color:blue">CREATE CLUSTERED INDEX </span><span style="color:black">IX_CL_IndexList </span><span style="color:blue">ON </span><span style="color:#434343">#BenchmarkIndexList</span><span style="color:gray">(</span><span style="color:black">ID</span><span style="color:gray">)</p>
<p></span><span style="color:blue">INSERT </span><span style="color:#434343">#BenchmarkIndexList </span><span style="color:gray">(</span><span style="color:black">DBName</span><span style="color:gray">, </span><span style="color:black">objectID</span><span style="color:gray">, </span><span style="color:black">indexID</span><span style="color:gray">, </span><span style="color:black">IndexType</span><span style="color:gray">, </span><span style="color:black">frag</span><span style="color:gray">, <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:black">avg_frag_size_pages</span><span style="color:gray">, </span><span style="color:black">page_count</span><span style="color:gray">, </span><span style="color:black">partition_number</span><span style="color:gray">)<br /></span><span style="color:blue">SELECT </span><span style="color:magenta">DB_NAME</span><span style="color:gray">(</span><span style="color:black">database_id</span><span style="color:gray">) </span><span style="color:blue">AS </span><span style="color:black">DBName</span><span style="color:gray">, </span><span style="color:magenta">OBJECT_ID</span><span style="color:gray">, </span><span style="color:black">index_id</span><span style="color:gray">, </span><span style="color:black">index_type_desc </span><span style="color:blue">AS </span><span style="color:red">&#39;IndexType&#39;</span><span style="color:gray">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:black">avg_fragmentation_in_percent</span><span style="color:gray">, </span><span style="color:black">avg_fragment_size_in_pages</span><span style="color:gray">, </span><span style="color:black">page_count</span><span style="color:gray">, </span><span style="color:black">partition_number<br /></span><span style="color:blue">FROM&nbsp;&nbsp; </span><span style="color:black">sys.dm_db_index_physical_stats </span><span style="color:gray">(</span><span style="color:#434343">@DB_ID</span><span style="color:gray">, NULL, NULL, NULL, </span><span style="color:red">&#39;LIMITED&#39;</span><span style="color:gray">)<br /></span><span style="color:blue">WHERE&nbsp;&nbsp;</span><span style="color:black">page_count </span><span style="color:gray">&gt; </span><span style="color:black">10 </span><span style="color:gray">AND <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:black">index_id </span><span style="color:gray">&gt; </span><span style="color:black">0 </span><span style="color:gray">AND <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:black">avg_fragmentation_in_percent </span><span style="color:gray">&gt; </span><span style="color:#434343">@FragThreshold </span><span style="color:gray">AND<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:black">index_type_desc </span><span style="color:gray">NOT LIKE </span><span style="color:red">&#39;%XML%&#39;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br /></span><span style="color:blue">SELECT </span><span style="color:#434343">@IndexListCount </span><span style="color:blue">= </span><span style="color:magenta">COUNT</span><span style="color:gray">(</span><span style="color:black">1</span><span style="color:gray">) </span><span style="color:blue">FROM </span><span style="color:#434343">#BenchmarkIndexList</p>
<p></span><span style="color:blue">SELECT </span><span style="color:#434343">@max </span><span style="color:blue">= </span><span style="color:gray">(</span><span style="color:blue">SELECT </span><span style="color:magenta">MAX</span><span style="color:gray">(</span><span style="color:black">ID</span><span style="color:gray">) </span><span style="color:blue">FROM </span><span style="color:#434343">#BenchmarkIndexList</span><span style="color:gray">)<br /></span><span style="color:blue">SELECT </span><span style="color:#434343">@min </span><span style="color:blue">= </span><span style="color:black">1<br /></span><span style="color:blue">WHILE </span><span style="color:#434343">@min </span><span style="color:gray">&lt;= </span><span style="color:#434343">@max<br /></span><span style="color:blue">BEGIN<br />&nbsp;&nbsp; SELECT&nbsp;&nbsp;</span><span style="color:#434343">@objectid </span><span style="color:blue">= </span><span style="color:black">objectID</span><span style="color:gray">, </span><span style="color:#434343">@indexid </span><span style="color:blue">= </span><span style="color:black">indexID</span><span style="color:gray">, </span><span style="color:#434343">@partitionnum </span><span style="color:blue">= </span><span style="color:black">partition_number</span><span style="color:gray">, </span><span style="color:#434343">@frag </span><span style="color:blue">= </span><span style="color:black">frag<br />&nbsp;&nbsp; </span><span style="color:blue">FROM&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:#434343">#BenchmarkIndexList<br />&nbsp;&nbsp; </span><span style="color:blue">WHERE&nbsp;&nbsp; </span><span style="color:black">ID </span><span style="color:blue">= </span><span style="color:#434343">@min</p>
<p>&nbsp;&nbsp; </span><span style="color:blue">SELECT&nbsp;&nbsp;</span><span style="color:#434343">@objectname </span><span style="color:blue">= </span><span style="color:black">o.name</span><span style="color:gray">, </span><span style="color:#434343">@schemaname </span><span style="color:blue">= </span><span style="color:black">s.name<br />&nbsp;&nbsp; </span><span style="color:blue">FROM&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:black">sys.objects </span><span style="color:blue">AS </span><span style="color:black">o </span><span style="color:blue">JOIN <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:black">sys.schemas </span><span style="color:blue">AS </span><span style="color:black">s </span><span style="color:blue">ON </span><span style="color:black">s.schema_id </span><span style="color:blue">= </span><span style="color:black">o.schema_id<br />&nbsp;&nbsp; </span><span style="color:blue">WHERE&nbsp;&nbsp; </span><span style="color:black">o.</span><span style="color:magenta">OBJECT_ID </span><span style="color:blue">= </span><span style="color:#434343">@objectid</p>
<p>&nbsp;&nbsp; </span><span style="color:blue">SELECT&nbsp;&nbsp;</span><span style="color:#434343">@indexname </span><span style="color:blue">= </span><span style="color:black">name</span><span style="color:gray">, </span><span style="color:#434343">@indextype </span><span style="color:blue">= TYPE<br />&nbsp;&nbsp; FROM&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:black">sys.indexes<br />&nbsp;&nbsp; </span><span style="color:blue">WHERE&nbsp;&nbsp; </span><span style="color:magenta">OBJECT_ID </span><span style="color:blue">= </span><span style="color:#434343">@objectid </span><span style="color:gray">AND <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:black">index_id </span><span style="color:blue">= </span><span style="color:#434343">@indexid</span><span style="color:gray">;</p>
<p>&nbsp;&nbsp; </span><span style="color:blue">SELECT&nbsp;&nbsp;</span><span style="color:#434343">@partitioncount </span><span style="color:blue">= </span><span style="color:magenta">COUNT</span><span style="color:gray">(</span><span style="color:black">1</span><span style="color:gray">)<br />&nbsp;&nbsp; </span><span style="color:blue">FROM&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:black">sys.partitions<br />&nbsp;&nbsp; </span><span style="color:blue">WHERE&nbsp;&nbsp; </span><span style="color:magenta">OBJECT_ID </span><span style="color:blue">= </span><span style="color:#434343">@objectid </span><span style="color:gray">AND <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:black">index_id </span><span style="color:blue">= </span><span style="color:#434343">@indexid<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;&nbsp; </span><span style="color:blue">SELECT&nbsp;&nbsp;</span><span style="color:#434343">@LobCount </span><span style="color:blue">= </span><span style="color:magenta">COUNT</span><span style="color:gray">(</span><span style="color:black">1</span><span style="color:gray">)<br />&nbsp;&nbsp; </span><span style="color:blue">FROM&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color:black">sys.index_columns A </span><span style="color:blue">INNER JOIN<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:black">sys.columns B </span><span style="color:blue">ON </span><span style="color:black">A.</span><span style="color:magenta">OBJECT_ID </span><span style="color:blue">= </span><span style="color:black">B.</span><span style="color:magenta">OBJECT_ID </span><span style="color:gray">AND </span><span style="color:black">A.Column_id </span><span style="color:blue">= </span><span style="color:black">B.Column_ID </span><span style="color:blue">INNER JOIN<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:black">sys.types C </span><span style="color:blue">ON </span><span style="color:black">B.system_type_id </span><span style="color:blue">= </span><span style="color:black">C.user_type_id<br />&nbsp;&nbsp; </span><span style="color:blue">WHERE&nbsp;&nbsp; </span><span style="color:black">A.</span><span style="color:magenta">OBJECT_ID </span><span style="color:blue">= </span><span style="color:#434343">@objectID </span><span style="color:gray">AND<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:black">A.index_id </span><span style="color:blue">= </span><span style="color:#434343">@indexID </span><span style="color:gray">AND <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (</span><span style="color:black">C.name </span><span style="color:blue">IN</span><span style="color:gray">(</span><span style="color:red">&#39;xml&#39;</span><span style="color:gray">,</span><span style="color:red">&#39;image&#39;</span><span style="color:gray">,</span><span style="color:red">&#39;text&#39;</span><span style="color:gray">,</span><span style="color:red">&#39;ntext&#39;</span><span style="color:gray">) OR<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (</span><span style="color:black">C.name </span><span style="color:blue">IN</span><span style="color:gray">(</span><span style="color:red">&#39;varchar&#39;</span><span style="color:gray">,</span><span style="color:red">&#39;nvarchar&#39;</span><span style="color:gray">,</span><span style="color:red">&#39;varbinary&#39;</span><span style="color:gray">,</span><span style="color:red">&#39;nvarbinary&#39;</span><span style="color:gray">) AND <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:black">B.max_length </span><span style="color:blue">= </span><span style="color:gray">-</span><span style="color:black">1</span><span style="color:gray">))</p>
<p>&nbsp;&nbsp; </span><span style="color:blue">IF </span><span style="color:#434343">@frag </span><span style="color:gray">&lt; </span><span style="color:#434343">@FragThreshold<br />&nbsp;&nbsp; </span><span style="color:blue">BEGIN<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SET </span><span style="color:#434343">@command </span><span style="color:blue">= </span><span style="color:red">&#39;ALTER INDEX &#39; </span><span style="color:gray">+ </span><span style="color:#434343">@indexname </span><span style="color:gray">+ </span><span style="color:red">&#39; ON &#39; </span><span style="color:gray">+ </span><span style="color:#434343">@schemaname </span><span style="color:gray">+ </span><span style="color:red">&#39;.&#39; </span><span style="color:gray">+ </span><span style="color:#434343">@objectname </span><span style="color:gray">+ </span><span style="color:red">&#39; REORGANIZE&#39;</span><span style="color:gray">;<br />&nbsp;&nbsp; </span><span style="color:blue">END</p>
<p>&nbsp;&nbsp; IF </span><span style="color:#434343">@frag </span><span style="color:gray">&gt;= </span><span style="color:#434343">@FragThreshold </span><span style="color:gray">AND </span><span style="color:#434343">@IndexType </span><span style="color:gray">&lt;&gt; </span><span style="color:black">1 </span><span style="color:gray">AND </span><span style="color:#434343">@LobCount </span><span style="color:blue">= </span><span style="color:black">0 </span><span style="color:gray">AND </span><span style="color:#434343">@Edition </span><span style="color:blue">= </span><span style="color:red">&#39;Enterprise Edition&#39;<br />&nbsp;&nbsp; </span><span style="color:blue">BEGIN<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SET </span><span style="color:#434343">@command </span><span style="color:blue">= </span><span style="color:red">&#39;ALTER INDEX &#39; </span><span style="color:gray">+ </span><span style="color:#434343">@indexname </span><span style="color:gray">+ </span><span style="color:red">&#39; ON &#39; </span><span style="color:gray">+ </span><span style="color:#434343">@schemaname </span><span style="color:gray">+ </span><span style="color:red">&#39;.&#39; </span><span style="color:gray">+ </span><span style="color:#434343">@objectname </span><span style="color:gray">+</span><span style="color:red">&#39; REBUILD WITH(ONLINE = ON, SORT_IN_TEMPDB = ON)&#39;</span><span style="color:gray">;<br />&nbsp;&nbsp; </span><span style="color:blue">END</p>
<p>&nbsp;&nbsp; IF </span><span style="color:#434343">@frag </span><span style="color:gray">&gt;= </span><span style="color:#434343">@FragThreshold </span><span style="color:gray">AND </span><span style="color:#434343">@DoOfflineToo </span><span style="color:blue">= </span><span style="color:black">1 </span><span style="color:gray">AND (</span><span style="color:#434343">@IndexType </span><span style="color:blue">= </span><span style="color:black">1 </span><span style="color:gray">OR </span><span style="color:#434343">@LobCount </span><span style="color:gray">&lt;&gt; </span><span style="color:black">0 </span><span style="color:gray">OR </span><span style="color:#434343">@Edition </span><span style="color:gray">&lt;&gt; </span><span style="color:red">&#39;Enterprise Edition&#39;</span><span style="color:gray">)<br />&nbsp;&nbsp; </span><span style="color:blue">BEGIN<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SET </span><span style="color:#434343">@command </span><span style="color:blue">= </span><span style="color:red">&#39;ALTER INDEX &#39; </span><span style="color:gray">+ </span><span style="color:#434343">@indexname </span><span style="color:gray">+ </span><span style="color:red">&#39; ON &#39; </span><span style="color:gray">+ </span><span style="color:#434343">@schemaname </span><span style="color:gray">+ </span><span style="color:red">&#39;.&#39; </span><span style="color:gray">+ </span><span style="color:#434343">@objectname </span><span style="color:gray">+</span><span style="color:red">&#39; REBUILD WITH(ONLINE = OFF, SORT_IN_TEMPDB = ON)&#39;</span><span style="color:gray">;<br />&nbsp;&nbsp; </span><span style="color:blue">END<br />&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IF </span><span style="color:#434343">@frag </span><span style="color:gray">&gt;= </span><span style="color:#434343">@FragThreshold </span><span style="color:gray">AND </span><span style="color:#434343">@DoOfflineToo </span><span style="color:blue">= </span><span style="color:black">0 </span><span style="color:gray">AND (</span><span style="color:#434343">@IndexType </span><span style="color:blue">= </span><span style="color:black">1 </span><span style="color:gray">OR </span><span style="color:#434343">@LobCount </span><span style="color:gray">&lt;&gt; </span><span style="color:black">0 </span><span style="color:gray">OR </span><span style="color:#434343">@Edition </span><span style="color:gray">&lt;&gt; </span><span style="color:red">&#39;Enterprise Edition&#39;</span><span style="color:gray">)<br />&nbsp;&nbsp; </span><span style="color:blue">BEGIN<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SET </span><span style="color:#434343">@command </span><span style="color:blue">= </span><span style="color:red">&#39;ALTER INDEX &#39; </span><span style="color:gray">+ </span><span style="color:#434343">@indexname </span><span style="color:gray">+ </span><span style="color:red">&#39; ON &#39; </span><span style="color:gray">+ </span><span style="color:#434343">@schemaname </span><span style="color:gray">+ </span><span style="color:red">&#39;.&#39; </span><span style="color:gray">+ </span><span style="color:#434343">@objectname </span><span style="color:gray">+ </span><span style="color:red">&#39; REORGANIZE&#39;</span><span style="color:gray">;<br />&nbsp;&nbsp; </span><span style="color:blue">END</p>
<p>&nbsp;&nbsp; IF </span><span style="color:#434343">@partitioncount </span><span style="color:gray">&gt; </span><span style="color:black">1<br />&nbsp;&nbsp; </span><span style="color:blue">BEGIN<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SET </span><span style="color:#434343">@command </span><span style="color:blue">= </span><span style="color:#434343">@command </span><span style="color:gray">+ </span><span style="color:red">&#39; PARTITION =&#39; </span><span style="color:gray">+ </span><span style="color:magenta">CAST</span><span style="color:gray">(</span><span style="color:#434343">@partitionnum </span><span style="color:blue">AS VARCHAR</span><span style="color:gray">(</span><span style="color:black">10</span><span style="color:gray">));<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue">EXEC </span><span style="color:gray">(</span><span style="color:#434343">@command</span><span style="color:gray">)<br />&nbsp;&nbsp; </span><span style="color:blue">END</p>
<p>&nbsp;&nbsp; ELSE<br />&nbsp;&nbsp; BEGIN <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EXEC </span><span style="color:gray">(</span><span style="color:#434343">@command</span><span style="color:gray">)<br />&nbsp;&nbsp; </span><span style="color:blue">END<br />&nbsp;&nbsp; <br />&nbsp;&nbsp; SET </span><span style="color:#434343">@min </span><span style="color:blue">= </span><span style="color:#434343">@min</span><span style="color:gray">+</span><span style="color:black">1</p>
<p></span><span style="color:blue">END</p>
<p>DROP TABLE </span><span style="color:#434343">#BenchmarkIndexList</p>
<p></span></code></p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=%26%238220%3BSmart%26%238221%3B+Reindexing+%28SQL+Server+2005%2C+2008%29+http://tinyurl.com/blpyk4" title="Post to Twitter"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-twitter-big4.png" alt="Post to Twitter" /></a> <a class="tt" href="http://delicious.com/post?url=http://benchmarkitconsulting.com/colin-stasiuk/2008/11/04/smart-reindexing-sql-server-2005-2008/&amp;title=%26%238220%3BSmart%26%238221%3B+Reindexing+%28SQL+Server+2005%2C+2008%29" title="Post to Delicious"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-delicious-big4.png" alt="Post to Delicious" /></a> <a class="tt" href="http://digg.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2008/11/04/smart-reindexing-sql-server-2005-2008/&amp;title=%26%238220%3BSmart%26%238221%3B+Reindexing+%28SQL+Server+2005%2C+2008%29" title="Post to Digg"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-digg-big4.png" alt="Post to Digg" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://benchmarkitconsulting.com/colin-stasiuk/2008/11/04/smart-reindexing-sql-server-2005-2008/&amp;title=%26%238220%3BSmart%26%238221%3B+Reindexing+%28SQL+Server+2005%2C+2008%29" title="Post to StumbleUpon"><img class="nothumb" src="http://benchmarkitconsulting.com/wp-content/plugins/tweet-this/icons/tt-su-big4.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://benchmarkitconsulting.com/colin-stasiuk/2008/11/04/smart-reindexing-sql-server-2005-2008/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

