<?xml version="1.0" encoding="iso-8859-1"?><!-- generator="b2evolution/2.4.5" -->
<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/">
	<channel>
		<title>LessThanDot - Data Management</title>
		<link>http://blogs.lessthandot.com/index.php/DataMgmt/</link>
		<description></description>
		<language>en-GB</language>
		<docs>http://blogs.law.harvard.edu/tech/rss</docs>
		<admin:generatorAgent rdf:resource="http://b2evolution.net/?v=2.4.5"/>
		<ttl>60</ttl>
				<item>
			<title>Use Filters in SSMS to only see the things that are of interest to you</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/MSSQLServer/use-filters-in-ssms-to-only-see-the-thin</link>
			<pubDate>Sat, 06 Feb 2010 20:13:04 +0000</pubDate>			<dc:creator>SQLDenis</dc:creator>
			<category domain="alt">Database Programming</category>
<category domain="main">Microsoft SQL Server</category>			<guid isPermaLink="false">745@http://blogs.lessthandot.com/</guid>
						<description>&lt;h3&gt;by &lt;a href=&quot;http://blogs.lessthandot.com/index.php/All/?disp=authdir&amp;amp;author=4&quot;&gt; SQLDenis &lt;/a&gt;&lt;/h3&gt;&lt;p&gt;Now that all my server are SQL Server 2008 I use SSMS with SSMS Toolpack and Toad. I don't really use Query Analyzer anymore. The other day I found out that you can hide objects you don't want to see in SSMS by using filters. &lt;/p&gt;

&lt;p&gt;Let's first look at some code. Create a new database named test, create a new schema named Denis and then add 3 tables to the dbo schema and 3 tables to the Denis schema. Just run the code below&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;cb8640&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;DATABASE&lt;/span&gt; test&lt;br /&gt;go&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;USE&lt;/span&gt; test&lt;br /&gt;go&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;SCHEMA&lt;/span&gt; Denis&lt;br /&gt;go&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; Test1&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;id &lt;span style=&quot;color: #0000FF;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;go&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; Test2&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;id &lt;span style=&quot;color: #0000FF;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;go&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; Test3&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;id &lt;span style=&quot;color: #0000FF;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;go&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; Denis.&lt;span style=&quot;color: #202020;&quot;&gt;Test1&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;id &lt;span style=&quot;color: #0000FF;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;go&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; Denis.&lt;span style=&quot;color: #202020;&quot;&gt;Test2&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;id &lt;span style=&quot;color: #0000FF;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;go&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;CREATE&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; Denis.&lt;span style=&quot;color: #202020;&quot;&gt;Test3&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;id &lt;span style=&quot;color: #0000FF;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;;&lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb99920&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now in SSMS you will see the following if you expand the tables folder&lt;/p&gt;

&lt;div&gt;&lt;img src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt//AllTables.PNG&quot; alt=&quot;&quot; title=&quot;&quot; width=&quot;195&quot; height=&quot;199&quot; /&gt;&lt;/div&gt;

&lt;p&gt;What if I only want to see the tables that belong to the Denis schema? Here is how you can setup the filter. Right click on the tables folder, select Filter and then Filter settings.&lt;/p&gt;

&lt;div&gt;&lt;img src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt//FilterSettings.PNG&quot; alt=&quot;&quot; title=&quot;&quot; width=&quot;403&quot; height=&quot;196&quot; /&gt;&lt;/div&gt;

&lt;p&gt;That will pop up a window that looks like the image below&lt;/p&gt;


&lt;div&gt;&lt;img src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt//Filters.png&quot; alt=&quot;&quot; title=&quot;&quot; width=&quot;559&quot; height=&quot;448&quot; /&gt;&lt;/div&gt;

&lt;p&gt;For the Schema property select equals for the operator and type Denis as value. Click OK and now you will see the following when you navigate to the tables folder.&lt;/p&gt;



&lt;div&gt;&lt;img src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt//FilteredView.PNG&quot; alt=&quot;&quot; title=&quot;&quot; width=&quot;187&quot; height=&quot;146&quot; /&gt;&lt;/div&gt;

&lt;p&gt;If you want to remove the filter, just right click on the folder, select Filter and then Remove Filter.&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;If you work in a team and you created several tables that all start with Report then you can create a new filter and just show these objects. This way you don't need to see any tables that your team members created. Below is a screen shot of a filter like that.&lt;/p&gt;

&lt;div&gt;&lt;img src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/NewSnip.PNG&quot; alt=&quot;&quot; title=&quot;&quot; width=&quot;538&quot; height=&quot;96&quot; /&gt;&lt;/div&gt;&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Another cool feature is that you can filter on Creation Date, this gives you the ability to filter all that old &lt;a href=&quot;http://en.wikipedia.org/wiki/Brownfield_(software_development)&quot;&gt;Brownfield&lt;/a&gt; stuff that nobody maintains anyway because if you change one thing it will break everything else :-)&lt;/p&gt;

&lt;p&gt;So are you using filters in SSMS?&lt;/p&gt;


&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
*** &lt;strong&gt;Remember, if you have a SQL related question, try our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=17&quot;&gt;Microsoft SQL Server Programming&lt;/a&gt; forum or our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=22&quot;&gt;Microsoft SQL Server Admin&lt;/a&gt; forum&lt;/strong&gt;&lt;ins&gt;&lt;/ins&gt;&lt;/p&gt;&lt;a href=&quot;http://api.tweetmeme.com/share?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/MSSQLServer/use-filters-in-ssms-to-only-see-the-thin&quot;&gt;&lt;img src=&quot;http://api.tweetmeme.com/imagebutton.gif?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/MSSQLServer/use-filters-in-ssms-to-only-see-the-thin&amp;source=LessThanDot&quot; height=&quot;61&quot; width=&quot;51&quot; /&gt;&lt;/a&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/use-filters-in-ssms-to-only-see-the-thin&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[				<h3>by <a href="http://blogs.lessthandot.com/index.php/All/?disp=authdir&amp;author=4"> SQLDenis </a></h3>
				<p>Now that all my server are SQL Server 2008 I use SSMS with SSMS Toolpack and Toad. I don't really use Query Analyzer anymore. The other day I found out that you can hide objects you don't want to see in SSMS by using filters. </p>

<p>Let's first look at some code. Create a new database named test, create a new schema named Denis and then add 3 tables to the dbo schema and 3 tables to the Denis schema. Just run the code below</p>

<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb59489" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">DATABASE</span> test<br />go<br />&nbsp;<br /><span style="color: #0000FF;">USE</span> test<br />go<br />&nbsp;<br /><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">SCHEMA</span> Denis<br />go<br />&nbsp;<br /><span style="color: #0000FF;">CREATE</span> &nbsp;<span style="color: #0000FF;">TABLE</span> Test1<span style="color: #808080;">&#40;</span>id <span style="color: #0000FF;">INT</span><span style="color: #808080;">&#41;</span>;<br />go<br /><span style="color: #0000FF;">CREATE</span> &nbsp;<span style="color: #0000FF;">TABLE</span> Test2<span style="color: #808080;">&#40;</span>id <span style="color: #0000FF;">INT</span><span style="color: #808080;">&#41;</span>;<br />go<br /><span style="color: #0000FF;">CREATE</span> &nbsp;<span style="color: #0000FF;">TABLE</span> Test3<span style="color: #808080;">&#40;</span>id <span style="color: #0000FF;">INT</span><span style="color: #808080;">&#41;</span>;<br />go<br />&nbsp;<br />&nbsp;<br /><span style="color: #0000FF;">CREATE</span> &nbsp;<span style="color: #0000FF;">TABLE</span> Denis.<span style="color: #202020;">Test1</span><span style="color: #808080;">&#40;</span>id <span style="color: #0000FF;">INT</span><span style="color: #808080;">&#41;</span>;<br />go<br /><span style="color: #0000FF;">CREATE</span> &nbsp;<span style="color: #0000FF;">TABLE</span> Denis.<span style="color: #202020;">Test2</span><span style="color: #808080;">&#40;</span>id <span style="color: #0000FF;">INT</span><span style="color: #808080;">&#41;</span>;<br />go<br /><span style="color: #0000FF;">CREATE</span> &nbsp;<span style="color: #0000FF;">TABLE</span> Denis.<span style="color: #202020;">Test3</span><span style="color: #808080;">&#40;</span>id <span style="color: #0000FF;">INT</span><span style="color: #808080;">&#41;</span>;<br />go<br /><br /></div><div id="cb17761" style="display: none; color: red;"></div></div></div>

<p>Now in SSMS you will see the following if you expand the tables folder</p>

<div><img src="http://blogs.lessthandot.com/media/blogs/DataMgmt//AllTables.PNG" alt="" title="" width="195" height="199" /></div>

<p>What if I only want to see the tables that belong to the Denis schema? Here is how you can setup the filter. Right click on the tables folder, select Filter and then Filter settings.</p>

<div><img src="http://blogs.lessthandot.com/media/blogs/DataMgmt//FilterSettings.PNG" alt="" title="" width="403" height="196" /></div>

<p>That will pop up a window that looks like the image below</p>


<div><img src="http://blogs.lessthandot.com/media/blogs/DataMgmt//Filters.png" alt="" title="" width="559" height="448" /></div>

<p>For the Schema property select equals for the operator and type Denis as value. Click OK and now you will see the following when you navigate to the tables folder.</p>



<div><img src="http://blogs.lessthandot.com/media/blogs/DataMgmt//FilteredView.PNG" alt="" title="" width="187" height="146" /></div>

<p>If you want to remove the filter, just right click on the folder, select Filter and then Remove Filter.</p>

<p><br /><br /><br /></p>

<p>If you work in a team and you created several tables that all start with Report then you can create a new filter and just show these objects. This way you don't need to see any tables that your team members created. Below is a screen shot of a filter like that.</p>

<div><img src="http://blogs.lessthandot.com/media/blogs/DataMgmt/NewSnip.PNG" alt="" title="" width="538" height="96" /></div><p> </p>

<p>Another cool feature is that you can filter on Creation Date, this gives you the ability to filter all that old <a href="http://en.wikipedia.org/wiki/Brownfield_(software_development)">Brownfield</a> stuff that nobody maintains anyway because if you change one thing it will break everything else :-)</p>

<p>So are you using filters in SSMS?</p>


<p><br /><br /><br />
*** <strong>Remember, if you have a SQL related question, try our <a href="http://forum.lessthandot.com/viewforum.php?f=17">Microsoft SQL Server Programming</a> forum or our <a href="http://forum.lessthandot.com/viewforum.php?f=22">Microsoft SQL Server Admin</a> forum</strong><ins></ins></p><a href="http://api.tweetmeme.com/share?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/MSSQLServer/use-filters-in-ssms-to-only-see-the-thin"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/MSSQLServer/use-filters-in-ssms-to-only-see-the-thin&amp;source=LessThanDot" height="61" width="51" /></a><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/MSSQLServer/use-filters-in-ssms-to-only-see-the-thin">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/use-filters-in-ssms-to-only-see-the-thin#comments</comments>
		</item>
				<item>
			<title>Interesting UNION with ORDER BY behavior</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/interesting-union-with-order-by-behaviou</link>
			<pubDate>Fri, 05 Feb 2010 18:26:59 +0000</pubDate>			<dc:creator>SQLDenis</dc:creator>
			<category domain="main">Database Programming</category>
<category domain="alt">Microsoft SQL Server</category>			<guid isPermaLink="false">744@http://blogs.lessthandot.com/</guid>
						<description>&lt;h3&gt;by &lt;a href=&quot;http://blogs.lessthandot.com/index.php/All/?disp=authdir&amp;amp;author=4&quot;&gt; SQLDenis &lt;/a&gt;&lt;/h3&gt;&lt;p&gt;Here is something interesting to think about&lt;/p&gt;

&lt;p&gt;Create this table and insert these 2 rows&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;cb56250&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;TABLE&lt;/span&gt; TableName&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;id &lt;span style=&quot;color: #0000FF;&quot;&gt;INT&lt;/span&gt;, 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;50&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;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; TableName &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: #000;&quot;&gt;1&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;'bla'&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;INSERT&lt;/span&gt; TableName &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: #000;&quot;&gt;2&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;'bla2'&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;cb85732&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now if you try to do something like this&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;cb71700&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;1&lt;/span&gt; ID,Name&lt;br /&gt;&amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; TableName&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; Name&lt;br /&gt;&amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;UNION&lt;/span&gt; ALL&lt;br /&gt;&amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;''&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb79718&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;You will get the following error&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Server: Msg 156, Level 15, State 1, Line 4&lt;br /&gt;
Incorrect syntax near the keyword 'UNION'.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Interestingly, this same code will work if you use it in the following matter&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;cb38655&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;1&lt;/span&gt; *&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&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;1&lt;/span&gt; ID,Name&lt;br /&gt;&amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; TableName&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; Name&lt;br /&gt;&amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;UNION&lt;/span&gt; ALL&lt;br /&gt;&amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;''&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; X&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ORDER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; ID &lt;span style=&quot;color: #0000FF;&quot;&gt;DESC&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb61585&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This same code also works with Common Table Expressions&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;cb84747&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WITH&lt;/span&gt; query &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&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;1&lt;/span&gt; ID,Name&lt;br /&gt;&amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; TableName&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; Name&lt;br /&gt;&amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;UNION&lt;/span&gt; ALL&lt;br /&gt;&amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;''&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&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;1&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;x.&lt;span style=&quot;color: #202020;&quot;&gt;id&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;x.&lt;span style=&quot;color: #202020;&quot;&gt;name&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; query x&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ORDER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;BY&lt;/span&gt; x.&lt;span style=&quot;color: #202020;&quot;&gt;id&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DESC&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb84576&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;My assumption is that the optimizer disregards any order by in the subquery. Still I find this interesting because usually a query will work, you stick it inside another piece of code and it will complain about something&lt;/p&gt;


&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
*** &lt;strong&gt;Remember, if you have a SQL related question, try our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=17&quot;&gt;Microsoft SQL Server Programming&lt;/a&gt; forum or our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=22&quot;&gt;Microsoft SQL Server Admin&lt;/a&gt; forum&lt;/strong&gt;&lt;ins&gt;&lt;/ins&gt;&lt;/p&gt;&lt;a href=&quot;http://api.tweetmeme.com/share?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/interesting-union-with-order-by-behaviou&quot;&gt;&lt;img src=&quot;http://api.tweetmeme.com/imagebutton.gif?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/interesting-union-with-order-by-behaviou&amp;source=LessThanDot&quot; height=&quot;61&quot; width=&quot;51&quot; /&gt;&lt;/a&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/interesting-union-with-order-by-behaviou&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[				<h3>by <a href="http://blogs.lessthandot.com/index.php/All/?disp=authdir&amp;author=4"> SQLDenis </a></h3>
				<p>Here is something interesting to think about</p>

<p>Create this table and insert these 2 rows</p>

<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb510" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> TableName<span style="color: #808080;">&#40;</span>id <span style="color: #0000FF;">INT</span>, name <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">50</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span><br /><span style="color: #0000FF;">INSERT</span> TableName <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #FF0000;">'bla'</span><span style="color: #808080;">&#41;</span><br /><span style="color: #0000FF;">INSERT</span> TableName <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #000;">2</span>,<span style="color: #FF0000;">'bla2'</span><span style="color: #808080;">&#41;</span><br /><br /></div><div id="cb51243" style="display: none; color: red;"></div></div></div>

<p>Now if you try to do something like this</p>

<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb48227" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">1</span> ID,Name<br />&nbsp; &nbsp;<span style="color: #0000FF;">FROM</span> TableName<br />&nbsp; &nbsp;<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> Name<br />&nbsp; &nbsp;<span style="color: #0000FF;">UNION</span> ALL<br />&nbsp; &nbsp;<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">0</span>,<span style="color: #FF0000;">''</span><br /><br /></div><div id="cb63933" style="display: none; color: red;"></div></div></div>

<p>You will get the following error</p>

<p><strong><em>Server: Msg 156, Level 15, State 1, Line 4<br />
Incorrect syntax near the keyword 'UNION'.</em></strong></p>

<p>Interestingly, this same code will work if you use it in the following matter</p>

<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb44778" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">1</span> *<br /><span style="color: #0000FF;">FROM</span> <span style="color: #808080;">&#40;</span><br />&nbsp; &nbsp;<span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">1</span> ID,Name<br />&nbsp; &nbsp;<span style="color: #0000FF;">FROM</span> TableName<br />&nbsp; &nbsp;<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> Name<br />&nbsp; &nbsp;<span style="color: #0000FF;">UNION</span> ALL<br />&nbsp; &nbsp;<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">0</span>,<span style="color: #FF0000;">''</span><br /><span style="color: #808080;">&#41;</span> X<br /><span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> ID <span style="color: #0000FF;">DESC</span><br /><br /></div><div id="cb19012" style="display: none; color: red;"></div></div></div>

<p>This same code also works with Common Table Expressions</p>

<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb4040" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">WITH</span> query <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#40;</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">1</span> ID,Name<br />&nbsp; &nbsp;<span style="color: #0000FF;">FROM</span> TableName<br />&nbsp; &nbsp;<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> Name<br />&nbsp; &nbsp;<span style="color: #0000FF;">UNION</span> ALL<br />&nbsp; &nbsp;<span style="color: #0000FF;">SELECT</span> <span style="color: #000;">0</span>,<span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span><br />&nbsp;<br />&nbsp;<br />&nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">1</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x.<span style="color: #202020;">id</span>,<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;x.<span style="color: #202020;">name</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">FROM</span> query x<br /><span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> x.<span style="color: #202020;">id</span> <span style="color: #0000FF;">DESC</span><br /><br /></div><div id="cb42501" style="display: none; color: red;"></div></div></div>

<p>My assumption is that the optimizer disregards any order by in the subquery. Still I find this interesting because usually a query will work, you stick it inside another piece of code and it will complain about something</p>


<p><br /><br /><br />
*** <strong>Remember, if you have a SQL related question, try our <a href="http://forum.lessthandot.com/viewforum.php?f=17">Microsoft SQL Server Programming</a> forum or our <a href="http://forum.lessthandot.com/viewforum.php?f=22">Microsoft SQL Server Admin</a> forum</strong><ins></ins></p><a href="http://api.tweetmeme.com/share?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/interesting-union-with-order-by-behaviou"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/interesting-union-with-order-by-behaviou&amp;source=LessThanDot" height="61" width="51" /></a><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/interesting-union-with-order-by-behaviou">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/interesting-union-with-order-by-behaviou#comments</comments>
		</item>
				<item>
			<title>Read from your mirror in SQL Server</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/read-from-your-mirror-in-sql-server</link>
			<pubDate>Wed, 03 Feb 2010 14:13:02 +0000</pubDate>			<dc:creator>onpnt</dc:creator>
			<category domain="alt">Data Modelling &amp; Design</category>
<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">742@http://blogs.lessthandot.com/</guid>
						<description>&lt;h3&gt;by &lt;a href=&quot;http://blogs.lessthandot.com/index.php/All/?disp=authdir&amp;amp;author=68&quot;&gt; onpnt &lt;/a&gt;&lt;/h3&gt;&lt;p&gt;This will primarily be an extension into the Wiki section on &lt;a href=&quot;http://wiki.lessthandot.com/index.php/SQL_Server_Admin_Hacks&quot;&gt;SQL Server Admin Hacks&lt;/a&gt;. &lt;/p&gt;
&lt;h2&gt;OLTP and Reporting&lt;/h2&gt;
&lt;p&gt;
Reporting from OLTP (Online Transactional Processing) databases can be nothing short of difficult at times.  The process of reporting of these types on databases can cause blocking, long wait times and in some severe cases, complete failure on the part of the high level operational requirements of the database.  In the stage in which we consider how reporting will affect our databases and how to react proactively to the constant is during the architectural phases of your database server landscape.  In regards to SQL Server, this means the feature set that you weigh in for each edition available to us.  
&lt;/p&gt;
&lt;p&gt;
A perfect example of editions in SQL Server that provide us the means to better handle our environmental variables and reporting is a comparison of Enterprise to the less feature packed editions.  With the advent of SQL Server 2005 and the major overhaul of SQL Server as a database server in enterprise installations, snapshot abilities were introduced.  Of course you get what you pay for as with anything.  Snapshots are only available to date in Enterprise Edition of SQL Server.  You may complain, but I&amp;#8217;m all for the marketing of this.  There must be a reason to step up to this level and the feature set is that reason.  It is important to understand that the SQL Server engine itself does not change across the edition levels.  SQL Server in my opinion is still at each edition, a true enterprise functioning landscape with the engine and will bring great benefits to most installations.  
&lt;/p&gt;
&lt;h2&gt;Getting to the solutions&lt;/h2&gt;
&lt;p&gt;
Snapshots are the key in this article because they provide us with the ability to not only create views of data easily accessed at a point in time, but they give us the ability to read from a secondary database in mirroring.  &lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;span class=&quot;MT_red&quot;&gt;Note: snapshots are just that, &quot;snapshots&quot;.  They do not give us a real-time active ability to read data as it is committed to the database.&lt;/span&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;br /&gt;
 &lt;br /&gt;
Typically in most installations you will never be able to remove all reporting from OLTP databases.  There is still business rules that require the user community to see the state of the data as it is at the time of report execution.  This is also not a bad thing and with proper indexing and tuning, you can be very affective at limiting the foot print your reports and query executions leave on the database.  Looking over my career, I came up with an 80/20 basis of reports on OLTP databases.  This equates to, 80% point in time reporting to 20% real-time reporting.  &lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;span class=&quot;MT_red&quot;&gt;Note: when discussing reporting needs with your business, you may find yourself in the predicament that the business thinks they need real-time reporting.  Dig deep into the true business needs however and you may uncover and come to an agreement that point in time reporting successfully meets the requirements. This is one of the highest means of tuning active reports on OLTP installations.&lt;/span&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;/p&gt;

&lt;p&gt;
There are other methods that should be noted to point in time reporting including but not limited to&lt;br /&gt;
&lt;ul&gt;
  &lt;li&gt;Full Database Restores&lt;/li&gt;
  &lt;li&gt;Log Shipping&lt;/li&gt;
  &lt;li&gt;Replication (snapshot primarily)&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
&lt;br /&gt;
These three that are mentioned have their advantages and disadvantages.  Take all of them into your own planning stages as viable solutions.
&lt;/p&gt;
&lt;p&gt;
In the following setup we will work through setting up a snapshot on a mirror to answer the main question of how we can take advantage of a mirror for reporting.  This can be a powerful aspect to your reporting infrastructure and also give you expansion to the data that in early version was not available.
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;To fully show our lab and setup steps, I will be using the mirror that we setup in, &lt;a href=&quot;http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/move-databases-to-new-server-with-little-1&quot;&gt;Using mirroring to Reduce Migration Downtime (Part 1)&lt;/a&gt;.  Once we have the mirror setup, creating the actual snapshot from the secondary in the mirroring configure in a T-SQL statement.&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;cb75350&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;DATABASE&lt;/span&gt; NEEDTOMOVE_20100203072007 &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;NAME = NEEDTOMOVE,&lt;br /&gt;FILENAME = &lt;span style=&quot;color: #FF0000;&quot;&gt;'C:\NEEDTOMOVE_20100203072007.ss'&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;AS&lt;/span&gt; SNAPSHOT &lt;span style=&quot;color: #0000FF;&quot;&gt;OF&lt;/span&gt; NEEDTOMOVE&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb70064&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;p&gt;
Once you have done this the snapshot is available as a read-only, point in time representation of the mirror.  You can locate the snapshot in SSMS under the &quot;database snapshots&quot; tree located directly under the system databases section.&lt;br /&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;img src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/snaps.gif&quot; alt=&quot;&quot; title=&quot;&quot; width=&quot;290&quot; height=&quot;224&quot; /&gt;&lt;/div&gt;&lt;br /&gt;
Now any read operation can be done on the snapshot supporting any reports that meet the needs of the data that has been captured.  &lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;span class=&quot;MT_red&quot;&gt;Note: naming conventions of snapshots are typically database name joined with the point in time of the snapshot execution.  This allows an easy way to maintain which snapshot holds the data you require.  &lt;/span&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;/p&gt;
&lt;h2&gt;It depends?&lt;/h2&gt;
&lt;p&gt;
In the note I mentioned that the naming conventions typically use the date and time to show the time the snapshot was taken.  In several of my installations this isn&amp;#8217;t the case though.  For reporting the data source can be very dynamic in nature but for many situations the data requirement can simply be the state the data was given a past time.  In this case you can drop the snapshot and create it under the same name.  This simplifies the needs in your reporting from platforms such as SQL Server Reporting Services.
&lt;/p&gt;
&lt;h2&gt;Closing&lt;/h2&gt;
&lt;p&gt;
What we went over may weigh heavily on asking for the added budget to your new or upgrading strategies for SQL Server.  I hope this will help in justifying those added costs when you take the task of licensing on with your own company.
&lt;/p&gt;&lt;a href=&quot;http://api.tweetmeme.com/share?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/read-from-your-mirror-in-sql-server&quot;&gt;&lt;img src=&quot;http://api.tweetmeme.com/imagebutton.gif?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/read-from-your-mirror-in-sql-server&amp;source=LessThanDot&quot; height=&quot;61&quot; width=&quot;51&quot; /&gt;&lt;/a&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/read-from-your-mirror-in-sql-server&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[				<h3>by <a href="http://blogs.lessthandot.com/index.php/All/?disp=authdir&amp;author=68"> onpnt </a></h3>
				<p>This will primarily be an extension into the Wiki section on <a href="http://wiki.lessthandot.com/index.php/SQL_Server_Admin_Hacks">SQL Server Admin Hacks</a>. </p>
<h2>OLTP and Reporting</h2>
<p>
Reporting from OLTP (Online Transactional Processing) databases can be nothing short of difficult at times.  The process of reporting of these types on databases can cause blocking, long wait times and in some severe cases, complete failure on the part of the high level operational requirements of the database.  In the stage in which we consider how reporting will affect our databases and how to react proactively to the constant is during the architectural phases of your database server landscape.  In regards to SQL Server, this means the feature set that you weigh in for each edition available to us.  
</p>
<p>
A perfect example of editions in SQL Server that provide us the means to better handle our environmental variables and reporting is a comparison of Enterprise to the less feature packed editions.  With the advent of SQL Server 2005 and the major overhaul of SQL Server as a database server in enterprise installations, snapshot abilities were introduced.  Of course you get what you pay for as with anything.  Snapshots are only available to date in Enterprise Edition of SQL Server.  You may complain, but I&#8217;m all for the marketing of this.  There must be a reason to step up to this level and the feature set is that reason.  It is important to understand that the SQL Server engine itself does not change across the edition levels.  SQL Server in my opinion is still at each edition, a true enterprise functioning landscape with the engine and will bring great benefits to most installations.  
</p>
<h2>Getting to the solutions</h2>
<p>
Snapshots are the key in this article because they provide us with the ability to not only create views of data easily accessed at a point in time, but they give us the ability to read from a secondary database in mirroring.  <br />
<br />
<blockquote><p><span class="MT_red">Note: snapshots are just that, "snapshots".  They do not give us a real-time active ability to read data as it is committed to the database.</span></p></blockquote> <br />
 <br />
Typically in most installations you will never be able to remove all reporting from OLTP databases.  There is still business rules that require the user community to see the state of the data as it is at the time of report execution.  This is also not a bad thing and with proper indexing and tuning, you can be very affective at limiting the foot print your reports and query executions leave on the database.  Looking over my career, I came up with an 80/20 basis of reports on OLTP databases.  This equates to, 80% point in time reporting to 20% real-time reporting.  <br />
<br />
<blockquote><p><span class="MT_red">Note: when discussing reporting needs with your business, you may find yourself in the predicament that the business thinks they need real-time reporting.  Dig deep into the true business needs however and you may uncover and come to an agreement that point in time reporting successfully meets the requirements. This is one of the highest means of tuning active reports on OLTP installations.</span></p></blockquote>
</p>

<p>
There are other methods that should be noted to point in time reporting including but not limited to<br />
<ul>
  <li>Full Database Restores</li>
  <li>Log Shipping</li>
  <li>Replication (snapshot primarily)</li>
</ul><br />
<br />
These three that are mentioned have their advantages and disadvantages.  Take all of them into your own planning stages as viable solutions.
</p>
<p>
In the following setup we will work through setting up a snapshot on a mirror to answer the main question of how we can take advantage of a mirror for reporting.  This can be a powerful aspect to your reporting infrastructure and also give you expansion to the data that in early version was not available.
</p>
<p>
<p>To fully show our lab and setup steps, I will be using the mirror that we setup in, <a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/move-databases-to-new-server-with-little-1">Using mirroring to Reduce Migration Downtime (Part 1)</a>.  Once we have the mirror setup, creating the actual snapshot from the secondary in the mirroring configure in a T-SQL statement.</p>

<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb37446" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">DATABASE</span> NEEDTOMOVE_20100203072007 <span style="color: #0000FF;">ON</span><br /><span style="color: #808080;">&#40;</span><br />NAME = NEEDTOMOVE,<br />FILENAME = <span style="color: #FF0000;">'C:\NEEDTOMOVE_20100203072007.ss'</span><br /><span style="color: #808080;">&#41;</span><br /><span style="color: #0000FF;">AS</span> SNAPSHOT <span style="color: #0000FF;">OF</span> NEEDTOMOVE<br /><br /></div><div id="cb66142" style="display: none; color: red;"></div></div></div>
</p>
<p></p><p>
Once you have done this the snapshot is available as a read-only, point in time representation of the mirror.  You can locate the snapshot in SSMS under the "database snapshots" tree located directly under the system databases section.<br />
<div class="image_block"><img src="http://blogs.lessthandot.com/media/blogs/DataMgmt/snaps.gif" alt="" title="" width="290" height="224" /></div><br />
Now any read operation can be done on the snapshot supporting any reports that meet the needs of the data that has been captured.  <br />
<br />
<blockquote><p><span class="MT_red">Note: naming conventions of snapshots are typically database name joined with the point in time of the snapshot execution.  This allows an easy way to maintain which snapshot holds the data you require.  </span></p></blockquote>
</p>
<h2>It depends?</h2>
<p>
In the note I mentioned that the naming conventions typically use the date and time to show the time the snapshot was taken.  In several of my installations this isn&#8217;t the case though.  For reporting the data source can be very dynamic in nature but for many situations the data requirement can simply be the state the data was given a past time.  In this case you can drop the snapshot and create it under the same name.  This simplifies the needs in your reporting from platforms such as SQL Server Reporting Services.
</p>
<h2>Closing</h2>
<p>
What we went over may weigh heavily on asking for the added budget to your new or upgrading strategies for SQL Server.  I hope this will help in justifying those added costs when you take the task of licensing on with your own company.
</p><a href="http://api.tweetmeme.com/share?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/read-from-your-mirror-in-sql-server"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/read-from-your-mirror-in-sql-server&amp;source=LessThanDot" height="61" width="51" /></a><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/read-from-your-mirror-in-sql-server">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/read-from-your-mirror-in-sql-server#comments</comments>
		</item>
				<item>
			<title>Running static code analysis on SQL Server database with Visual Studio Team System for database architects</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/running-static-code-analysis-on-sql-serv</link>
			<pubDate>Mon, 01 Feb 2010 01:35:26 +0000</pubDate>			<dc:creator>SQLDenis</dc:creator>
			<category domain="main">Data Modelling &amp; Design</category>
<category domain="alt">Database Programming</category>
<category domain="alt">Database Administration</category>
<category domain="alt">Microsoft SQL Server Admin</category>
<category domain="alt">Microsoft SQL Server</category>			<guid isPermaLink="false">739@http://blogs.lessthandot.com/</guid>
						<description>&lt;h3&gt;by &lt;a href=&quot;http://blogs.lessthandot.com/index.php/All/?disp=authdir&amp;amp;author=4&quot;&gt; SQLDenis &lt;/a&gt;&lt;/h3&gt;&lt;p&gt;To me it seems that Visual Studio Team System 2008 Database Edition is the stepchild of the Visual Studio family. Even in shops that have MSDN Universal/Ultimate subscriptions this version is just not used that much. Maybe it is that long name of this product, I still prefer DataDude. I would like to show you that if you do have licenses for this tool then you should use it because it has some great features. Today we will focus on static code analysis.&lt;/p&gt;

&lt;p&gt;Before we start, make sure to grab Microsoft Visual Studio Team System 2008 Database Edition GDR R2&lt;br /&gt;
and install that on top of your Visual Studio Team System 2008 Database Edition. This install adds the following things to Visual Studio&lt;/p&gt;

&lt;p&gt;In addition to providing support for SQL Server 2008 database projects, this release incorporates many previously released Power Tools as well as several new features. The new features include distinct Build and Deploy phases, Static Code Analysis and improved integration with SQL CLR projects.&lt;/p&gt;

&lt;p&gt;Database Edition no longer requires a Design Database. Therefore, it is no longer necessary to install an instance of SQL Express or SQL Server prior to using Database Edition. &lt;br /&gt;
You can download  Microsoft Visual Studio Team System 2008 Database Edition GDR R2  here &lt;a href=&quot;http://www.microsoft.com/downloads/details.aspx?FamilyID=bb3ad767-5f69-4db9-b1c9-8f55759846ed&amp;amp;displaylang=en&quot;&gt;http://www.microsoft.com/downloads/details.aspx?FamilyID=bb3ad767-5f69-4db9-b1c9-8f55759846ed&amp;amp;displaylang=en&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you have everything installed, start up Visual Studio and create a new database project. You should see something similar to the pic below&lt;/p&gt;

&lt;div class=&quot;image_block&quot;&gt;&lt;img src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt//db1.png&quot; alt=&quot;&quot; title=&quot;&quot; width=&quot;630&quot; height=&quot;426&quot; /&gt;&lt;/div&gt;

&lt;p&gt;Create a new database connection, you can do that by going to  View--&gt;Server Explorer. Under Data Connections add a new connection to your database that you want to connect to, follow the wizard and create the connection. Now I decided to run static code analysis against the aspnetdb database that is used with ASP.NET. If you want to follow along and use this same database, take a look at this post&lt;a href=&quot;http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/ASPNET/setting-up-sql-server-with-asp-net-mvc&quot;&gt; Setting up SQL Server with ASP.NET MVC&lt;/a&gt; to see how to set it up&lt;/p&gt;



&lt;p&gt;Once your database is setup go to the Solution explorer, right click on the project and select Import Database Objects and Settings. &lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;img src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt//db2.png&quot; alt=&quot;&quot; title=&quot;&quot; width=&quot;276&quot; height=&quot;197&quot; /&gt;&lt;/div&gt;
&lt;p&gt;The following dialog will be shown&lt;/p&gt;

&lt;div class=&quot;image_block&quot;&gt;&lt;img src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt//db3.png&quot; alt=&quot;&quot; title=&quot;&quot; width=&quot;841&quot; height=&quot;301&quot; /&gt;&lt;/div&gt;

&lt;p&gt;Pick your connection and then click start&lt;/p&gt;

&lt;p&gt;The output will be similar to this&lt;/p&gt;

&lt;p&gt;1/30/2010 11:26:40 AM	Import of database schema has started.&lt;br /&gt;
1/30/2010 11:26:45 AM	Adding all files to the project...&lt;br /&gt;
1/30/2010 11:26:46 AM	Finished adding all files to the project.&lt;br /&gt;
1/30/2010 11:26:46 AM	Done&lt;br /&gt;
1/30/2010 11:26:46 AM	Import of database schema is complete.&lt;br /&gt;
1/30/2010 11:26:46 AM	A summary of the import operation has been saved to the log file C:\SVN\InterrogateASP\InterrogateASP\Import Schema Logs\InterrogateASP_20100130042639.log.&lt;br /&gt;
1/30/2010 11:26:46 AM	Press Finish to continue...&lt;/p&gt;


&lt;p&gt;Here is what the solution explorer looks like, as you can see the folder hierarchy is very similar to the one in SSMS&lt;/p&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;img src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt//db4.png&quot; alt=&quot;&quot; title=&quot;&quot; width=&quot;345&quot; height=&quot;765&quot; /&gt;&lt;/div&gt;





&lt;p&gt;Okay now we are ready to run our static code analysis, click on Data--&gt;Static Code Analysis--Run&lt;br /&gt;
&lt;img src=&quot;http://imgur.com/oqRCC.png&quot; alt=&quot;Run Analysis&quot; title=&quot;Run Analysis&quot; /&gt;&lt;/p&gt;


&lt;p&gt;Here is the output from that, this creates 216 warnings, I have not pasted the whole output here because it is longer than this whole blog post and most warnings are the same but just for different objects. Here is just a small part and we will look at two of those procedures mentioned in this&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;MT_smaller&quot;&gt;Running Code Analysis...&lt;br /&gt;
Verifying project state...&lt;br /&gt;
Finished verifying project state.&lt;br /&gt;
Loading project references...&lt;br /&gt;
Loading project files...&lt;br /&gt;
Building the project model and resolving object interdependencies...&lt;br /&gt;
Validating the project model...&lt;br /&gt;
ASPNET_APPLICATIONS_CREATEAPPLICATION.PROC.SQL(7,79)Warning : SR0015 : Microsoft.Performance : Deterministic function call (LOWER) might cause an unnecessary table scan.&lt;br /&gt;
ASPNET_APPLICATIONS_CREATEAPPLICATION.PROC.SQL(24,15)Warning : SR0015 : Microsoft.Performance : Deterministic function call (LOWER) might cause an unnecessary table scan.&lt;br /&gt;
ASPNET_CHECKSCHEMAVERSION.PROC.SQL(9,35)Warning : SR0015 : Microsoft.Performance : Deterministic function call (LOWER) might cause an unnecessary table scan.&lt;br /&gt;
ASPNET_MEMBERSHIP_CHANGEPASSWORDQUESTIONANDANSWER.PROC.SQL(11,38)Warning : SR0010 : Microsoft.Design : Old-style JOIN syntax is used.&lt;br /&gt;
ASPNET_MEMBERSHIP_CHANGEPASSWORDQUESTIONANDANSWER.PROC.SQL(11,58)Warning : SR0010 : Microsoft.Design : Old-style JOIN syntax is used.&lt;br /&gt;
ASPNET_MEMBERSHIP_CHANGEPASSWORDQUESTIONANDANSWER.PROC.SQL(12,31)Warning : SR0015 : Microsoft.Performance : Deterministic function call (LOWER) might cause an unnecessary table scan.&lt;br /&gt;
ASPNET_MEMBERSHIP_CHANGEPASSWORDQUESTIONANDANSWER.PROC.SQL(14,13)Warning : SR0015 : Microsoft.Performance : Deterministic function call (LOWER) might cause an unnecessary table scan.&lt;br /&gt;
ASPNET_USERSINROLES_GETUSERSINROLES.PROC.SQL(9,75)Warning : SR0015 : Microsoft.Performance : Deterministic function call (LOWER) might cause an unnecessary table scan.&lt;br /&gt;
ASPNET_USERSINROLES_GETUSERSINROLES.PROC.SQL(17,14)Warning : SR0015 : Microsoft.Performance : Deterministic function call (LOWER) might cause an unnecessary table scan.&lt;br /&gt;
ASPNET_USERSINROLES_GETUSERSINROLES.PROC.SQL(23,32)Warning : SR0010 : Microsoft.Design : Old-style JOIN syntax is used.&lt;br /&gt;
ASPNET_USERSINROLES_ISUSERINROLE.PROC.SQL(10,75)Warning : SR0015 : Microsoft.Performance : Deterministic function call (LOWER) might cause an unnecessary table scan.&lt;br /&gt;
ASPNET_USERSINROLES_ISUSERINROLE.PROC.SQL(20,31)Warning : SR0015 : Microsoft.Performance : Deterministic function call (LOWER) might cause an unnecessary table scan.&lt;br /&gt;
ASPNET_USERSINROLES_ISUSERINROLE.PROC.SQL(27,31)Warning : SR0015 : Microsoft.Performance : Deterministic function call (LOWER) might cause an unnecessary table scan.&lt;br /&gt;
ASPNET_USERSINROLES_REMOVEUSERSFROMROLES.PROC.SQL(10,64)Warning : SR0015 : Microsoft.Performance : Deterministic function call (LOWER) might cause an unnecessary table scan.&lt;br /&gt;
ASPNET_USERSINROLES_REMOVEUSERSFROMROLES.PROC.SQL(52,32)Warning : SR0010 : Microsoft.Design : Old-style JOIN syntax is used.&lt;br /&gt;
ASPNET_USERSINROLES_REMOVEUSERSFROMROLES.PROC.SQL(60,83)Warning : SR0010 : Microsoft.Design : Old-style JOIN syntax is used.&lt;br /&gt;
ASPNET_USERSINROLES_REMOVEUSERSFROMROLES.PROC.SQL(86,32)Warning : SR0010 : Microsoft.Design : Old-style JOIN syntax is used.&lt;br /&gt;
ASPNET_USERSINROLES_REMOVEUSERSFROMROLES.PROC.SQL(94,83)Warning : SR0010 : Microsoft.Design : Old-style JOIN syntax is used.&lt;br /&gt;
ASPNET_USERSINROLES_REMOVEUSERSFROMROLES.PROC.SQL(102,35)Warning : SR0010 : Microsoft.Design : Old-style JOIN syntax is used.&lt;br /&gt;
ASPNET_USERSINROLES_REMOVEUSERSFROMROLES.PROC.SQL(102,47)Warning : SR0010 : Microsoft.Design : Old-style JOIN syntax is used.&lt;br /&gt;
ASPNET_USERSINROLES_REMOVEUSERSFROMROLES.PROC.SQL(108,23)Warning : SR0010 : Microsoft.Design : Old-style JOIN syntax is used.&lt;br /&gt;
ASPNET_USERSINROLES_REMOVEUSERSFROMROLES.PROC.SQL(108,36)Warning : SR0010 : Microsoft.Design : Old-style JOIN syntax is used.&lt;br /&gt;
ASPNET_USERSINROLES_REMOVEUSERSFROMROLES.PROC.SQL(108,56)Warning : SR0010 : Microsoft.Design : Old-style JOIN syntax is used.&lt;br /&gt;
ASPNET_USERSINROLES_REMOVEUSERSFROMROLES.PROC.SQL(110,7)Warning : SR0004 : Microsoft.Performance : A column without an index that is used as an IN predicate test expression might degrade performance.&lt;br /&gt;
ASPNET_USERSINROLES_REMOVEUSERSFROMROLES.PROC.SQL(111,7)Warning : SR0004 : Microsoft.Performance : A column without an index that is used as an IN predicate test expression might degrade performance.&lt;br /&gt;
ASPNET_USERSINROLES_REMOVEUSERSFROMROLES.PROC.SQL(118,8)Warning : SR0004 : Microsoft.Performance : A column without an index that is used as an IN predicate test expression might degrade performance.&lt;br /&gt;
ASPNET_WEBEVENT_LOGEVENT.PROC.SQL(44,9)Warning : SR0014 : Microsoft.Design : Data loss might occur when casting from Decimal to Decimal.&lt;br /&gt;
ASPNET_WEBEVENT_LOGEVENT.PROC.SQL(45,9)Warning : SR0014 : Microsoft.Design : Data loss might occur when casting from Decimal to Decimal.&lt;br /&gt;
Code analysis complete -- 0 error(s), 216 warning(s)&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Now I was intrigued so I picked the aspnet_Applications_CreateApplication stored procedure from that list.&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;cb61408&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;USE&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;aspnetdb&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;GO&lt;br /&gt;&lt;span style=&quot;color: #008080;&quot;&gt;/****** Object: &amp;nbsp;StoredProcedure [dbo].[aspnet_Applications_CreateApplication] &amp;nbsp; &amp;nbsp;Script Date: 01/17/2010 15:51:15 ******/&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; ANSI_NULLS &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt;&lt;br /&gt;GO&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; QUOTED_IDENTIFIER &lt;span style=&quot;color: #0000FF;&quot;&gt;OFF&lt;/span&gt;&lt;br /&gt;GO&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ALTER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;PROCEDURE&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;dbo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;aspnet_Applications_CreateApplication&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; @ApplicationName &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;NVARCHAR&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;256&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; @ApplicationId &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;UNIQUEIDENTIFIER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;OUTPUT&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;BEGIN&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &amp;nbsp;@ApplicationId = ApplicationId &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;aspnet_Applications&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;LOWER&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@ApplicationName&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; = LoweredApplicationName&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;IF&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@ApplicationId &lt;span style=&quot;color: #0000FF;&quot;&gt;IS&lt;/span&gt; NULL&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;BEGIN&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; @TranStarted &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;BIT&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; @TranStarted = &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;IF&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;@@TRANCOUNT&lt;/span&gt; = &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt; &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;BEGIN&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;BEGIN&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TRANSACTION&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @TranStarted = &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;END&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &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; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @TranStarted = &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &amp;nbsp;@ApplicationId = ApplicationId&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;aspnet_Applications&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;WITH&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;UPDLOCK, &lt;span style=&quot;color: #0000FF;&quot;&gt;HOLDLOCK&lt;/span&gt;&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;WHERE&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;LOWER&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@ApplicationName&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; = LoweredApplicationName&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;IF&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@ApplicationId &lt;span style=&quot;color: #0000FF;&quot;&gt;IS&lt;/span&gt; NULL&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;BEGIN&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &amp;nbsp;@ApplicationId = NEWID&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &amp;nbsp;dbo.&lt;span style=&quot;color: #202020;&quot;&gt;aspnet_Applications&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;ApplicationId, ApplicationName, LoweredApplicationName&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;VALUES&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@ApplicationId, @ApplicationName, &lt;span style=&quot;color: #FF00FF;&quot;&gt;LOWER&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@ApplicationName&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;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;END&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;IF&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt; @TranStarted = &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; &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; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;IF&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;@@ERROR&lt;/span&gt; = &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &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; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @TranStarted = &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;COMMIT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TRANSACTION&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;END&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &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; &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @TranStarted = &lt;span style=&quot;color: #000;&quot;&gt;0&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: #0000FF;&quot;&gt;ROLLBACK&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TRANSACTION&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;END&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;END&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;END&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb18845&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As you can see it uses the LOWER function in this line&lt;/p&gt;

&lt;p&gt;WHERE LOWER(@ApplicationName) = LoweredApplicationName&lt;/p&gt;

&lt;p&gt;It actually tells you on which line and position this is actually used (ASPNET_APPLICATIONS_CREATEAPPLICATION.PROC.SQL(7,79)Warning) so this means line 7, position 79&lt;/p&gt;


&lt;p&gt;Here is another stored procedure, this one uses the LOWER function and old style joins&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;cb26059&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;USE&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;aspnetdb&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;GO&lt;br /&gt;&lt;span style=&quot;color: #008080;&quot;&gt;/****** Object: &amp;nbsp;StoredProcedure [dbo].[aspnet_UsersInRoles_RemoveUsersFromRoles] &amp;nbsp; &amp;nbsp;Script Date: 01/17/2010 15:53:59 ******/&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; ANSI_NULLS &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt;&lt;br /&gt;GO&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; QUOTED_IDENTIFIER &lt;span style=&quot;color: #0000FF;&quot;&gt;OFF&lt;/span&gt;&lt;br /&gt;GO&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ALTER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;PROCEDURE&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;dbo&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;.&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;aspnet_UsersInRoles_RemoveUsersFromRoles&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; @ApplicationName &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;NVARCHAR&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;256&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp; @UserNames &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;NVARCHAR&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;br /&gt;&amp;nbsp; &amp;nbsp; @RoleNames &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;NVARCHAR&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;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;BEGIN&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; @AppId &lt;span style=&quot;color: #0000FF;&quot;&gt;UNIQUEIDENTIFIER&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &amp;nbsp;@AppId = NULL&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &amp;nbsp;@AppId = ApplicationId &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; aspnet_Applications &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;LOWER&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@ApplicationName&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; = LoweredApplicationName&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;IF&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@AppId &lt;span style=&quot;color: #0000FF;&quot;&gt;IS&lt;/span&gt; NULL&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;RETURN&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;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; @TranStarted &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;BIT&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @TranStarted = &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;IF&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;@@TRANCOUNT&lt;/span&gt; = &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt; &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;BEGIN&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;BEGIN&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TRANSACTION&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; @TranStarted = &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;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; @tbNames &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Name &lt;span style=&quot;color: #0000FF;&quot;&gt;NVARCHAR&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;256&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL &lt;span style=&quot;color: #0000FF;&quot;&gt;PRIMARY&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;KEY&lt;/span&gt;&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;DECLARE&lt;/span&gt; @tbRoles &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;RoleId &lt;span style=&quot;color: #0000FF;&quot;&gt;UNIQUEIDENTIFIER&lt;/span&gt; NOT NULL &lt;span style=&quot;color: #0000FF;&quot;&gt;PRIMARY&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;KEY&lt;/span&gt;&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;DECLARE&lt;/span&gt; @tbUsers &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;UserId &lt;span style=&quot;color: #0000FF;&quot;&gt;UNIQUEIDENTIFIER&lt;/span&gt; NOT NULL &lt;span style=&quot;color: #0000FF;&quot;&gt;PRIMARY&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;KEY&lt;/span&gt;&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;DECLARE&lt;/span&gt; @Num &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;INT&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; @Pos &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;INT&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; @NextPos &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;INT&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; @Name &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;NVARCHAR&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;256&lt;/span&gt;&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;DECLARE&lt;/span&gt; @CountAll &lt;span style=&quot;color: #0000FF;&quot;&gt;INT&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; @CountU &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INT&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;DECLARE&lt;/span&gt; @CountR &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INT&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @Num = &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @Pos = &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;WHILE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@Pos &amp;lt;= &lt;span style=&quot;color: #FF00FF;&quot;&gt;LEN&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@RoleNames&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;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;SELECT&lt;/span&gt; @NextPos = &lt;span style=&quot;color: #FF00FF;&quot;&gt;CHARINDEX&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;N&lt;span style=&quot;color: #FF0000;&quot;&gt;','&lt;/span&gt;, @RoleNames, &amp;nbsp;@Pos&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;IF&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@NextPos = &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt; OR @NextPos &lt;span style=&quot;color: #0000FF;&quot;&gt;IS&lt;/span&gt; NULL&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; @NextPos = &lt;span style=&quot;color: #FF00FF;&quot;&gt;LEN&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@RoleNames&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &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; @Name = &lt;span style=&quot;color: #FF00FF;&quot;&gt;RTRIM&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;LTRIM&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;SUBSTRING&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@RoleNames, @Pos, @NextPos - @Pos&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;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; @Pos = @NextPos&lt;span style=&quot;color: #000;&quot;&gt;+1&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; @tbNames &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;@Name&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;SET&lt;/span&gt; @Num = @Num + &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;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; @tbRoles&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; RoleId&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; &amp;nbsp; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;aspnet_Roles&lt;/span&gt; ar, @tbNames t&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;LOWER&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;t.&lt;span style=&quot;color: #202020;&quot;&gt;Name&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; = ar.&lt;span style=&quot;color: #202020;&quot;&gt;LoweredRoleName&lt;/span&gt; AND ar.&lt;span style=&quot;color: #202020;&quot;&gt;ApplicationId&lt;/span&gt; = @AppId&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; @CountR = &lt;span style=&quot;color: #FF00FF;&quot;&gt;@@ROWCOUNT&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;IF&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@CountR &amp;lt;&amp;gt; @Num&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;BEGIN&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;TOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt; N&lt;span style=&quot;color: #FF0000;&quot;&gt;''&lt;/span&gt;, Name&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; &amp;nbsp; @tbNames&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;LOWER&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT 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; ar.&lt;span style=&quot;color: #202020;&quot;&gt;LoweredRoleName&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;aspnet_Roles&lt;/span&gt; ar, &amp;nbsp;@tbRoles r &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; r.&lt;span style=&quot;color: #202020;&quot;&gt;RoleId&lt;/span&gt; = ar.&lt;span style=&quot;color: #202020;&quot;&gt;RoleId&lt;/span&gt;&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;IF&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt; @TranStarted = &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;ROLLBACK&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TRANSACTION&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;RETURN&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;&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;END&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;DELETE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; @tbNames &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&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;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @Num = &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; @Pos = &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHILE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@Pos &amp;lt;= &lt;span style=&quot;color: #FF00FF;&quot;&gt;LEN&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@UserNames&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;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;SELECT&lt;/span&gt; @NextPos = &lt;span style=&quot;color: #FF00FF;&quot;&gt;CHARINDEX&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;N&lt;span style=&quot;color: #FF0000;&quot;&gt;','&lt;/span&gt;, @UserNames, &amp;nbsp;@Pos&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;IF&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@NextPos = &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt; OR @NextPos &lt;span style=&quot;color: #0000FF;&quot;&gt;IS&lt;/span&gt; NULL&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; @NextPos = &lt;span style=&quot;color: #FF00FF;&quot;&gt;LEN&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@UserNames&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; + &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; @Name = &lt;span style=&quot;color: #FF00FF;&quot;&gt;RTRIM&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;LTRIM&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;SUBSTRING&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@UserNames, @Pos, @NextPos - @Pos&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;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; @Pos = @NextPos&lt;span style=&quot;color: #000;&quot;&gt;+1&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; @tbNames &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;@Name&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;SET&lt;/span&gt; @Num = @Num + &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;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INTO&lt;/span&gt; @tbUsers&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; UserId&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; &amp;nbsp; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;aspnet_Users&lt;/span&gt; ar, @tbNames t&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;LOWER&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;t.&lt;span style=&quot;color: #202020;&quot;&gt;Name&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; = ar.&lt;span style=&quot;color: #202020;&quot;&gt;LoweredUserName&lt;/span&gt; AND ar.&lt;span style=&quot;color: #202020;&quot;&gt;ApplicationId&lt;/span&gt; = @AppId&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; @CountU = &lt;span style=&quot;color: #FF00FF;&quot;&gt;@@ROWCOUNT&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;IF&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@CountU &amp;lt;&amp;gt; @Num&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;BEGIN&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;TOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt; Name, N&lt;span style=&quot;color: #FF0000;&quot;&gt;''&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; &amp;nbsp; @tbNames&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;LOWER&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;Name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT 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; au.&lt;span style=&quot;color: #202020;&quot;&gt;LoweredUserName&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;aspnet_Users&lt;/span&gt; au, &amp;nbsp;@tbUsers u &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; u.&lt;span style=&quot;color: #202020;&quot;&gt;UserId&lt;/span&gt; = au.&lt;span style=&quot;color: #202020;&quot;&gt;UserId&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;IF&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt; @TranStarted = &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;ROLLBACK&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TRANSACTION&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;RETURN&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; &lt;span style=&quot;color: #0000FF;&quot;&gt;END&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &amp;nbsp;@CountAll = &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;*&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;FROM&lt;/span&gt; &amp;nbsp; &amp;nbsp;dbo.&lt;span style=&quot;color: #202020;&quot;&gt;aspnet_UsersInRoles&lt;/span&gt; ur, @tbUsers u, @tbRoles r&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; &amp;nbsp; ur.&lt;span style=&quot;color: #202020;&quot;&gt;UserId&lt;/span&gt; = u.&lt;span style=&quot;color: #202020;&quot;&gt;UserId&lt;/span&gt; AND ur.&lt;span style=&quot;color: #202020;&quot;&gt;RoleId&lt;/span&gt; = r.&lt;span style=&quot;color: #202020;&quot;&gt;RoleId&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;IF&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;@CountAll &amp;lt;&amp;gt; @CountU * @CountR&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;BEGIN&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;TOP&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt; UserName, RoleName&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @tbUsers tu, @tbRoles tr, dbo.&lt;span style=&quot;color: #202020;&quot;&gt;aspnet_Users&lt;/span&gt; u, dbo.&lt;span style=&quot;color: #202020;&quot;&gt;aspnet_Roles&lt;/span&gt; r&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;u.&lt;span style=&quot;color: #202020;&quot;&gt;UserId&lt;/span&gt; = tu.&lt;span style=&quot;color: #202020;&quot;&gt;UserId&lt;/span&gt; AND r.&lt;span style=&quot;color: #202020;&quot;&gt;RoleId&lt;/span&gt; = tr.&lt;span style=&quot;color: #202020;&quot;&gt;RoleId&lt;/span&gt; AND&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;tu.&lt;span style=&quot;color: #202020;&quot;&gt;UserId&lt;/span&gt; NOT 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; ur.&lt;span style=&quot;color: #202020;&quot;&gt;UserId&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;aspnet_UsersInRoles&lt;/span&gt; ur &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; ur.&lt;span style=&quot;color: #202020;&quot;&gt;RoleId&lt;/span&gt; = tr.&lt;span style=&quot;color: #202020;&quot;&gt;RoleId&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; AND&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;tr.&lt;span style=&quot;color: #202020;&quot;&gt;RoleId&lt;/span&gt; NOT 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; ur.&lt;span style=&quot;color: #202020;&quot;&gt;RoleId&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;aspnet_UsersInRoles&lt;/span&gt; ur &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; ur.&lt;span style=&quot;color: #202020;&quot;&gt;UserId&lt;/span&gt; = tu.&lt;span style=&quot;color: #202020;&quot;&gt;UserId&lt;/span&gt;&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;IF&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt; @TranStarted = &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;ROLLBACK&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TRANSACTION&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;RETURN&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;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;END&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;DELETE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; dbo.&lt;span style=&quot;color: #202020;&quot;&gt;aspnet_UsersInRoles&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; UserId 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; UserId &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; @tbUsers&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; AND RoleId 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; RoleId &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; @tbRoles&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;IF&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt; @TranStarted = &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; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;COMMIT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TRANSACTION&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;RETURN&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;0&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;END&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb33176&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So, as you can see it is pretty simple to use Visual Studio Team System 2008 Database Edition to perform static code analysis, the question is of course if you dare to run this against your own database?&lt;/p&gt;

&lt;p&gt;I will be back with another post to explore some other things that this tool can do&lt;/p&gt;


&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
*** &lt;strong&gt;Remember, if you have a SQL related question try our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=17&quot;&gt;Microsoft SQL Server Programming&lt;/a&gt; forum or our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=22&quot;&gt;Microsoft SQL Server Admin&lt;/a&gt; forum&lt;/strong&gt;&lt;ins&gt;&lt;/ins&gt;&lt;/p&gt;&lt;a href=&quot;http://api.tweetmeme.com/share?url=http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/running-static-code-analysis-on-sql-serv&quot;&gt;&lt;img src=&quot;http://api.tweetmeme.com/imagebutton.gif?url=http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/running-static-code-analysis-on-sql-serv&amp;source=LessThanDot&quot; height=&quot;61&quot; width=&quot;51&quot; /&gt;&lt;/a&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/DataDesign/running-static-code-analysis-on-sql-serv&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[				<h3>by <a href="http://blogs.lessthandot.com/index.php/All/?disp=authdir&amp;author=4"> SQLDenis </a></h3>
				<p>To me it seems that Visual Studio Team System 2008 Database Edition is the stepchild of the Visual Studio family. Even in shops that have MSDN Universal/Ultimate subscriptions this version is just not used that much. Maybe it is that long name of this product, I still prefer DataDude. I would like to show you that if you do have licenses for this tool then you should use it because it has some great features. Today we will focus on static code analysis.</p>

<p>Before we start, make sure to grab Microsoft Visual Studio Team System 2008 Database Edition GDR R2<br />
and install that on top of your Visual Studio Team System 2008 Database Edition. This install adds the following things to Visual Studio</p>

<p>In addition to providing support for SQL Server 2008 database projects, this release incorporates many previously released Power Tools as well as several new features. The new features include distinct Build and Deploy phases, Static Code Analysis and improved integration with SQL CLR projects.</p>

<p>Database Edition no longer requires a Design Database. Therefore, it is no longer necessary to install an instance of SQL Express or SQL Server prior to using Database Edition. <br />
You can download  Microsoft Visual Studio Team System 2008 Database Edition GDR R2  here <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=bb3ad767-5f69-4db9-b1c9-8f55759846ed&amp;displaylang=en">http://www.microsoft.com/downloads/details.aspx?FamilyID=bb3ad767-5f69-4db9-b1c9-8f55759846ed&amp;displaylang=en</a></p>

<p>Once you have everything installed, start up Visual Studio and create a new database project. You should see something similar to the pic below</p>

<div class="image_block"><img src="http://blogs.lessthandot.com/media/blogs/DataMgmt//db1.png" alt="" title="" width="630" height="426" /></div>

<p>Create a new database connection, you can do that by going to  View-->Server Explorer. Under Data Connections add a new connection to your database that you want to connect to, follow the wizard and create the connection. Now I decided to run static code analysis against the aspnetdb database that is used with ASP.NET. If you want to follow along and use this same database, take a look at this post<a href="http://blogs.lessthandot.com/index.php/WebDev/ServerProgramming/ASPNET/setting-up-sql-server-with-asp-net-mvc"> Setting up SQL Server with ASP.NET MVC</a> to see how to set it up</p>



<p>Once your database is setup go to the Solution explorer, right click on the project and select Import Database Objects and Settings. </p>
<div class="image_block"><img src="http://blogs.lessthandot.com/media/blogs/DataMgmt//db2.png" alt="" title="" width="276" height="197" /></div>
<p>The following dialog will be shown</p>

<div class="image_block"><img src="http://blogs.lessthandot.com/media/blogs/DataMgmt//db3.png" alt="" title="" width="841" height="301" /></div>

<p>Pick your connection and then click start</p>

<p>The output will be similar to this</p>

<p>1/30/2010 11:26:40 AM	Import of database schema has started.<br />
1/30/2010 11:26:45 AM	Adding all files to the project...<br />
1/30/2010 11:26:46 AM	Finished adding all files to the project.<br />
1/30/2010 11:26:46 AM	Done<br />
1/30/2010 11:26:46 AM	Import of database schema is complete.<br />
1/30/2010 11:26:46 AM	A summary of the import operation has been saved to the log file C:\SVN\InterrogateASP\InterrogateASP\Import Schema Logs\InterrogateASP_20100130042639.log.<br />
1/30/2010 11:26:46 AM	Press Finish to continue...</p>


<p>Here is what the solution explorer looks like, as you can see the folder hierarchy is very similar to the one in SSMS</p>
<div class="image_block"><img src="http://blogs.lessthandot.com/media/blogs/DataMgmt//db4.png" alt="" title="" width="345" height="765" /></div>





<p>Okay now we are ready to run our static code analysis, click on Data-->Static Code Analysis--Run<br />
<img src="http://imgur.com/oqRCC.png" alt="Run Analysis" title="Run Analysis" /></p>


<p>Here is the output from that, this creates 216 warnings, I have not pasted the whole output here because it is longer than this whole blog post and most warnings are the same but just for different objects. Here is just a small part and we will look at two of those procedures mentioned in this</p>

<p><span class="MT_smaller">Running Code Analysis...<br />
Verifying project state...<br />
Finished verifying project state.<br />
Loading project references...<br />
Loading project files...<br />
Building the project model and resolving object interdependencies...<br />
Validating the project model...<br />
ASPNET_APPLICATIONS_CREATEAPPLICATION.PROC.SQL(7,79)Warning : SR0015 : Microsoft.Performance : Deterministic function call (LOWER) might cause an unnecessary table scan.<br />
ASPNET_APPLICATIONS_CREATEAPPLICATION.PROC.SQL(24,15)Warning : SR0015 : Microsoft.Performance : Deterministic function call (LOWER) might cause an unnecessary table scan.<br />
ASPNET_CHECKSCHEMAVERSION.PROC.SQL(9,35)Warning : SR0015 : Microsoft.Performance : Deterministic function call (LOWER) might cause an unnecessary table scan.<br />
ASPNET_MEMBERSHIP_CHANGEPASSWORDQUESTIONANDANSWER.PROC.SQL(11,38)Warning : SR0010 : Microsoft.Design : Old-style JOIN syntax is used.<br />
ASPNET_MEMBERSHIP_CHANGEPASSWORDQUESTIONANDANSWER.PROC.SQL(11,58)Warning : SR0010 : Microsoft.Design : Old-style JOIN syntax is used.<br />
ASPNET_MEMBERSHIP_CHANGEPASSWORDQUESTIONANDANSWER.PROC.SQL(12,31)Warning : SR0015 : Microsoft.Performance : Deterministic function call (LOWER) might cause an unnecessary table scan.<br />
ASPNET_MEMBERSHIP_CHANGEPASSWORDQUESTIONANDANSWER.PROC.SQL(14,13)Warning : SR0015 : Microsoft.Performance : Deterministic function call (LOWER) might cause an unnecessary table scan.<br />
ASPNET_USERSINROLES_GETUSERSINROLES.PROC.SQL(9,75)Warning : SR0015 : Microsoft.Performance : Deterministic function call (LOWER) might cause an unnecessary table scan.<br />
ASPNET_USERSINROLES_GETUSERSINROLES.PROC.SQL(17,14)Warning : SR0015 : Microsoft.Performance : Deterministic function call (LOWER) might cause an unnecessary table scan.<br />
ASPNET_USERSINROLES_GETUSERSINROLES.PROC.SQL(23,32)Warning : SR0010 : Microsoft.Design : Old-style JOIN syntax is used.<br />
ASPNET_USERSINROLES_ISUSERINROLE.PROC.SQL(10,75)Warning : SR0015 : Microsoft.Performance : Deterministic function call (LOWER) might cause an unnecessary table scan.<br />
ASPNET_USERSINROLES_ISUSERINROLE.PROC.SQL(20,31)Warning : SR0015 : Microsoft.Performance : Deterministic function call (LOWER) might cause an unnecessary table scan.<br />
ASPNET_USERSINROLES_ISUSERINROLE.PROC.SQL(27,31)Warning : SR0015 : Microsoft.Performance : Deterministic function call (LOWER) might cause an unnecessary table scan.<br />
ASPNET_USERSINROLES_REMOVEUSERSFROMROLES.PROC.SQL(10,64)Warning : SR0015 : Microsoft.Performance : Deterministic function call (LOWER) might cause an unnecessary table scan.<br />
ASPNET_USERSINROLES_REMOVEUSERSFROMROLES.PROC.SQL(52,32)Warning : SR0010 : Microsoft.Design : Old-style JOIN syntax is used.<br />
ASPNET_USERSINROLES_REMOVEUSERSFROMROLES.PROC.SQL(60,83)Warning : SR0010 : Microsoft.Design : Old-style JOIN syntax is used.<br />
ASPNET_USERSINROLES_REMOVEUSERSFROMROLES.PROC.SQL(86,32)Warning : SR0010 : Microsoft.Design : Old-style JOIN syntax is used.<br />
ASPNET_USERSINROLES_REMOVEUSERSFROMROLES.PROC.SQL(94,83)Warning : SR0010 : Microsoft.Design : Old-style JOIN syntax is used.<br />
ASPNET_USERSINROLES_REMOVEUSERSFROMROLES.PROC.SQL(102,35)Warning : SR0010 : Microsoft.Design : Old-style JOIN syntax is used.<br />
ASPNET_USERSINROLES_REMOVEUSERSFROMROLES.PROC.SQL(102,47)Warning : SR0010 : Microsoft.Design : Old-style JOIN syntax is used.<br />
ASPNET_USERSINROLES_REMOVEUSERSFROMROLES.PROC.SQL(108,23)Warning : SR0010 : Microsoft.Design : Old-style JOIN syntax is used.<br />
ASPNET_USERSINROLES_REMOVEUSERSFROMROLES.PROC.SQL(108,36)Warning : SR0010 : Microsoft.Design : Old-style JOIN syntax is used.<br />
ASPNET_USERSINROLES_REMOVEUSERSFROMROLES.PROC.SQL(108,56)Warning : SR0010 : Microsoft.Design : Old-style JOIN syntax is used.<br />
ASPNET_USERSINROLES_REMOVEUSERSFROMROLES.PROC.SQL(110,7)Warning : SR0004 : Microsoft.Performance : A column without an index that is used as an IN predicate test expression might degrade performance.<br />
ASPNET_USERSINROLES_REMOVEUSERSFROMROLES.PROC.SQL(111,7)Warning : SR0004 : Microsoft.Performance : A column without an index that is used as an IN predicate test expression might degrade performance.<br />
ASPNET_USERSINROLES_REMOVEUSERSFROMROLES.PROC.SQL(118,8)Warning : SR0004 : Microsoft.Performance : A column without an index that is used as an IN predicate test expression might degrade performance.<br />
ASPNET_WEBEVENT_LOGEVENT.PROC.SQL(44,9)Warning : SR0014 : Microsoft.Design : Data loss might occur when casting from Decimal to Decimal.<br />
ASPNET_WEBEVENT_LOGEVENT.PROC.SQL(45,9)Warning : SR0014 : Microsoft.Design : Data loss might occur when casting from Decimal to Decimal.<br />
Code analysis complete -- 0 error(s), 216 warning(s)</span></p>

<p>Now I was intrigued so I picked the aspnet_Applications_CreateApplication stored procedure from that list.</p>

<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb33631" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">USE</span> <span style="color: #808080;">&#91;</span>aspnetdb<span style="color: #808080;">&#93;</span><br />GO<br /><span style="color: #008080;">/****** Object: &nbsp;StoredProcedure [dbo].[aspnet_Applications_CreateApplication] &nbsp; &nbsp;Script Date: 01/17/2010 15:51:15 ******/</span><br /><span style="color: #0000FF;">SET</span> ANSI_NULLS <span style="color: #0000FF;">ON</span><br />GO<br /><span style="color: #0000FF;">SET</span> QUOTED_IDENTIFIER <span style="color: #0000FF;">OFF</span><br />GO<br />&nbsp;<br /><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">PROCEDURE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>aspnet_Applications_CreateApplication<span style="color: #808080;">&#93;</span><br />&nbsp; &nbsp; @ApplicationName &nbsp; &nbsp; &nbsp;<span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">256</span><span style="color: #808080;">&#41;</span>,<br />&nbsp; &nbsp; @ApplicationId &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000FF;">UNIQUEIDENTIFIER</span> <span style="color: #0000FF;">OUTPUT</span><br /><span style="color: #0000FF;">AS</span><br /><span style="color: #0000FF;">BEGIN</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> &nbsp;@ApplicationId = ApplicationId <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">aspnet_Applications</span> <span style="color: #0000FF;">WHERE</span> <span style="color: #FF00FF;">LOWER</span><span style="color: #808080;">&#40;</span>@ApplicationName<span style="color: #808080;">&#41;</span> = LoweredApplicationName<br />&nbsp;<br />&nbsp; &nbsp; <span style="color: #0000FF;">IF</span><span style="color: #808080;">&#40;</span>@ApplicationId <span style="color: #0000FF;">IS</span> NULL<span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">DECLARE</span> @TranStarted &nbsp; <span style="color: #0000FF;">BIT</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @TranStarted = <span style="color: #000;">0</span><br />&nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">IF</span><span style="color: #808080;">&#40;</span> <span style="color: #FF00FF;">@@TRANCOUNT</span> = <span style="color: #000;">0</span> <span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</span> <span style="color: #0000FF;">TRANSACTION</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @TranStarted = <span style="color: #000;">1</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ELSE</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @TranStarted = <span style="color: #000;">0</span><br />&nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> &nbsp;@ApplicationId = ApplicationId<br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">aspnet_Applications</span> <span style="color: #0000FF;">WITH</span> <span style="color: #808080;">&#40;</span>UPDLOCK, <span style="color: #0000FF;">HOLDLOCK</span><span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> <span style="color: #FF00FF;">LOWER</span><span style="color: #808080;">&#40;</span>@ApplicationName<span style="color: #808080;">&#41;</span> = LoweredApplicationName<br />&nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">IF</span><span style="color: #808080;">&#40;</span>@ApplicationId <span style="color: #0000FF;">IS</span> NULL<span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> &nbsp;@ApplicationId = NEWID<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span> &nbsp;dbo.<span style="color: #202020;">aspnet_Applications</span> <span style="color: #808080;">&#40;</span>ApplicationId, ApplicationName, LoweredApplicationName<span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">VALUES</span> &nbsp;<span style="color: #808080;">&#40;</span>@ApplicationId, @ApplicationName, <span style="color: #FF00FF;">LOWER</span><span style="color: #808080;">&#40;</span>@ApplicationName<span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span><br />&nbsp;<br />&nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">IF</span><span style="color: #808080;">&#40;</span> @TranStarted = <span style="color: #000;">1</span> <span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">IF</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">@@ERROR</span> = <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @TranStarted = <span style="color: #000;">0</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">COMMIT</span> <span style="color: #0000FF;">TRANSACTION</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ELSE</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @TranStarted = <span style="color: #000;">0</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ROLLBACK</span> <span style="color: #0000FF;">TRANSACTION</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">END</span><br /><span style="color: #0000FF;">END</span><br /><br /></div><div id="cb200" style="display: none; color: red;"></div></div></div>

<p>As you can see it uses the LOWER function in this line</p>

<p>WHERE LOWER(@ApplicationName) = LoweredApplicationName</p>

<p>It actually tells you on which line and position this is actually used (ASPNET_APPLICATIONS_CREATEAPPLICATION.PROC.SQL(7,79)Warning) so this means line 7, position 79</p>


<p>Here is another stored procedure, this one uses the LOWER function and old style joins</p>

<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb73457" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">USE</span> <span style="color: #808080;">&#91;</span>aspnetdb<span style="color: #808080;">&#93;</span><br />GO<br /><span style="color: #008080;">/****** Object: &nbsp;StoredProcedure [dbo].[aspnet_UsersInRoles_RemoveUsersFromRoles] &nbsp; &nbsp;Script Date: 01/17/2010 15:53:59 ******/</span><br /><span style="color: #0000FF;">SET</span> ANSI_NULLS <span style="color: #0000FF;">ON</span><br />GO<br /><span style="color: #0000FF;">SET</span> QUOTED_IDENTIFIER <span style="color: #0000FF;">OFF</span><br />GO<br />&nbsp;<br /><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">PROCEDURE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>aspnet_UsersInRoles_RemoveUsersFromRoles<span style="color: #808080;">&#93;</span><br />&nbsp; &nbsp; @ApplicationName &nbsp;<span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">256</span><span style="color: #808080;">&#41;</span>,<br />&nbsp; &nbsp; @UserNames &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">4000</span><span style="color: #808080;">&#41;</span>,<br />&nbsp; &nbsp; @RoleNames &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">4000</span><span style="color: #808080;">&#41;</span><br /><span style="color: #0000FF;">AS</span><br /><span style="color: #0000FF;">BEGIN</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">DECLARE</span> @AppId <span style="color: #0000FF;">UNIQUEIDENTIFIER</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> &nbsp;@AppId = NULL<br />&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> &nbsp;@AppId = ApplicationId <span style="color: #0000FF;">FROM</span> aspnet_Applications <span style="color: #0000FF;">WHERE</span> <span style="color: #FF00FF;">LOWER</span><span style="color: #808080;">&#40;</span>@ApplicationName<span style="color: #808080;">&#41;</span> = LoweredApplicationName<br />&nbsp; &nbsp; <span style="color: #0000FF;">IF</span> <span style="color: #808080;">&#40;</span>@AppId <span style="color: #0000FF;">IS</span> NULL<span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">RETURN</span><span style="color: #808080;">&#40;</span><span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><br />&nbsp;<br />&nbsp;<br />&nbsp; &nbsp; <span style="color: #0000FF;">DECLARE</span> @TranStarted &nbsp; <span style="color: #0000FF;">BIT</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @TranStarted = <span style="color: #000;">0</span><br />&nbsp;<br />&nbsp; &nbsp; <span style="color: #0000FF;">IF</span><span style="color: #808080;">&#40;</span> <span style="color: #FF00FF;">@@TRANCOUNT</span> = <span style="color: #000;">0</span> <span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</span> <span style="color: #0000FF;">TRANSACTION</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @TranStarted = <span style="color: #000;">1</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">END</span><br />&nbsp;<br />&nbsp; &nbsp; <span style="color: #0000FF;">DECLARE</span> @tbNames &nbsp;<span style="color: #0000FF;">TABLE</span><span style="color: #808080;">&#40;</span>Name <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">256</span><span style="color: #808080;">&#41;</span> NOT NULL <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span><span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">DECLARE</span> @tbRoles &nbsp;<span style="color: #0000FF;">TABLE</span><span style="color: #808080;">&#40;</span>RoleId <span style="color: #0000FF;">UNIQUEIDENTIFIER</span> NOT NULL <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span><span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">DECLARE</span> @tbUsers &nbsp;<span style="color: #0000FF;">TABLE</span><span style="color: #808080;">&#40;</span>UserId <span style="color: #0000FF;">UNIQUEIDENTIFIER</span> NOT NULL <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span><span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">DECLARE</span> @Num &nbsp; &nbsp; &nbsp;<span style="color: #0000FF;">INT</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">DECLARE</span> @Pos &nbsp; &nbsp; &nbsp;<span style="color: #0000FF;">INT</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">DECLARE</span> @NextPos &nbsp;<span style="color: #0000FF;">INT</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">DECLARE</span> @Name &nbsp; &nbsp; <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">256</span><span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">DECLARE</span> @CountAll <span style="color: #0000FF;">INT</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">DECLARE</span> @CountU &nbsp; <span style="color: #0000FF;">INT</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">DECLARE</span> @CountR &nbsp; <span style="color: #0000FF;">INT</span><br />&nbsp;<br />&nbsp;<br />&nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @Num = <span style="color: #000;">0</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @Pos = <span style="color: #000;">1</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">WHILE</span><span style="color: #808080;">&#40;</span>@Pos &lt;= <span style="color: #FF00FF;">LEN</span><span style="color: #808080;">&#40;</span>@RoleNames<span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> @NextPos = <span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span>N<span style="color: #FF0000;">','</span>, @RoleNames, &nbsp;@Pos<span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">IF</span> <span style="color: #808080;">&#40;</span>@NextPos = <span style="color: #000;">0</span> OR @NextPos <span style="color: #0000FF;">IS</span> NULL<span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> @NextPos = <span style="color: #FF00FF;">LEN</span><span style="color: #808080;">&#40;</span>@RoleNames<span style="color: #808080;">&#41;</span> + <span style="color: #000;">1</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> @Name = <span style="color: #FF00FF;">RTRIM</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">LTRIM</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>@RoleNames, @Pos, @NextPos - @Pos<span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> @Pos = @NextPos<span style="color: #000;">+1</span><br />&nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @tbNames <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span>@Name<span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @Num = @Num + <span style="color: #000;">1</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">END</span><br />&nbsp;<br />&nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @tbRoles<br />&nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> RoleId<br />&nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FROM</span> &nbsp; dbo.<span style="color: #202020;">aspnet_Roles</span> ar, @tbNames t<br />&nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> &nbsp;<span style="color: #FF00FF;">LOWER</span><span style="color: #808080;">&#40;</span>t.<span style="color: #202020;">Name</span><span style="color: #808080;">&#41;</span> = ar.<span style="color: #202020;">LoweredRoleName</span> AND ar.<span style="color: #202020;">ApplicationId</span> = @AppId<br />&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> @CountR = <span style="color: #FF00FF;">@@ROWCOUNT</span><br />&nbsp;<br />&nbsp; &nbsp; <span style="color: #0000FF;">IF</span> <span style="color: #808080;">&#40;</span>@CountR &lt;&gt; @Num<span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">1</span> N<span style="color: #FF0000;">''</span>, Name<br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FROM</span> &nbsp; @tbNames<br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> &nbsp;<span style="color: #FF00FF;">LOWER</span><span style="color: #808080;">&#40;</span>Name<span style="color: #808080;">&#41;</span> NOT IN <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> ar.<span style="color: #202020;">LoweredRoleName</span> <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">aspnet_Roles</span> ar, &nbsp;@tbRoles r <span style="color: #0000FF;">WHERE</span> r.<span style="color: #202020;">RoleId</span> = ar.<span style="color: #202020;">RoleId</span><span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">IF</span><span style="color: #808080;">&#40;</span> @TranStarted = <span style="color: #000;">1</span> <span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ROLLBACK</span> <span style="color: #0000FF;">TRANSACTION</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">RETURN</span><span style="color: #808080;">&#40;</span><span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">END</span><br />&nbsp;<br />&nbsp;<br />&nbsp; &nbsp; <span style="color: #0000FF;">DELETE</span> <span style="color: #0000FF;">FROM</span> @tbNames <span style="color: #0000FF;">WHERE</span> <span style="color: #000;">1</span>=<span style="color: #000;">1</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @Num = <span style="color: #000;">0</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @Pos = <span style="color: #000;">1</span><br />&nbsp;<br />&nbsp;<br />&nbsp; &nbsp; <span style="color: #0000FF;">WHILE</span><span style="color: #808080;">&#40;</span>@Pos &lt;= <span style="color: #FF00FF;">LEN</span><span style="color: #808080;">&#40;</span>@UserNames<span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> @NextPos = <span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span>N<span style="color: #FF0000;">','</span>, @UserNames, &nbsp;@Pos<span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">IF</span> <span style="color: #808080;">&#40;</span>@NextPos = <span style="color: #000;">0</span> OR @NextPos <span style="color: #0000FF;">IS</span> NULL<span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> @NextPos = <span style="color: #FF00FF;">LEN</span><span style="color: #808080;">&#40;</span>@UserNames<span style="color: #808080;">&#41;</span> + <span style="color: #000;">1</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> @Name = <span style="color: #FF00FF;">RTRIM</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">LTRIM</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>@UserNames, @Pos, @NextPos - @Pos<span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> @Pos = @NextPos<span style="color: #000;">+1</span><br />&nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @tbNames <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span>@Name<span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @Num = @Num + <span style="color: #000;">1</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">END</span><br />&nbsp;<br />&nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @tbUsers<br />&nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> UserId<br />&nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FROM</span> &nbsp; dbo.<span style="color: #202020;">aspnet_Users</span> ar, @tbNames t<br />&nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> &nbsp;<span style="color: #FF00FF;">LOWER</span><span style="color: #808080;">&#40;</span>t.<span style="color: #202020;">Name</span><span style="color: #808080;">&#41;</span> = ar.<span style="color: #202020;">LoweredUserName</span> AND ar.<span style="color: #202020;">ApplicationId</span> = @AppId<br />&nbsp;<br />&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> @CountU = <span style="color: #FF00FF;">@@ROWCOUNT</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">IF</span> <span style="color: #808080;">&#40;</span>@CountU &lt;&gt; @Num<span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">1</span> Name, N<span style="color: #FF0000;">''</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FROM</span> &nbsp; @tbNames<br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> &nbsp;<span style="color: #FF00FF;">LOWER</span><span style="color: #808080;">&#40;</span>Name<span style="color: #808080;">&#41;</span> NOT IN <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> au.<span style="color: #202020;">LoweredUserName</span> <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">aspnet_Users</span> au, &nbsp;@tbUsers u <span style="color: #0000FF;">WHERE</span> u.<span style="color: #202020;">UserId</span> = au.<span style="color: #202020;">UserId</span><span style="color: #808080;">&#41;</span><br />&nbsp;<br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">IF</span><span style="color: #808080;">&#40;</span> @TranStarted = <span style="color: #000;">1</span> <span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ROLLBACK</span> <span style="color: #0000FF;">TRANSACTION</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">RETURN</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">END</span><br />&nbsp;<br />&nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> &nbsp;@CountAll = <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span>*<span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">FROM</span> &nbsp; &nbsp;dbo.<span style="color: #202020;">aspnet_UsersInRoles</span> ur, @tbUsers u, @tbRoles r<br />&nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> &nbsp; ur.<span style="color: #202020;">UserId</span> = u.<span style="color: #202020;">UserId</span> AND ur.<span style="color: #202020;">RoleId</span> = r.<span style="color: #202020;">RoleId</span><br />&nbsp;<br />&nbsp; &nbsp; <span style="color: #0000FF;">IF</span> <span style="color: #808080;">&#40;</span>@CountAll &lt;&gt; @CountU * @CountR<span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">1</span> UserName, RoleName<br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FROM</span> &nbsp; &nbsp; &nbsp; &nbsp; @tbUsers tu, @tbRoles tr, dbo.<span style="color: #202020;">aspnet_Users</span> u, dbo.<span style="color: #202020;">aspnet_Roles</span> r<br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> &nbsp; &nbsp; &nbsp; &nbsp;u.<span style="color: #202020;">UserId</span> = tu.<span style="color: #202020;">UserId</span> AND r.<span style="color: #202020;">RoleId</span> = tr.<span style="color: #202020;">RoleId</span> AND<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tu.<span style="color: #202020;">UserId</span> NOT IN <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> ur.<span style="color: #202020;">UserId</span> <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">aspnet_UsersInRoles</span> ur <span style="color: #0000FF;">WHERE</span> ur.<span style="color: #202020;">RoleId</span> = tr.<span style="color: #202020;">RoleId</span><span style="color: #808080;">&#41;</span> AND<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tr.<span style="color: #202020;">RoleId</span> NOT IN <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> ur.<span style="color: #202020;">RoleId</span> <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">aspnet_UsersInRoles</span> ur <span style="color: #0000FF;">WHERE</span> ur.<span style="color: #202020;">UserId</span> = tu.<span style="color: #202020;">UserId</span><span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">IF</span><span style="color: #808080;">&#40;</span> @TranStarted = <span style="color: #000;">1</span> <span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ROLLBACK</span> <span style="color: #0000FF;">TRANSACTION</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">RETURN</span><span style="color: #808080;">&#40;</span><span style="color: #000;">3</span><span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">END</span><br />&nbsp;<br />&nbsp; &nbsp; <span style="color: #0000FF;">DELETE</span> <span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">aspnet_UsersInRoles</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> UserId IN <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> UserId <span style="color: #0000FF;">FROM</span> @tbUsers<span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; &nbsp; AND RoleId IN <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> RoleId <span style="color: #0000FF;">FROM</span> @tbRoles<span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">IF</span><span style="color: #808080;">&#40;</span> @TranStarted = <span style="color: #000;">1</span> <span style="color: #808080;">&#41;</span><br />&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">COMMIT</span> <span style="color: #0000FF;">TRANSACTION</span><br />&nbsp; &nbsp; <span style="color: #0000FF;">RETURN</span><span style="color: #808080;">&#40;</span><span style="color: #000;">0</span><span style="color: #808080;">&#41;</span><br /><span style="color: #0000FF;">END</span><br /><br /></div><div id="cb52701" style="display: none; color: red;"></div></div></div>

<p>So, as you can see it is pretty simple to use Visual Studio Team System 2008 Database Edition to perform static code analysis, the question is of course if you dare to run this against your own database?</p>

<p>I will be back with another post to explore some other things that this tool can do</p>


<p><br /><br /><br />
*** <strong>Remember, if you have a SQL related question try our <a href="http://forum.lessthandot.com/viewforum.php?f=17">Microsoft SQL Server Programming</a> forum or our <a href="http://forum.lessthandot.com/viewforum.php?f=22">Microsoft SQL Server Admin</a> forum</strong><ins></ins></p><a href="http://api.tweetmeme.com/share?url=http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/running-static-code-analysis-on-sql-serv"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/running-static-code-analysis-on-sql-serv&amp;source=LessThanDot" height="61" width="51" /></a><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/running-static-code-analysis-on-sql-serv">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/DataDesign/running-static-code-analysis-on-sql-serv#comments</comments>
		</item>
				<item>
			<title>Backup and copy warm-standby (log shipped) databases in SQL Server</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/backup-and-copy-warm-standby-log-shipped</link>
			<pubDate>Wed, 27 Jan 2010 15:13:19 +0000</pubDate>			<dc:creator>onpnt</dc:creator>
			<category domain="alt">Data Modelling &amp; Design</category>
<category domain="alt">Database Programming</category>
<category domain="alt">Database Administration</category>
<category domain="main">Microsoft SQL Server Admin</category>
<category domain="alt">Microsoft SQL Server</category>			<guid isPermaLink="false">737@http://blogs.lessthandot.com/</guid>
						<description>&lt;h3&gt;by &lt;a href=&quot;http://blogs.lessthandot.com/index.php/All/?disp=authdir&amp;amp;author=68&quot;&gt; onpnt &lt;/a&gt;&lt;/h3&gt;&lt;p&gt;
Most DR solutions include log shipping strategies.  Log shipping (LS) is an extremely inexpensive solution for DR and also one I recommend.  There is little learning curve to individuals just coming into the administration career for setting LS up and maintaining the flow.
&lt;/p&gt;
&lt;p&gt;
One thing that will undoubtedly be asked of you at some point in time will be to make a backup of the warm-standby database for other purposes.  Reasoning for this is commonly due to the geographical location of the standby database verses the online production environment.  Often in order to move a backup that is larger in size, there will be several hours involved in the actual copy of the phsyical files and sometimes even days depending on the variables in place on the WAN.
&lt;/p&gt;
&lt;p&gt;
When you go to make a backup of a database in standby, you will be presented with the following&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;MT_red&quot;&gt;The database &quot;bah&quot; is in warm-standby state (set by executing RESTORE WITH STANDBY) and cannot be backed up until the entire restore sequence is completed.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
In the state the database is in, you will not be able to use normal methods to get this task done.  Depending on the restore rate you have for LS, you could also break the LS stream and in the worst case scenario, cause you to reinitialize the entire LS plan.  I&amp;#8217;d like to show you a quick and easy solution to that though.
&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;
Note:  This method is the quickest way I have found.  As always with my work, I am open to others knowing better and more efficient methods.  There is a break point below in the copy.  It is a good idea to disable you LS plan while you perform this to prevent failed restore job.
&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;
&lt;p&gt;Let&amp;#8217;s setup a test database to play with.  We&amp;#8217;ll create, backup and restore to standby&amp;#8230;&lt;br /&gt;
&lt;br /&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;cb24475&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;DATABASE&lt;/span&gt; bah&lt;br /&gt;GO&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;BACKUP&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DATABASE&lt;/span&gt; bah &lt;span style=&quot;color: #0000FF;&quot;&gt;TO&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DISK&lt;/span&gt; = &lt;span style=&quot;color: #FF0000;&quot;&gt;'C:\bah.bak'&lt;/span&gt;&lt;br /&gt;GO&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;RESTORE&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DATABASE&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;bah&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; &amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DISK&lt;/span&gt; = N&lt;span style=&quot;color: #FF0000;&quot;&gt;'C:\bah.bak'&lt;/span&gt; &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WITH&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: #0000FF;&quot;&gt;FILE&lt;/span&gt; = &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt;, &amp;nbsp;STANDBY = N&lt;span style=&quot;color: #FF0000;&quot;&gt;'C:\ROLLBACK_UNDO_bah.BAK'&lt;/span&gt;, &amp;nbsp;&lt;br /&gt;NOUNLOAD, &amp;nbsp;&lt;span style=&quot;color: #FF00FF;&quot;&gt;REPLACE&lt;/span&gt;, &amp;nbsp;STATS = &lt;span style=&quot;color: #000;&quot;&gt;10&lt;/span&gt;&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb58685&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;
We should now have, &amp;#8220;bah (Standby /Read-Only) listed in our database tree.&lt;/p&gt;

The regular Copy Database with either detaching or SMO is not ideal for us at this point.  We can however take this database offline, grab the mdf, ndf&amp;#8217;s and ldf&amp;#8217;s to bring to the other instance we want to restore them to.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;p&gt;
&lt;p&gt;To do this we perform the following steps&lt;br /&gt;
&lt;br /&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;cb49185&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;USE&lt;/span&gt; MASTER&lt;br /&gt;GO&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ALTER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DATABASE&lt;/span&gt; bah &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; OFFLINE&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb88825&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

Now go to the default folder where the mdf and other files are located and copy/paste them to a local drive.  I mentioned local drive because we need to do this as quickly as possible.  If there is enough free space locally, it will be the fastest copy transmission.  Otherwise at this stage it is crucial to ensure you have disabled the LS plans so no unwanted jobs attempt to access the database while it is offline.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;p&gt;
&lt;p&gt;Once you&amp;#8217;ve copied the files off, run the following to bring the database back up&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;cb33236&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;USE&lt;/span&gt; MASTER&lt;br /&gt;GO&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ALTER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DATABASE&lt;/span&gt; bah &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; ONLINE&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb34142&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We should see the database has come back up in standby mode and log shipping can proceed as normal.&lt;/p&gt;

Next, we only need to attach the files to a new instance given the following commands or using SSMS attach/detach wizard.&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;p&gt;
&lt;blockquote&gt;&lt;p&gt;Note:  You cannot bring these files up on the same instance.  This is due to being required to leave the database &quot;as is&quot; and with the same file names.  When the database was taken offline, it was in standby mode and will be the same when attached again during the initial loading.  Once on the new instance, you can bring the database into any state you require and move it back to the other instance is required.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;On the secondary instance do the following steps&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;cb14963&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;DATABASE&lt;/span&gt; bah&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; &lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt; NAME = bah_data,FILENAME = &lt;span style=&quot;color: #FF0000;&quot;&gt;'c:\restore\bah.mdf'&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #FF00FF;&quot;&gt;LOG&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt; NAME = &lt;span style=&quot;color: #FF0000;&quot;&gt;'bah_log'&lt;/span&gt;,&lt;br /&gt;&amp;nbsp; &amp;nbsp;FILENAME = &lt;span style=&quot;color: #FF0000;&quot;&gt;'c:\restore\bah_1.ldf'&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;GO&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ALTER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DATABASE&lt;/span&gt; bah &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; OFFLINE&lt;br /&gt;GO&lt;br /&gt;&lt;span style=&quot;color: #008080;&quot;&gt;--Copy the bah files from our original standby database into the location you just created the &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #008080;&quot;&gt;--new database to. &amp;nbsp;in my case C:\restore\ &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #008080;&quot;&gt;--make sure you replace the files and that the file names, both data and logs are identical to the original&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ALTER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DATABASE&lt;/span&gt; bah &lt;span style=&quot;color: #0000FF;&quot;&gt;SET&lt;/span&gt; ONLINE&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb27258&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;&lt;br /&gt;
Now we can see already in the tree of object explorer that the database online and read-write state.  There is no need to issue a restore to recovery or anything.  &lt;/p&gt;

&lt;p&gt;To verify, we can check the read only state in sys.databases&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;cb95093&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; is_read_only &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; sys.&lt;span style=&quot;color: #202020;&quot;&gt;databases&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;name&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; = &lt;span style=&quot;color: #FF0000;&quot;&gt;'bah'&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb94903&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
This will result in 0 meaning the database is read-write&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;p&gt;
We now have a backup of a warm-standby database that is used in a log shipping plan.
&lt;/p&gt;&lt;a href=&quot;http://api.tweetmeme.com/share?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/backup-and-copy-warm-standby-log-shipped&quot;&gt;&lt;img src=&quot;http://api.tweetmeme.com/imagebutton.gif?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/backup-and-copy-warm-standby-log-shipped&amp;source=LessThanDot&quot; height=&quot;61&quot; width=&quot;51&quot; /&gt;&lt;/a&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/backup-and-copy-warm-standby-log-shipped&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[				<h3>by <a href="http://blogs.lessthandot.com/index.php/All/?disp=authdir&amp;author=68"> onpnt </a></h3>
				<p>
Most DR solutions include log shipping strategies.  Log shipping (LS) is an extremely inexpensive solution for DR and also one I recommend.  There is little learning curve to individuals just coming into the administration career for setting LS up and maintaining the flow.
</p>
<p>
One thing that will undoubtedly be asked of you at some point in time will be to make a backup of the warm-standby database for other purposes.  Reasoning for this is commonly due to the geographical location of the standby database verses the online production environment.  Often in order to move a backup that is larger in size, there will be several hours involved in the actual copy of the phsyical files and sometimes even days depending on the variables in place on the WAN.
</p>
<p>
When you go to make a backup of a database in standby, you will be presented with the following<br />
<br />
<span class="MT_red">The database "bah" is in warm-standby state (set by executing RESTORE WITH STANDBY) and cannot be backed up until the entire restore sequence is completed.</span><br />
<br />
In the state the database is in, you will not be able to use normal methods to get this task done.  Depending on the restore rate you have for LS, you could also break the LS stream and in the worst case scenario, cause you to reinitialize the entire LS plan.  I&#8217;d like to show you a quick and easy solution to that though.
</p>
<blockquote><p>
Note:  This method is the quickest way I have found.  As always with my work, I am open to others knowing better and more efficient methods.  There is a break point below in the copy.  It is a good idea to disable you LS plan while you perform this to prevent failed restore job.
</p></blockquote>
<p>
<p>Let&#8217;s setup a test database to play with.  We&#8217;ll create, backup and restore to standby&#8230;<br />
<br /></p>
<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb24614" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">DATABASE</span> bah<br />GO<br /><span style="color: #0000FF;">BACKUP</span> <span style="color: #0000FF;">DATABASE</span> bah <span style="color: #0000FF;">TO</span> <span style="color: #0000FF;">DISK</span> = <span style="color: #FF0000;">'C:\bah.bak'</span><br />GO<br /><span style="color: #0000FF;">RESTORE</span> <span style="color: #0000FF;">DATABASE</span> <span style="color: #808080;">&#91;</span>bah<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">FROM</span> &nbsp;<br /><span style="color: #0000FF;">DISK</span> = N<span style="color: #FF0000;">'C:\bah.bak'</span> <br /><span style="color: #0000FF;">WITH</span> &nbsp;<span style="color: #0000FF;">FILE</span> = <span style="color: #000;">1</span>, &nbsp;STANDBY = N<span style="color: #FF0000;">'C:\ROLLBACK_UNDO_bah.BAK'</span>, &nbsp;<br />NOUNLOAD, &nbsp;<span style="color: #FF00FF;">REPLACE</span>, &nbsp;STATS = <span style="color: #000;">10</span><br />GO<br /><br /></div><div id="cb47071" style="display: none; color: red;"></div></div></div>
<p><br /><br />
We should now have, &#8220;bah (Standby /Read-Only) listed in our database tree.</p>

The regular Copy Database with either detaching or SMO is not ideal for us at this point.  We can however take this database offline, grab the mdf, ndf&#8217;s and ldf&#8217;s to bring to the other instance we want to restore them to.<br />
</p>
<p></p><p>
<p>To do this we perform the following steps<br />
<br /></p>
<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb47811" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">USE</span> MASTER<br />GO<br /><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">DATABASE</span> bah <span style="color: #0000FF;">SET</span> OFFLINE<br /><br /></div><div id="cb72512" style="display: none; color: red;"></div></div></div>

Now go to the default folder where the mdf and other files are located and copy/paste them to a local drive.  I mentioned local drive because we need to do this as quickly as possible.  If there is enough free space locally, it will be the fastest copy transmission.  Otherwise at this stage it is crucial to ensure you have disabled the LS plans so no unwanted jobs attempt to access the database while it is offline.<br />
</p>
<p></p><p>
<p>Once you&#8217;ve copied the files off, run the following to bring the database back up</p>

<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb2825" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">USE</span> MASTER<br />GO<br /><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">DATABASE</span> bah <span style="color: #0000FF;">SET</span> ONLINE<br /><br /></div><div id="cb91150" style="display: none; color: red;"></div></div></div>

<p>We should see the database has come back up in standby mode and log shipping can proceed as normal.</p>

Next, we only need to attach the files to a new instance given the following commands or using SSMS attach/detach wizard.<br />
</p>
<p></p><p>
<blockquote><p>Note:  You cannot bring these files up on the same instance.  This is due to being required to leave the database "as is" and with the same file names.  When the database was taken offline, it was in standby mode and will be the same when attached again during the initial loading.  Once on the new instance, you can bring the database into any state you require and move it back to the other instance is required.</p></blockquote>
<p>On the secondary instance do the following steps</p>

<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb9558" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">DATABASE</span> bah<br /><span style="color: #0000FF;">ON</span> <br /><span style="color: #808080;">&#40;</span> NAME = bah_data,FILENAME = <span style="color: #FF0000;">'c:\restore\bah.mdf'</span><span style="color: #808080;">&#41;</span><br /><span style="color: #FF00FF;">LOG</span> <span style="color: #0000FF;">ON</span><br /><span style="color: #808080;">&#40;</span> NAME = <span style="color: #FF0000;">'bah_log'</span>,<br />&nbsp; &nbsp;FILENAME = <span style="color: #FF0000;">'c:\restore\bah_1.ldf'</span> <span style="color: #808080;">&#41;</span><br />GO<br /><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">DATABASE</span> bah <span style="color: #0000FF;">SET</span> OFFLINE<br />GO<br /><span style="color: #008080;">--Copy the bah files from our original standby database into the location you just created the </span><br /><span style="color: #008080;">--new database to. &nbsp;in my case C:\restore\ </span><br /><span style="color: #008080;">--make sure you replace the files and that the file names, both data and logs are identical to the original</span><br /><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">DATABASE</span> bah <span style="color: #0000FF;">SET</span> ONLINE<br />GO<br /><br /></div><div id="cb51735" style="display: none; color: red;"></div></div></div>
<p><br /><br />
Now we can see already in the tree of object explorer that the database online and read-write state.  There is no need to issue a restore to recovery or anything.  </p>

<p>To verify, we can check the read only state in sys.databases</p>

<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb64650" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">SELECT</span> is_read_only <span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">databases</span> <span style="color: #0000FF;">WHERE</span> <span style="color: #808080;">&#91;</span>name<span style="color: #808080;">&#93;</span> = <span style="color: #FF0000;">'bah'</span><br /><br /></div><div id="cb3845" style="display: none; color: red;"></div></div></div>
This will result in 0 meaning the database is read-write<br />
</p>
<p></p><p>
We now have a backup of a warm-standby database that is used in a log shipping plan.
</p><a href="http://api.tweetmeme.com/share?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/backup-and-copy-warm-standby-log-shipped"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/backup-and-copy-warm-standby-log-shipped&amp;source=LessThanDot" height="61" width="51" /></a><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/backup-and-copy-warm-standby-log-shipped">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/backup-and-copy-warm-standby-log-shipped#comments</comments>
		</item>
				<item>
			<title>Use sys.dm_os_performance_counters to get your Buffer cache hit ratio and Page life expectancy counters</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/use-sys-dm_os_performance_counters-to-ge</link>
			<pubDate>Fri, 22 Jan 2010 15:23:49 +0000</pubDate>			<dc:creator>SQLDenis</dc:creator>
			<category domain="alt">Database Programming</category>
<category domain="alt">Database Administration</category>
<category domain="main">Microsoft SQL Server Admin</category>
<category domain="alt">Microsoft SQL Server</category>			<guid isPermaLink="false">735@http://blogs.lessthandot.com/</guid>
						<description>&lt;h3&gt;by &lt;a href=&quot;http://blogs.lessthandot.com/index.php/All/?disp=authdir&amp;amp;author=4&quot;&gt; SQLDenis &lt;/a&gt;&lt;/h3&gt;&lt;p&gt;In order to figure out if you need more memory for a SQL Server you can start by taking a look at Buffer cache hit ratio and Page life expectancy.&lt;/p&gt;

&lt;h2&gt;Buffer cache hit ratio&lt;/h2&gt;
&lt;p&gt;Here is what Books On Line has to say about Buffer cache hit ratio&lt;br /&gt;
&lt;em&gt;Buffer cache hit ratio&lt;br /&gt;
Percentage of pages found in the buffer cache without having to read from disk. The ratio is the total number of cache hits divided by the total number of cache lookups over the last few thousand page accesses. After a long period of time, the ratio moves very little. Because reading from the cache is much less expensive than reading from disk, you want this ratio to be high. Generally, you can increase the buffer cache hit ratio by increasing the amount of memory available to SQL Server.&lt;br /&gt;
&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Basically what this means is what is the percentage that SQL Server had the data in cache and did not have to read the data from disk. Ideally you want this number to be as close to 100 as possible.&lt;/p&gt;

&lt;p&gt;In order to calculate the Buffer cache hit ratio we need to query the sys.dm_os_performance_counters dynamic management view. There are 2 counters we need in order to do our calculation, one counter is Buffer cache hit ratio and the other counter is Buffer cache hit ratio base. We divide &lt;em&gt;Buffer cache hit ratio&lt;/em&gt; base by &lt;em&gt;Buffer cache hit ratio&lt;/em&gt; and it will give us the Buffer cache hit ratio.&lt;br /&gt;
Here is the query that will do that, this query will only work on SQL Server 2005 and up.&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;cb6730&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: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;a.&lt;span style=&quot;color: #202020;&quot;&gt;cntr_value&lt;/span&gt; * &lt;span style=&quot;color: #000;&quot;&gt;1.0&lt;/span&gt; / b.&lt;span style=&quot;color: #202020;&quot;&gt;cntr_value&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; * &lt;span style=&quot;color: #000;&quot;&gt;100.0&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; BufferCacheHitRatio&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; sys.&lt;span style=&quot;color: #202020;&quot;&gt;dm_os_performance_counters&lt;/span&gt; &amp;nbsp;a&lt;br /&gt;JOIN &amp;nbsp;&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; cntr_value,&lt;span style=&quot;color: #FF00FF;&quot;&gt;OBJECT_NAME&lt;/span&gt; &lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; sys.&lt;span style=&quot;color: #202020;&quot;&gt;dm_os_performance_counters&lt;/span&gt; &amp;nbsp;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; counter_name = &lt;span style=&quot;color: #FF0000;&quot;&gt;'Buffer cache hit ratio base'&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; AND &lt;span style=&quot;color: #FF00FF;&quot;&gt;OBJECT_NAME&lt;/span&gt; = &lt;span style=&quot;color: #FF0000;&quot;&gt;'SQLServer:Buffer Manager'&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; b &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; &amp;nbsp;a.&lt;span style=&quot;color: #FF00FF;&quot;&gt;OBJECT_NAME&lt;/span&gt; = b.&lt;span style=&quot;color: #FF00FF;&quot;&gt;OBJECT_NAME&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; a.&lt;span style=&quot;color: #202020;&quot;&gt;counter_name&lt;/span&gt; = &lt;span style=&quot;color: #FF0000;&quot;&gt;'Buffer cache hit ratio'&lt;/span&gt;&lt;br /&gt;AND a.&lt;span style=&quot;color: #FF00FF;&quot;&gt;OBJECT_NAME&lt;/span&gt; = &lt;span style=&quot;color: #FF0000;&quot;&gt;'SQLServer:Buffer Manager'&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb51178&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;p&gt; &lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;
&lt;h2&gt;Page life expectancy&lt;/h2&gt;
&lt;p&gt;Now let's look at Page life expectancy.&lt;br /&gt;
Page life expectancy is the number of seconds a page will stay in the buffer pool, ideally it should be above 300 seconds. If it is less than 300 seconds this could indicate memory pressure, a cache flush or missing indexes.&lt;/p&gt;

&lt;p&gt;Here is how to get the Page life expectancy&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;cb25762&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;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; sys.&lt;span style=&quot;color: #202020;&quot;&gt;dm_os_performance_counters&lt;/span&gt; &amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; counter_name = &lt;span style=&quot;color: #FF0000;&quot;&gt;'Page life expectancy'&lt;/span&gt;&lt;br /&gt;AND &lt;span style=&quot;color: #FF00FF;&quot;&gt;OBJECT_NAME&lt;/span&gt; = &lt;span style=&quot;color: #FF0000;&quot;&gt;'SQLServer:Buffer Manager'&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb61136&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;



&lt;p&gt;What I currently get for the queries is a Page life expectancy of 470333 and the Buffer cache hit ratio is 100.&lt;strong&gt; What I would like you to do is run these 2 queries on your systems and leave me the results in a comment so that we can compare&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Also take a look at how to capture this info if you prefer to run perfmon (or if you are still running SQL Server 2000) by reading this excellent article by Brent Ozar here: &lt;a href=&quot;http://www.brentozar.com/archive/2006/12/dba-101-using-perfmon-for-sql-performance-tuning/&quot;&gt;SQL Server Perfmon (Performance Monitor) Best Practices&lt;/a&gt;&lt;/p&gt;


&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
*** &lt;strong&gt;Remember, if you have a SQL related question try our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=17&quot;&gt;Microsoft SQL Server Programming&lt;/a&gt; forum or our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=22&quot;&gt;Microsoft SQL Server Admin&lt;/a&gt; forum&lt;/strong&gt;&lt;ins&gt;&lt;/ins&gt;&lt;/p&gt;&lt;a href=&quot;http://api.tweetmeme.com/share?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/use-sys-dm_os_performance_counters-to-ge&quot;&gt;&lt;img src=&quot;http://api.tweetmeme.com/imagebutton.gif?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/use-sys-dm_os_performance_counters-to-ge&amp;source=LessThanDot&quot; height=&quot;61&quot; width=&quot;51&quot; /&gt;&lt;/a&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/use-sys-dm_os_performance_counters-to-ge&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[				<h3>by <a href="http://blogs.lessthandot.com/index.php/All/?disp=authdir&amp;author=4"> SQLDenis </a></h3>
				<p>In order to figure out if you need more memory for a SQL Server you can start by taking a look at Buffer cache hit ratio and Page life expectancy.</p>

<h2>Buffer cache hit ratio</h2>
<p>Here is what Books On Line has to say about Buffer cache hit ratio<br />
<em>Buffer cache hit ratio<br />
Percentage of pages found in the buffer cache without having to read from disk. The ratio is the total number of cache hits divided by the total number of cache lookups over the last few thousand page accesses. After a long period of time, the ratio moves very little. Because reading from the cache is much less expensive than reading from disk, you want this ratio to be high. Generally, you can increase the buffer cache hit ratio by increasing the amount of memory available to SQL Server.<br />
</em></p>

<p>Basically what this means is what is the percentage that SQL Server had the data in cache and did not have to read the data from disk. Ideally you want this number to be as close to 100 as possible.</p>

<p>In order to calculate the Buffer cache hit ratio we need to query the sys.dm_os_performance_counters dynamic management view. There are 2 counters we need in order to do our calculation, one counter is Buffer cache hit ratio and the other counter is Buffer cache hit ratio base. We divide <em>Buffer cache hit ratio</em> base by <em>Buffer cache hit ratio</em> and it will give us the Buffer cache hit ratio.<br />
Here is the query that will do that, this query will only work on SQL Server 2005 and up.</p>

<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb59411" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">&#40;</span>a.<span style="color: #202020;">cntr_value</span> * <span style="color: #000;">1.0</span> / b.<span style="color: #202020;">cntr_value</span><span style="color: #808080;">&#41;</span> * <span style="color: #000;">100.0</span> <span style="color: #0000FF;">AS</span> BufferCacheHitRatio<br /><span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">dm_os_performance_counters</span> &nbsp;a<br />JOIN &nbsp;<span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> cntr_value,<span style="color: #FF00FF;">OBJECT_NAME</span> <br />&nbsp; &nbsp; <span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">dm_os_performance_counters</span> &nbsp;<br />&nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> counter_name = <span style="color: #FF0000;">'Buffer cache hit ratio base'</span><br />&nbsp; &nbsp; &nbsp; &nbsp; AND <span style="color: #FF00FF;">OBJECT_NAME</span> = <span style="color: #FF0000;">'SQLServer:Buffer Manager'</span><span style="color: #808080;">&#41;</span> b <span style="color: #0000FF;">ON</span> &nbsp;a.<span style="color: #FF00FF;">OBJECT_NAME</span> = b.<span style="color: #FF00FF;">OBJECT_NAME</span><br /><span style="color: #0000FF;">WHERE</span> a.<span style="color: #202020;">counter_name</span> = <span style="color: #FF0000;">'Buffer cache hit ratio'</span><br />AND a.<span style="color: #FF00FF;">OBJECT_NAME</span> = <span style="color: #FF0000;">'SQLServer:Buffer Manager'</span><br /><br /></div><div id="cb38866" style="display: none; color: red;"></div></div></div><p> <br />
<br /><br /></p>
<h2>Page life expectancy</h2>
<p>Now let's look at Page life expectancy.<br />
Page life expectancy is the number of seconds a page will stay in the buffer pool, ideally it should be above 300 seconds. If it is less than 300 seconds this could indicate memory pressure, a cache flush or missing indexes.</p>

<p>Here is how to get the Page life expectancy</p>
<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb78560" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">SELECT</span> *<br /><span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">dm_os_performance_counters</span> &nbsp;<br /><span style="color: #0000FF;">WHERE</span> counter_name = <span style="color: #FF0000;">'Page life expectancy'</span><br />AND <span style="color: #FF00FF;">OBJECT_NAME</span> = <span style="color: #FF0000;">'SQLServer:Buffer Manager'</span><br /><br /></div><div id="cb40794" style="display: none; color: red;"></div></div></div>



<p>What I currently get for the queries is a Page life expectancy of 470333 and the Buffer cache hit ratio is 100.<strong> What I would like you to do is run these 2 queries on your systems and leave me the results in a comment so that we can compare</strong></p>

<p>Also take a look at how to capture this info if you prefer to run perfmon (or if you are still running SQL Server 2000) by reading this excellent article by Brent Ozar here: <a href="http://www.brentozar.com/archive/2006/12/dba-101-using-perfmon-for-sql-performance-tuning/">SQL Server Perfmon (Performance Monitor) Best Practices</a></p>


<p><br /><br /><br />
*** <strong>Remember, if you have a SQL related question try our <a href="http://forum.lessthandot.com/viewforum.php?f=17">Microsoft SQL Server Programming</a> forum or our <a href="http://forum.lessthandot.com/viewforum.php?f=22">Microsoft SQL Server Admin</a> forum</strong><ins></ins></p><a href="http://api.tweetmeme.com/share?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/use-sys-dm_os_performance_counters-to-ge"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/use-sys-dm_os_performance_counters-to-ge&amp;source=LessThanDot" height="61" width="51" /></a><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/use-sys-dm_os_performance_counters-to-ge">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/use-sys-dm_os_performance_counters-to-ge#comments</comments>
		</item>
				<item>
			<title>Use alter table alter column to change datatypes for a column in SQL Server</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/use-alter-table-alter-column-to-change-d</link>
			<pubDate>Tue, 19 Jan 2010 18:10:48 +0000</pubDate>			<dc:creator>SQLDenis</dc:creator>
			<category domain="main">Data Modelling &amp; Design</category>
<category domain="alt">Database Programming</category>
<category domain="alt">Database Administration</category>
<category domain="alt">Microsoft SQL Server Admin</category>
<category domain="alt">Microsoft SQL Server</category>			<guid isPermaLink="false">730@http://blogs.lessthandot.com/</guid>
						<description>&lt;h3&gt;by &lt;a href=&quot;http://blogs.lessthandot.com/index.php/All/?disp=authdir&amp;amp;author=4&quot;&gt; SQLDenis &lt;/a&gt;&lt;/h3&gt;&lt;p&gt;This question popped in the&lt;a href=&quot;http://groups.google.com/group/microsoft.public.sqlserver.programming/browse_thread/thread/91c5da9982cfb1cf?hl=en#&quot;&gt; microsoft.public.sqlserver.programming&lt;/a&gt; forum yesterday. A person wanted to change a column from varchar 500 to varchar 2000. This is actually pretty easy to do in SQL Server, you can use the &lt;em&gt;alter table alter column&lt;/em&gt; command&lt;br /&gt;
Let's take a quick look at how this works&lt;br /&gt;
First create this table&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;cb59851&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;TABLE&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;TestTable&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;id&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&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; NOT NULL ,&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;itemdate&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DATETIME&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; NOT NULL ,&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;title&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;500&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; NOT NULL ,&lt;br /&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;PRIMARY&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb14925&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now insert one row of 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;cb97340&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;TestTable&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;'bla'&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb5785&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now do a simple select and verify that you have one row of 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;cb4541&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; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;TestTable&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb41719&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Results&lt;/p&gt;


&lt;pre&gt;id          itemdate                title
----------- ----------------------- ----------
1           2010-01-18 17:28:19.820 bla&lt;/pre&gt;

&lt;p&gt;We will change the column from varchar 500 to varchar 2000. All you have to do is alter table [table name] alter column [column name] [new data type and length]&lt;br /&gt;
So in this case the command would look like this&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;cb32997&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ALTER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;TestTable&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ALTER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;COLUMN&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;title&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;2000&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;cb22896&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To verify that indeed the column is now varchar 2000 run 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;cb30891&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; INFORMATION_SCHEMA.&lt;span style=&quot;color: #202020;&quot;&gt;COLUMNS&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; table_name = &lt;span style=&quot;color: #FF0000;&quot;&gt;'TestTable'&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb89279&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Results&lt;/p&gt;


&lt;pre&gt;COLUMN_NAME    DATA_TYPE       CHARACTER_MAXIMUM_LENGTH
-------------- -----------------------------------------
id              int             NULL
itemdate        datetime        NULL
title           varchar         2000&lt;/pre&gt;


&lt;p&gt;Now let's change the datetime column to a varchar, execute 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;cb38082&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ALTER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;TestTable&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ALTER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;COLUMN&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;itemdate&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #000;&quot;&gt;49&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;cb15531&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now verify that it was changed&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;cb61232&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; INFORMATION_SCHEMA.&lt;span style=&quot;color: #202020;&quot;&gt;COLUMNS&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; table_name = &lt;span style=&quot;color: #FF0000;&quot;&gt;'TestTable'&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb13848&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Results&lt;/p&gt;


&lt;pre&gt;COLUMN_NAME    DATA_TYPE       CHARACTER_MAXIMUM_LENGTH
-------------- -----------------------------------------
id              int             NULL
itemdate        varchar         49
title           varchar         2000&lt;/pre&gt;

&lt;p&gt;Insert a new row&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;cb49703&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;TestTable&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;'bla'&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb77358&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now check what is in the table&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;cb54994&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; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;TestTable&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb35614&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Results&lt;/p&gt;


&lt;pre&gt;id          itemdate                                          title
----------- ------------------------------------------------- -----------
1           Jan 18 2010  5:28PM                               bla
2           Jan 18 2010  5:30PM                               bla&lt;/pre&gt;

&lt;p&gt;Now we will add a column. The command to add a column is very similar to the one where you alter a column, the difference is that you don't use the column keyword. Below is a query that will add an int column with a default of 0&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;cb42644&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ALTER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;TestTable&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ADD&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;SomeInt&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INT&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;DEFAULT&lt;/span&gt; &lt;span style=&quot;color: #000;&quot;&gt;0&lt;/span&gt; not null&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb8360&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;


&lt;p&gt;Run this query to see what is in the table now&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;cb73471&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; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;TestTable&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb21250&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Results&lt;/p&gt;


&lt;pre&gt;id          itemdate                    title     SomeInt
----------- ---------------------------- ---------- -----------
1           Jan 18 2010  5:28PM         bla       0
2           Jan 18 2010  5:30PM         bla       0&lt;/pre&gt;

&lt;p&gt;As you can see the column was added and the default of 0 was used.&lt;/p&gt;

&lt;p&gt;We can interrogate the INFORMATION_SCHEMA.COLUMNS view again to verify that the column is there&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;cb41603&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; INFORMATION_SCHEMA.&lt;span style=&quot;color: #202020;&quot;&gt;COLUMNS&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; table_name = &lt;span style=&quot;color: #FF0000;&quot;&gt;'TestTable'&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb48676&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Results&lt;/p&gt;

&lt;pre&gt;COLUMN_NAME    DATA_TYPE       CHARACTER_MAXIMUM_LENGTH
-------------- -----------------------------------------
id              int             NULL
itemdate        varchar         49
title           varchar         2000
SomeInt		int		NULL&lt;/pre&gt;

&lt;p&gt;Now let's insert a row, we will use a value of 2 for the newly added SomeInt 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;cb69030&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;TestTable&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; &lt;span style=&quot;color: #FF00FF;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;,&lt;span style=&quot;color: #FF0000;&quot;&gt;'bla'&lt;/span&gt;,&lt;span style=&quot;color: #000;&quot;&gt;2&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb73567&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;When we run this 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;cb64416&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; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;TestTable&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb97442&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can see that the value 2 is there&lt;/p&gt;

&lt;p&gt;Results&lt;/p&gt;


&lt;pre&gt;id          itemdate			title     SomeInt
----------- ------------------------------------------------- 
1           Jan 18 2010  5:28PM           bla       0
2           Jan 18 2010  5:30PM           bla       0
3           Jan 18 2010  5:33PM           bla       2&lt;/pre&gt;


&lt;p&gt;If you try to change a column to a datatype which is incompatible with the data that is stored you will get an error message&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;cb73853&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;ALTER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;TABLE&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;TestTable&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;ALTER&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;COLUMN&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;itemdate&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;INT&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb99052&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;


&lt;p&gt;Here is the error for the above command&lt;br /&gt;
&lt;strong&gt;Msg 245, Level 16, State 1, Line 1&lt;br /&gt;
Conversion failed when converting the varchar value 'Jan 18 2010  5:12PM' to data type int.&lt;br /&gt;
The statement has been terminated.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That is it for this post, as you can see it is pretty easy to change the data type of a column by using the alter table alter column command&lt;/p&gt;



&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
*** &lt;strong&gt;Remember, if you have a SQL related question try our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=17&quot;&gt;Microsoft SQL Server Programming&lt;/a&gt; forum or our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=22&quot;&gt;Microsoft SQL Server Admin&lt;/a&gt; forum&lt;/strong&gt;&lt;ins&gt;&lt;/ins&gt;&lt;/p&gt;&lt;a href=&quot;http://api.tweetmeme.com/share?url=http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/use-alter-table-alter-column-to-change-d&quot;&gt;&lt;img src=&quot;http://api.tweetmeme.com/imagebutton.gif?url=http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/use-alter-table-alter-column-to-change-d&amp;source=LessThanDot&quot; height=&quot;61&quot; width=&quot;51&quot; /&gt;&lt;/a&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/DataDesign/use-alter-table-alter-column-to-change-d&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[				<h3>by <a href="http://blogs.lessthandot.com/index.php/All/?disp=authdir&amp;author=4"> SQLDenis </a></h3>
				<p>This question popped in the<a href="http://groups.google.com/group/microsoft.public.sqlserver.programming/browse_thread/thread/91c5da9982cfb1cf?hl=en#"> microsoft.public.sqlserver.programming</a> forum yesterday. A person wanted to change a column from varchar 500 to varchar 2000. This is actually pretty easy to do in SQL Server, you can use the <em>alter table alter column</em> command<br />
Let's take a quick look at how this works<br />
First create this table</p>

<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb27637" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#91;</span>TestTable<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#40;</span><br /><span style="color: #808080;">&#91;</span>id<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">INT</span><span style="color: #808080;">&#93;</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> NOT NULL ,<br /><span style="color: #808080;">&#91;</span>itemdate<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">DATETIME</span><span style="color: #808080;">&#93;</span> NOT NULL ,<br /><span style="color: #808080;">&#91;</span>title<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">500</span><span style="color: #808080;">&#41;</span> NOT NULL ,<br /><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">ON</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">PRIMARY</span><span style="color: #808080;">&#93;</span><br /><br /></div><div id="cb33705" style="display: none; color: red;"></div></div></div>

<p>Now insert one row of data</p>

<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb13978" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">INSERT</span> <span style="color: #808080;">&#91;</span>TestTable<span style="color: #808080;">&#93;</span><br /><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>,<span style="color: #FF0000;">'bla'</span><br /><br /></div><div id="cb24978" style="display: none; color: red;"></div></div></div>

<p>Now do a simple select and verify that you have one row of data</p>

<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb39490" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">SELECT</span> * <span style="color: #0000FF;">FROM</span> <span style="color: #808080;">&#91;</span>TestTable<span style="color: #808080;">&#93;</span><br /><br /></div><div id="cb18519" style="display: none; color: red;"></div></div></div>

<p>Results</p>


<pre>id          itemdate                title
----------- ----------------------- ----------
1           2010-01-18 17:28:19.820 bla</pre>

<p>We will change the column from varchar 500 to varchar 2000. All you have to do is alter table [table name] alter column [column name] [new data type and length]<br />
So in this case the command would look like this</p>

<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb66698" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#91;</span>TestTable<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">COLUMN</span> <span style="color: #808080;">&#91;</span>title<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">2000</span><span style="color: #808080;">&#41;</span><br /><br /></div><div id="cb72488" style="display: none; color: red;"></div></div></div>

<p>To verify that indeed the column is now varchar 2000 run the following query</p>

<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb41416" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">SELECT</span> COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH<br /><span style="color: #0000FF;">FROM</span> INFORMATION_SCHEMA.<span style="color: #202020;">COLUMNS</span><br /><span style="color: #0000FF;">WHERE</span> table_name = <span style="color: #FF0000;">'TestTable'</span><br /><br /></div><div id="cb97589" style="display: none; color: red;"></div></div></div>

<p>Results</p>


<pre>COLUMN_NAME    DATA_TYPE       CHARACTER_MAXIMUM_LENGTH
-------------- -----------------------------------------
id              int             NULL
itemdate        datetime        NULL
title           varchar         2000</pre>


<p>Now let's change the datetime column to a varchar, execute the following query</p>
<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb61767" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#91;</span>TestTable<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">COLUMN</span> <span style="color: #808080;">&#91;</span>itemdate<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#93;</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">49</span><span style="color: #808080;">&#41;</span><br /><br /></div><div id="cb79499" style="display: none; color: red;"></div></div></div>

<p>Now verify that it was changed</p>
<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb13121" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">SELECT</span> COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH<br /><span style="color: #0000FF;">FROM</span> INFORMATION_SCHEMA.<span style="color: #202020;">COLUMNS</span><br /><span style="color: #0000FF;">WHERE</span> table_name = <span style="color: #FF0000;">'TestTable'</span><br /><br /></div><div id="cb23000" style="display: none; color: red;"></div></div></div>

<p>Results</p>


<pre>COLUMN_NAME    DATA_TYPE       CHARACTER_MAXIMUM_LENGTH
-------------- -----------------------------------------
id              int             NULL
itemdate        varchar         49
title           varchar         2000</pre>

<p>Insert a new row</p>

<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb93347" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">INSERT</span> <span style="color: #808080;">&#91;</span>TestTable<span style="color: #808080;">&#93;</span><br /><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>,<span style="color: #FF0000;">'bla'</span><br /><br /></div><div id="cb62824" style="display: none; color: red;"></div></div></div>

<p>Now check what is in the table</p>

<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb359" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">SELECT</span> * <span style="color: #0000FF;">FROM</span> <span style="color: #808080;">&#91;</span>TestTable<span style="color: #808080;">&#93;</span><br /><br /></div><div id="cb48342" style="display: none; color: red;"></div></div></div>

<p>Results</p>


<pre>id          itemdate                                          title
----------- ------------------------------------------------- -----------
1           Jan 18 2010  5:28PM                               bla
2           Jan 18 2010  5:30PM                               bla</pre>

<p>Now we will add a column. The command to add a column is very similar to the one where you alter a column, the difference is that you don't use the column keyword. Below is a query that will add an int column with a default of 0</p>

<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb98439" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#91;</span>TestTable<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">ADD</span> &nbsp;<span style="color: #808080;">&#91;</span>SomeInt<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">INT</span> <span style="color: #0000FF;">DEFAULT</span> <span style="color: #000;">0</span> not null<br /><br /></div><div id="cb43003" style="display: none; color: red;"></div></div></div>


<p>Run this query to see what is in the table now</p>
<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb56702" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">SELECT</span> * <span style="color: #0000FF;">FROM</span> <span style="color: #808080;">&#91;</span>TestTable<span style="color: #808080;">&#93;</span><br /><br /></div><div id="cb71910" style="display: none; color: red;"></div></div></div>

<p>Results</p>


<pre>id          itemdate                    title     SomeInt
----------- ---------------------------- ---------- -----------
1           Jan 18 2010  5:28PM         bla       0
2           Jan 18 2010  5:30PM         bla       0</pre>

<p>As you can see the column was added and the default of 0 was used.</p>

<p>We can interrogate the INFORMATION_SCHEMA.COLUMNS view again to verify that the column is there</p>

<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb64254" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">SELECT</span> COLUMN_NAME,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH<br /><span style="color: #0000FF;">FROM</span> INFORMATION_SCHEMA.<span style="color: #202020;">COLUMNS</span><br /><span style="color: #0000FF;">WHERE</span> table_name = <span style="color: #FF0000;">'TestTable'</span><br /><br /></div><div id="cb98306" style="display: none; color: red;"></div></div></div>

<p>Results</p>

<pre>COLUMN_NAME    DATA_TYPE       CHARACTER_MAXIMUM_LENGTH
-------------- -----------------------------------------
id              int             NULL
itemdate        varchar         49
title           varchar         2000
SomeInt		int		NULL</pre>

<p>Now let's insert a row, we will use a value of 2 for the newly added SomeInt column</p>
<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb20587" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">INSERT</span> <span style="color: #808080;">&#91;</span>TestTable<span style="color: #808080;">&#93;</span><br /><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>,<span style="color: #FF0000;">'bla'</span>,<span style="color: #000;">2</span><br /><br /></div><div id="cb33285" style="display: none; color: red;"></div></div></div>

<p>When we run this query again</p>
<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb71874" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">SELECT</span> * <span style="color: #0000FF;">FROM</span> <span style="color: #808080;">&#91;</span>TestTable<span style="color: #808080;">&#93;</span><br /><br /></div><div id="cb85003" style="display: none; color: red;"></div></div></div>

<p>We can see that the value 2 is there</p>

<p>Results</p>


<pre>id          itemdate			title     SomeInt
----------- ------------------------------------------------- 
1           Jan 18 2010  5:28PM           bla       0
2           Jan 18 2010  5:30PM           bla       0
3           Jan 18 2010  5:33PM           bla       2</pre>


<p>If you try to change a column to a datatype which is incompatible with the data that is stored you will get an error message</p>
<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb30727" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#91;</span>TestTable<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">COLUMN</span> <span style="color: #808080;">&#91;</span>itemdate<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">INT</span><br /><br /></div><div id="cb45727" style="display: none; color: red;"></div></div></div>


<p>Here is the error for the above command<br />
<strong>Msg 245, Level 16, State 1, Line 1<br />
Conversion failed when converting the varchar value 'Jan 18 2010  5:12PM' to data type int.<br />
The statement has been terminated.</strong></p>

<p>That is it for this post, as you can see it is pretty easy to change the data type of a column by using the alter table alter column command</p>



<p><br /><br /><br />
*** <strong>Remember, if you have a SQL related question try our <a href="http://forum.lessthandot.com/viewforum.php?f=17">Microsoft SQL Server Programming</a> forum or our <a href="http://forum.lessthandot.com/viewforum.php?f=22">Microsoft SQL Server Admin</a> forum</strong><ins></ins></p><a href="http://api.tweetmeme.com/share?url=http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/use-alter-table-alter-column-to-change-d"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/use-alter-table-alter-column-to-change-d&amp;source=LessThanDot" height="61" width="51" /></a><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DataDesign/use-alter-table-alter-column-to-change-d">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/DataDesign/use-alter-table-alter-column-to-change-d#comments</comments>
		</item>
				<item>
			<title>SQL Server 2008 R2 to be released in May</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/sql-server-2008-r2-to-be-released-in-may</link>
			<pubDate>Tue, 19 Jan 2010 17:10:42 +0000</pubDate>			<dc:creator>SQLDenis</dc:creator>
			<category domain="alt">Data Modelling &amp; Design</category>
<category domain="alt">Database Programming</category>
<category domain="alt">Database Administration</category>
<category domain="main">Microsoft SQL Server Admin</category>
<category domain="alt">Microsoft SQL Server</category>			<guid isPermaLink="false">732@http://blogs.lessthandot.com/</guid>
						<description>&lt;h3&gt;by &lt;a href=&quot;http://blogs.lessthandot.com/index.php/All/?disp=authdir&amp;amp;author=4&quot;&gt; SQLDenis &lt;/a&gt;&lt;/h3&gt;&lt;p&gt;A quick post. It has just been announced,  SQL Server 2008 R2 will be released in May.&lt;/p&gt;

&lt;blockquote&gt;&lt;p&gt;Customers with Software Assurance can upgrade to SQL Server 2008 R2 and take advantage of the new features without incurring additional licensing costs.&lt;/p&gt;&lt;/blockquote&gt;

&lt;p&gt;I am glad I have Software Assurance so hopefully I can roll this out sometimes this year&lt;/p&gt;

&lt;p&gt;More about this announcement here: &lt;a href=&quot;http://blogs.technet.com/dataplatforminsider/archive/2010/01/19/sql-server-2008-r2-gets-an-official-date.aspx&quot;&gt;http://blogs.technet.com/dataplatforminsider/archive/2010/01/19/sql-server-2008-r2-gets-an-official-date.aspx&lt;/a&gt;&lt;/p&gt;&lt;a href=&quot;http://api.tweetmeme.com/share?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/sql-server-2008-r2-to-be-released-in-may&quot;&gt;&lt;img src=&quot;http://api.tweetmeme.com/imagebutton.gif?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/sql-server-2008-r2-to-be-released-in-may&amp;source=LessThanDot&quot; height=&quot;61&quot; width=&quot;51&quot; /&gt;&lt;/a&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/sql-server-2008-r2-to-be-released-in-may&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[				<h3>by <a href="http://blogs.lessthandot.com/index.php/All/?disp=authdir&amp;author=4"> SQLDenis </a></h3>
				<p>A quick post. It has just been announced,  SQL Server 2008 R2 will be released in May.</p>

<blockquote><p>Customers with Software Assurance can upgrade to SQL Server 2008 R2 and take advantage of the new features without incurring additional licensing costs.</p></blockquote>

<p>I am glad I have Software Assurance so hopefully I can roll this out sometimes this year</p>

<p>More about this announcement here: <a href="http://blogs.technet.com/dataplatforminsider/archive/2010/01/19/sql-server-2008-r2-gets-an-official-date.aspx">http://blogs.technet.com/dataplatforminsider/archive/2010/01/19/sql-server-2008-r2-gets-an-official-date.aspx</a></p><a href="http://api.tweetmeme.com/share?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/sql-server-2008-r2-to-be-released-in-may"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/sql-server-2008-r2-to-be-released-in-may&amp;source=LessThanDot" height="61" width="51" /></a><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/MSSQLServerAdmin/sql-server-2008-r2-to-be-released-in-may">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/sql-server-2008-r2-to-be-released-in-may#comments</comments>
		</item>
				<item>
			<title>Activity Monitor to gather statistics and health of SQL Server 2008</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/capture-data-from-activity-monitor-2008</link>
			<pubDate>Mon, 18 Jan 2010 14:53:31 +0000</pubDate>			<dc:creator>onpnt</dc:creator>
			<category domain="alt">Database Programming</category>
<category domain="main">Database Administration</category>
<category domain="alt">Microsoft SQL Server</category>			<guid isPermaLink="false">727@http://blogs.lessthandot.com/</guid>
						<description>&lt;h3&gt;by &lt;a href=&quot;http://blogs.lessthandot.com/index.php/All/?disp=authdir&amp;amp;author=68&quot;&gt; onpnt &lt;/a&gt;&lt;/h3&gt;&lt;p&gt;The enhancements to the Activity Monitor were an extremely useful addition to SQL Server 2008.  We can gather a moving view of statistics and the health of the instances we connect to.  In the real world however, the activity monitor loses value when you need to connect to dozens of SQL Servers in order to obtain that moving feel of each server you manage.  There is also the nuisance of being required to open a separate Activity Monitor instance for each server we connect to from SSMS.  There is a way we can eliminate those aspects though.&lt;br /&gt;
&lt;br /&gt;
One method we can use is SQL Server Profiler to capture and reuse valuable information gathered from the new Activity Monitor.  Profiler allows us to see exactly what is populating the tables behind the engine.  
&lt;/p&gt;
&lt;h2&gt;Let&amp;#8217;s take a look&lt;/h2&gt;
&lt;p&gt;
Open SSMS 2008 and connect to a development instance.  Once open, right click the server name in object explorer and select Activity Monitor from the list.&lt;br /&gt;
&lt;br /&gt;
Now open SQL Server Profiler from the performance tools section located in the all programs section of SQL Server 2008.  &lt;br /&gt;
&lt;br /&gt;
Create a new trace and either save to a table or a file for the output.  Saving to a table for this type of task is useful so we can query the results later and will not be a heavy performance issue as the trace will not be running long.  Next, click the Event Selection so we can filter out most of the batch statements that we don&amp;#8217;t want to see.  I put my filter to only show ApplicationName value of &amp;#8220;Microsoft SQL Server Management Studio&amp;#8221; and also LoginName of myself.  &lt;br /&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;img src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/am2.png&quot; alt=&quot;&quot; title=&quot;&quot; width=&quot;480&quot; height=&quot;315&quot; /&gt;&lt;/div&gt;
&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;&lt;br /&gt;
Once that is completely setup, hit Run to start the trace.&lt;/p&gt;

&lt;p&gt;
You should start to see all of the main statements that are driving the Activity Monitor start coming into profiler.  &lt;br /&gt;
&lt;br /&gt;
We can see that the queries are based on several DMV/DMF and are being inserted into temporary tables for the Activity Monitor to show.  At this point the query isn&amp;#8217;t helpful sense the data is being inserted into temporary tables we cannot control.  The temporary tables are already created by the monitor so recreating them manually ourselves would also cause a problem.  We can use these queries though to manage our own results by simple changes in them and executing them in tasks we create ourselves.&lt;br /&gt;
&lt;br /&gt;
Let&amp;#8217;s show this by grabbing the query that populates the file information in the &amp;#8220;Data File I/O&amp;#8221; section of the Activity Monitor.  The query that does this should look like this in the beginning of the batch&lt;br /&gt;
&lt;div class=&quot;image_block&quot;&gt;&lt;img src=&quot;http://blogs.lessthandot.com/media/blogs/DataMgmt/am.gif&quot; alt=&quot;&quot; title=&quot;&quot; width=&quot;555&quot; height=&quot;177&quot; /&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;p&gt;To use this query for ourselves, all we need to do is comment out the INSERT INTO #am_dbfilestats and we can either convert the INSERT INTO to a SELECT INTO statement with our own temporary table or simply use the query as a direct SELECT &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;cb90196&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;br /&gt;&amp;nbsp; &amp;nbsp; d.&lt;span style=&quot;color: #202020;&quot;&gt;name&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DATABASE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; f.&lt;span style=&quot;color: #202020;&quot;&gt;physical_name&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FILE&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;fs.&lt;span style=&quot;color: #202020;&quot;&gt;num_of_bytes_read&lt;/span&gt; / &lt;span style=&quot;color: #000;&quot;&gt;1024.0&lt;/span&gt; / &lt;span style=&quot;color: #000;&quot;&gt;1024.0&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;#91;&lt;/span&gt;Total MB &lt;span style=&quot;color: #0000FF;&quot;&gt;READ&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;fs.&lt;span style=&quot;color: #202020;&quot;&gt;num_of_bytes_written&lt;/span&gt; / &lt;span style=&quot;color: #000;&quot;&gt;1024.0&lt;/span&gt; / &lt;span style=&quot;color: #000;&quot;&gt;1024.0&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; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Total MB Written&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;fs.&lt;span style=&quot;color: #202020;&quot;&gt;num_of_reads&lt;/span&gt; + fs.&lt;span style=&quot;color: #202020;&quot;&gt;num_of_writes&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; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Total I/O &lt;span style=&quot;color: #FF00FF;&quot;&gt;COUNT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; fs.&lt;span style=&quot;color: #202020;&quot;&gt;io_stall&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;Total I/O Wait &lt;span style=&quot;color: #0000FF;&quot;&gt;TIME&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;ms&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;, &lt;br /&gt;&amp;nbsp; &amp;nbsp; fs.&lt;span style=&quot;color: #202020;&quot;&gt;size_on_disk_bytes&lt;/span&gt; / &lt;span style=&quot;color: #000;&quot;&gt;1024&lt;/span&gt; / &lt;span style=&quot;color: #000;&quot;&gt;1024&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SIZE&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;MB&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; sys.&lt;span style=&quot;color: #202020;&quot;&gt;dm_io_virtual_file_stats&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;DEFAULT&lt;/span&gt;, &lt;span style=&quot;color: #0000FF;&quot;&gt;DEFAULT&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; fs&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;INNER&lt;/span&gt; JOIN sys.&lt;span style=&quot;color: #202020;&quot;&gt;master_files&lt;/span&gt; f &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; fs.&lt;span style=&quot;color: #202020;&quot;&gt;database_id&lt;/span&gt; = f.&lt;span style=&quot;color: #202020;&quot;&gt;database_id&lt;/span&gt; AND fs.&lt;span style=&quot;color: #FF00FF;&quot;&gt;FILE_ID&lt;/span&gt; = f.&lt;span style=&quot;color: #FF00FF;&quot;&gt;FILE_ID&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;INNER&lt;/span&gt; JOIN sys.&lt;span style=&quot;color: #202020;&quot;&gt;databases&lt;/span&gt; d &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; d.&lt;span style=&quot;color: #202020;&quot;&gt;database_id&lt;/span&gt; = fs.&lt;span style=&quot;color: #202020;&quot;&gt;database_id&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb32485&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;p&gt;
We can take this even further by using the statements we captured in order to insert data from SQL Server over a time period.&lt;br /&gt;
&lt;br /&gt;
We can already see the value in the information we&amp;#8217;ve captured out of Profiler and the Activity Monitor for our own use in reporting and overtime analysis of SQL Server with a rapid development feel to the process.   
&lt;/p&gt;&lt;a href=&quot;http://api.tweetmeme.com/share?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/capture-data-from-activity-monitor-2008&quot;&gt;&lt;img src=&quot;http://api.tweetmeme.com/imagebutton.gif?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/capture-data-from-activity-monitor-2008&amp;source=LessThanDot&quot; height=&quot;61&quot; width=&quot;51&quot; /&gt;&lt;/a&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/capture-data-from-activity-monitor-2008&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[				<h3>by <a href="http://blogs.lessthandot.com/index.php/All/?disp=authdir&amp;author=68"> onpnt </a></h3>
				<p>The enhancements to the Activity Monitor were an extremely useful addition to SQL Server 2008.  We can gather a moving view of statistics and the health of the instances we connect to.  In the real world however, the activity monitor loses value when you need to connect to dozens of SQL Servers in order to obtain that moving feel of each server you manage.  There is also the nuisance of being required to open a separate Activity Monitor instance for each server we connect to from SSMS.  There is a way we can eliminate those aspects though.<br />
<br />
One method we can use is SQL Server Profiler to capture and reuse valuable information gathered from the new Activity Monitor.  Profiler allows us to see exactly what is populating the tables behind the engine.  
</p>
<h2>Let&#8217;s take a look</h2>
<p>
Open SSMS 2008 and connect to a development instance.  Once open, right click the server name in object explorer and select Activity Monitor from the list.<br />
<br />
Now open SQL Server Profiler from the performance tools section located in the all programs section of SQL Server 2008.  <br />
<br />
Create a new trace and either save to a table or a file for the output.  Saving to a table for this type of task is useful so we can query the results later and will not be a heavy performance issue as the trace will not be running long.  Next, click the Event Selection so we can filter out most of the batch statements that we don&#8217;t want to see.  I put my filter to only show ApplicationName value of &#8220;Microsoft SQL Server Management Studio&#8221; and also LoginName of myself.  <br />
<div class="image_block"><img src="http://blogs.lessthandot.com/media/blogs/DataMgmt/am2.png" alt="" title="" width="480" height="315" /></div>
</p>

<p><br /><br />
Once that is completely setup, hit Run to start the trace.</p>

<p>
You should start to see all of the main statements that are driving the Activity Monitor start coming into profiler.  <br />
<br />
We can see that the queries are based on several DMV/DMF and are being inserted into temporary tables for the Activity Monitor to show.  At this point the query isn&#8217;t helpful sense the data is being inserted into temporary tables we cannot control.  The temporary tables are already created by the monitor so recreating them manually ourselves would also cause a problem.  We can use these queries though to manage our own results by simple changes in them and executing them in tasks we create ourselves.<br />
<br />
Let&#8217;s show this by grabbing the query that populates the file information in the &#8220;Data File I/O&#8221; section of the Activity Monitor.  The query that does this should look like this in the beginning of the batch<br />
<div class="image_block"><img src="http://blogs.lessthandot.com/media/blogs/DataMgmt/am.gif" alt="" title="" width="555" height="177" /></div>
</p>
<p>
<p>To use this query for ourselves, all we need to do is comment out the INSERT INTO #am_dbfilestats and we can either convert the INSERT INTO to a SELECT INTO statement with our own temporary table or simply use the query as a direct SELECT </p>

<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb9505" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">SELECT</span> <br />&nbsp; &nbsp; d.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">DATABASE</span><span style="color: #808080;">&#93;</span>, <br />&nbsp; &nbsp; f.<span style="color: #202020;">physical_name</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">FILE</span><span style="color: #808080;">&#93;</span>, <br />&nbsp; &nbsp; <span style="color: #808080;">&#40;</span>fs.<span style="color: #202020;">num_of_bytes_read</span> / <span style="color: #000;">1024.0</span> / <span style="color: #000;">1024.0</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">&#91;</span>Total MB <span style="color: #0000FF;">READ</span><span style="color: #808080;">&#93;</span>, <br />&nbsp; &nbsp; <span style="color: #808080;">&#40;</span>fs.<span style="color: #202020;">num_of_bytes_written</span> / <span style="color: #000;">1024.0</span> / <span style="color: #000;">1024.0</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Total MB Written<span style="color: #808080;">&#93;</span>, <br />&nbsp; &nbsp; <span style="color: #808080;">&#40;</span>fs.<span style="color: #202020;">num_of_reads</span> + fs.<span style="color: #202020;">num_of_writes</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Total I/O <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#93;</span>, <br />&nbsp; &nbsp; fs.<span style="color: #202020;">io_stall</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Total I/O Wait <span style="color: #0000FF;">TIME</span> <span style="color: #808080;">&#40;</span>ms<span style="color: #808080;">&#41;</span><span style="color: #808080;">&#93;</span>, <br />&nbsp; &nbsp; fs.<span style="color: #202020;">size_on_disk_bytes</span> / <span style="color: #000;">1024</span> / <span style="color: #000;">1024</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">SIZE</span> <span style="color: #808080;">&#40;</span>MB<span style="color: #808080;">&#41;</span><span style="color: #808080;">&#93;</span><br /><span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">dm_io_virtual_file_stats</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">DEFAULT</span>, <span style="color: #0000FF;">DEFAULT</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> fs<br /><span style="color: #0000FF;">INNER</span> JOIN sys.<span style="color: #202020;">master_files</span> f <span style="color: #0000FF;">ON</span> fs.<span style="color: #202020;">database_id</span> = f.<span style="color: #202020;">database_id</span> AND fs.<span style="color: #FF00FF;">FILE_ID</span> = f.<span style="color: #FF00FF;">FILE_ID</span><br /><span style="color: #0000FF;">INNER</span> JOIN sys.<span style="color: #202020;">databases</span> d <span style="color: #0000FF;">ON</span> d.<span style="color: #202020;">database_id</span> = fs.<span style="color: #202020;">database_id</span>;<br /><br /></div><div id="cb21156" style="display: none; color: red;"></div></div></div>
</p>
<p></p><p>
We can take this even further by using the statements we captured in order to insert data from SQL Server over a time period.<br />
<br />
We can already see the value in the information we&#8217;ve captured out of Profiler and the Activity Monitor for our own use in reporting and overtime analysis of SQL Server with a rapid development feel to the process.   
</p><a href="http://api.tweetmeme.com/share?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/capture-data-from-activity-monitor-2008"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/capture-data-from-activity-monitor-2008&amp;source=LessThanDot" height="61" width="51" /></a><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBAdmin/capture-data-from-activity-monitor-2008">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/capture-data-from-activity-monitor-2008#comments</comments>
		</item>
				<item>
			<title>How to return all the skipped identity values from a table in SQL Server</title>
			<link>http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/how-to-return-all-the-skipped-identity-v</link>
			<pubDate>Mon, 18 Jan 2010 14:41:34 +0000</pubDate>			<dc:creator>SQLDenis</dc:creator>
			<category domain="main">Database Programming</category>
<category domain="alt">Microsoft SQL Server</category>			<guid isPermaLink="false">728@http://blogs.lessthandot.com/</guid>
						<description>&lt;h3&gt;by &lt;a href=&quot;http://blogs.lessthandot.com/index.php/All/?disp=authdir&amp;amp;author=4&quot;&gt; SQLDenis &lt;/a&gt;&lt;/h3&gt;&lt;p&gt;Every now and then someone will ask how to return a list of all the identity values in a table that have been skipped.  You will probably have a table with an identity column, the 'problem' with identity columns is that if an insert is rolled back or fails in any way then the identity value is not reused...you end up with gaps. Identifying gaps is pretty easy to do if you have a table of numbers in your database.&lt;/p&gt;

&lt;p&gt;If you don't have a table of numbers, here is some code that will create a table with numbers between 1` and 2048&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;cb19133&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;TABLE&lt;/span&gt; Numbers &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;number &lt;span style=&quot;color: #0000FF;&quot;&gt;INT&lt;/span&gt; not null &lt;span style=&quot;color: #0000FF;&quot;&gt;PRIMARY&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;KEY&lt;/span&gt; &lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;go&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; Numbers &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; number + &lt;span style=&quot;color: #000;&quot;&gt;1&lt;/span&gt; &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; master..&lt;span style=&quot;color: #202020;&quot;&gt;spt_values&lt;/span&gt; s&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; s.&lt;span style=&quot;color: #202020;&quot;&gt;type&lt;/span&gt;=&lt;span style=&quot;color: #FF0000;&quot;&gt;'P'&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb12566&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now that we have our numbers table we can proceed with creating another table which we will populate with some numbers&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;cb17337&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;TABLE&lt;/span&gt; #bla&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;id &lt;span style=&quot;color: #0000FF;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;INSERT&lt;/span&gt; #bla &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: #000;&quot;&gt;1&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;INSERT&lt;/span&gt; #bla &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: #000;&quot;&gt;2&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;INSERT&lt;/span&gt; #bla &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: #000;&quot;&gt;4&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;INSERT&lt;/span&gt; #bla &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: #000;&quot;&gt;5&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;INSERT&lt;/span&gt; #bla &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: #000;&quot;&gt;9&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;INSERT&lt;/span&gt; #bla &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: #000;&quot;&gt;12&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;cb99350&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;



&lt;p&gt;Here is the code that will return the gaps (the values 3,6,7,8,10,11)  from the temp table&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;cb18487&quot; style=&quot;display: block; color: rgb(0, 0, 0);&quot;&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;SELECT&lt;/span&gt; number &lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; Numbers n&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;LEFT&lt;/span&gt; join #bla b &lt;span style=&quot;color: #0000FF;&quot;&gt;ON&lt;/span&gt; n.&lt;span style=&quot;color: #202020;&quot;&gt;number&lt;/span&gt; = b.&lt;span style=&quot;color: #202020;&quot;&gt;id&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: #0000FF;&quot;&gt;WHERE&lt;/span&gt; n.&lt;span style=&quot;color: #202020;&quot;&gt;number&lt;/span&gt; &amp;lt; &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; &lt;span style=&quot;color: #FF00FF;&quot;&gt;MAX&lt;/span&gt;&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#40;&lt;/span&gt;id&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;FROM&lt;/span&gt; #bla&lt;span style=&quot;color: #808080;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;br /&gt;and &amp;nbsp;b.&lt;span style=&quot;color: #202020;&quot;&gt;id&lt;/span&gt; &lt;span style=&quot;color: #0000FF;&quot;&gt;IS&lt;/span&gt; null&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div id=&quot;cb91515&quot; style=&quot;display: none; color: red;&quot;&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As you can see it is a simple left join, we also check for the max value otherwise you would get everything back that is greater than the max value in the #bla table.&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;
*** &lt;strong&gt;Remember, if you have a SQL related question, try our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=17&quot;&gt;Microsoft SQL Server Programming&lt;/a&gt; forum or our &lt;a href=&quot;http://forum.lessthandot.com/viewforum.php?f=22&quot;&gt;Microsoft SQL Server Admin&lt;/a&gt; forum&lt;/strong&gt;&lt;ins&gt;&lt;/ins&gt;&lt;/p&gt;&lt;a href=&quot;http://api.tweetmeme.com/share?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/how-to-return-all-the-skipped-identity-v&quot;&gt;&lt;img src=&quot;http://api.tweetmeme.com/imagebutton.gif?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/how-to-return-all-the-skipped-identity-v&amp;source=LessThanDot&quot; height=&quot;61&quot; width=&quot;51&quot; /&gt;&lt;/a&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/how-to-return-all-the-skipped-identity-v&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[				<h3>by <a href="http://blogs.lessthandot.com/index.php/All/?disp=authdir&amp;author=4"> SQLDenis </a></h3>
				<p>Every now and then someone will ask how to return a list of all the identity values in a table that have been skipped.  You will probably have a table with an identity column, the 'problem' with identity columns is that if an insert is rolled back or fails in any way then the identity value is not reused...you end up with gaps. Identifying gaps is pretty easy to do if you have a table of numbers in your database.</p>

<p>If you don't have a table of numbers, here is some code that will create a table with numbers between 1` and 2048</p>

<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb98555" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> Numbers <span style="color: #808080;">&#40;</span>number <span style="color: #0000FF;">INT</span> not null <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #808080;">&#41;</span><br />go<br /><span style="color: #0000FF;">INSERT</span> Numbers <br /><span style="color: #0000FF;">SELECT</span> number + <span style="color: #000;">1</span> <br /><span style="color: #0000FF;">FROM</span> master..<span style="color: #202020;">spt_values</span> s<br /><span style="color: #0000FF;">WHERE</span> s.<span style="color: #202020;">type</span>=<span style="color: #FF0000;">'P'</span><br /><br /></div><div id="cb31356" style="display: none; color: red;"></div></div></div>

<p>Now that we have our numbers table we can proceed with creating another table which we will populate with some numbers</p>

<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb28416" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #bla<span style="color: #808080;">&#40;</span>id <span style="color: #0000FF;">INT</span><span style="color: #808080;">&#41;</span><br />&nbsp;<br /><span style="color: #0000FF;">INSERT</span> #bla <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span><br /><span style="color: #0000FF;">INSERT</span> #bla <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><br /><span style="color: #0000FF;">INSERT</span> #bla <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #000;">4</span><span style="color: #808080;">&#41;</span><br /><span style="color: #0000FF;">INSERT</span> #bla <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #000;">5</span><span style="color: #808080;">&#41;</span><br /><span style="color: #0000FF;">INSERT</span> #bla <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #000;">9</span><span style="color: #808080;">&#41;</span><br /><span style="color: #0000FF;">INSERT</span> #bla <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #000;">12</span><span style="color: #808080;">&#41;</span><br /><br /></div><div id="cb37961" style="display: none; color: red;"></div></div></div>



<p>Here is the code that will return the gaps (the values 3,6,7,8,10,11)  from the temp table</p>

<div class="codebox"><div class="codeheader">Code: <span>tsql</span></div><div class="codeholder"><div class="tsql" id="cb65417" style="display: block; color: rgb(0, 0, 0);"><span style="color: #0000FF;">SELECT</span> number <br /><span style="color: #0000FF;">FROM</span> Numbers n<br /><span style="color: #0000FF;">LEFT</span> join #bla b <span style="color: #0000FF;">ON</span> n.<span style="color: #202020;">number</span> = b.<span style="color: #202020;">id</span><br /><span style="color: #0000FF;">WHERE</span> n.<span style="color: #202020;">number</span> &lt; <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#40;</span>id<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">FROM</span> #bla<span style="color: #808080;">&#41;</span><br />and &nbsp;b.<span style="color: #202020;">id</span> <span style="color: #0000FF;">IS</span> null<br /><br /></div><div id="cb48673" style="display: none; color: red;"></div></div></div>

<p>As you can see it is a simple left join, we also check for the max value otherwise you would get everything back that is greater than the max value in the #bla table.<br />
<br /><br /><br />
*** <strong>Remember, if you have a SQL related question, try our <a href="http://forum.lessthandot.com/viewforum.php?f=17">Microsoft SQL Server Programming</a> forum or our <a href="http://forum.lessthandot.com/viewforum.php?f=22">Microsoft SQL Server Admin</a> forum</strong><ins></ins></p><a href="http://api.tweetmeme.com/share?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/how-to-return-all-the-skipped-identity-v"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/how-to-return-all-the-skipped-identity-v&amp;source=LessThanDot" height="61" width="51" /></a><div class="item_footer"><p><small><a href="http://blogs.lessthandot.com/index.php/DataMgmt/DBProgramming/how-to-return-all-the-skipped-identity-v">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/how-to-return-all-the-skipped-identity-v#comments</comments>
		</item>
			</channel>
</rss>
