<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>SQL Server Journey with SQL Authority &#187; SQL Stored Procedure</title>
	<atom:link href="http://blog.sqlauthority.com/category/sql-stored-procedure/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com</link>
	<description>Personal Notes of Pinal Dave</description>
	<lastBuildDate>Wed, 08 Feb 2012 17:24:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.sqlauthority.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/08e35387c05b61340e885b1763a69d9f?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>SQL Server Journey with SQL Authority &#187; SQL Stored Procedure</title>
		<link>http://blog.sqlauthority.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.sqlauthority.com/osd.xml" title="SQL Server Journey with SQL Authority" />
	<atom:link rel='hub' href='http://blog.sqlauthority.com/?pushpress=hub'/>
		<item>
		<title>SQL SERVER &#8211; Bad Practice of Using Keywords as an Object Name &#8211; Avoid Using Keywords as an Object</title>
		<link>http://blog.sqlauthority.com/2011/12/09/sql-server-bad-practice-of-using-keywords-as-an-object-name-avoid-using-keywords-as-an-object/</link>
		<comments>http://blog.sqlauthority.com/2011/12/09/sql-server-bad-practice-of-using-keywords-as-an-object-name-avoid-using-keywords-as-an-object/#comments</comments>
		<pubDate>Fri, 09 Dec 2011 01:30:41 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[CodeProject]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=16261</guid>
		<description><![CDATA[Madhivanan is SQL Server MVP and very talented SQL expert. Here is one of the nugget he shared on Just Learned. He shared a tip where there were two interesting point to learn. Do not use keywords as an object name [read DHall's excellent comment below] He has given excellent example how GO can be [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16261&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Madhivanan is SQL Server MVP and very talented SQL expert. Here is one of the nugget he shared on <a href="http://beyondrelational.com/justlearned/" target="_blank">Just Learned</a>.</p>
<p>He shared a tip where there were two interesting point to learn.</p>
<ol>
<li>Do not use keywords as an object name</li>
<li><strong><a href="http://blog.sqlauthority.com/2011/12/09/sql-server-bad-practice-of-using-keywords-as-an-object-name-avoid-using-keywords-as-an-object/#comment-213797" target="_blank">[read DHall's excellent comment below]</a></strong></li>
</ol>
<p>He has given excellent example how GO can be executed as stored procedure. Here is the extension of the tip. Create a small table and now just hit EXEC GO; and you will notice that there is row in the table.</p>
<p>Create Stored Procedure<br />
<code style="font-size:12px;"><span style="color:blue;">CREATE PROCEDURE </span><span style="color:black;">GO<br />
</span><span style="color:blue;">AS<br />
SELECT </span><span style="color:black;">1 </span><span style="color:blue;">AS </span><span style="color:black;">NUMBER<br />
</span></code><br />
Create Table<br />
<code style="font-size:12px;"><span style="color:blue;">CREATE TABLE </span><span style="color:black;">T1 </span><span style="color:gray;">(</span><span style="color:black;">ID </span><span style="color:blue;">INT</span><span style="color:gray;">)</span></code></p>
<p>Now execute following code<br />
<code style="font-size:12px;"><span style="color:blue;">INSERT INTO </span><span style="color:black;">T1</span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">EXEC </span><span style="color:black;">GO</span><span style="color:gray;">;</span></code></p>
<p>Now when selecting from table it will give us following result:<br />
<code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">T1</span></code></p>
<p>Now see following resultset:</p>
<p><img class="alignnone" src="http://www.pinaldave.com/bimg/execgo.jpg" alt="" width="236" height="124" /></p>
<p>So without inserting any data we inserted the data, well indeed good puzzle but very bad practical practice. Every body should be aware of this gotcha and avoid it. Thanks Madhivanan for teaching this interesting learning.</p>
<p>Republishing here with authorization of <a href="http://beyondrelational.com/justlearned/" target="_blank">Just Learned</a>.</p>
<p>Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/codeproject-2/'>CodeProject</a>, <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/sql-stored-procedure/'>SQL Stored Procedure</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sqlserver/'>SQLServer</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/16261/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/16261/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/16261/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/16261/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/16261/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/16261/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/16261/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/16261/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/16261/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/16261/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/16261/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/16261/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/16261/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/16261/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16261&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/12/09/sql-server-bad-practice-of-using-keywords-as-an-object-name-avoid-using-keywords-as-an-object/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/execgo.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Powershell &#8211; Get a List of Fixed Hard Drive and Free Space on Server</title>
		<link>http://blog.sqlauthority.com/2011/11/27/sql-server-powershell-get-a-list-of-fixed-hard-drive-and-free-space-on-server/</link>
		<comments>http://blog.sqlauthority.com/2011/11/27/sql-server-powershell-get-a-list-of-fixed-hard-drive-and-free-space-on-server/#comments</comments>
		<pubDate>Sun, 27 Nov 2011 01:30:46 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Powershell]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=16062</guid>
		<description><![CDATA[Earlier I have written this article SQL SERVER – Get a List of Fixed Hard Drive and Free Space on Server. I recently received excellent comment by MVP Ravikanth. He demonstrated that how the same can be done using Powershell. It is very sweet and quick solution. Here is the powershell script. Run the same [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16062&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Earlier I have written this article <strong><a href="blog.sqlauthority.com/2009/08/05/sql-server-get-a-list-of-fixed-hard-drive-and-free-space-on-server/" target="_blank">SQL SERVER – Get a List of Fixed Hard Drive and Free Space on Server</a></strong>. I recently received excellent comment by MVP <strong><a href="http://blog.sqlauthority.com/2009/08/05/sql-server-get-a-list-of-fixed-hard-drive-and-free-space-on-server/#comment-204215" target="_blank">Ravikanth</a></strong>. He demonstrated that how the same can be done using Powershell. It is very sweet and quick solution.</p>
<p>Here is the powershell script. Run the same in your powershell windows.</p>
<pre>Get-WmiObject -Class Win32_LogicalDisk | Select -Property DeviceID, @{Name=’FreeSpaceMB’;Expression={$_.FreeSpace/1MB} } | Format-Table -AutoSize</pre>
<p>Well, I ran this script in my powershell window, it gave me following result &#8211; very accurately and easily.</p>
<p>Get-WmiObject -Class Win32_LogicalDisk | Select -Property DeviceID, @{Name=’FreeSpaceMB’;Expression={$_.FreeSpace/1MB} } | Format-Table -AutoSize</p>
<p><img class="alignnone" src="http://www.pinaldave.com/bimg/pssize.jpg" alt="" width="515" height="234" /></p>
<p>Thanks <strong><a href="http://blog.sqlauthority.com/2009/08/05/sql-server-get-a-list-of-fixed-hard-drive-and-free-space-on-server/#comment-204215" target="_blank">Ravikanth</a></strong> one more time for excellent tip.</p>
<p>Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/sql-stored-procedure/'>SQL Stored Procedure</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a> Tagged: <a href='http://blog.sqlauthority.com/tag/powershell/'>Powershell</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/16062/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/16062/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/16062/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/16062/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/16062/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/16062/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/16062/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/16062/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/16062/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/16062/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/16062/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/16062/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/16062/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/16062/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16062&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/27/sql-server-powershell-get-a-list-of-fixed-hard-drive-and-free-space-on-server/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/pssize.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Get Directory Structure using Extended Stored Procedure xp_dirtree</title>
		<link>http://blog.sqlauthority.com/2011/11/26/sql-server-get-directory-structure-using-extended-stored-procedure-xp_dirtree/</link>
		<comments>http://blog.sqlauthority.com/2011/11/26/sql-server-get-directory-structure-using-extended-stored-procedure-xp_dirtree/#comments</comments>
		<pubDate>Sat, 26 Nov 2011 01:30:29 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=16055</guid>
		<description><![CDATA[Many years ago I wrote article SQL SERVER – Get a List of Fixed Hard Drive and Free Space on Server where I demonstrated using undocumented Stored Procedure to find the drive letter in local system and available free space. I received question in email from reader asking if there any way he can list directory [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16055&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Many years ago I wrote article <strong><a href="blog.sqlauthority.com/2009/08/05/sql-server-get-a-list-of-fixed-hard-drive-and-free-space-on-server/" target="_blank">SQL SERVER – Get a List of Fixed Hard Drive and Free Space on Server</a></strong> where I demonstrated using undocumented Stored Procedure to find the drive letter in local system and available free space. I received question in email from reader asking if there any way he can list directory structure within the T-SQL. When I inquired more he suggested that he needs this because he wanted set up backup of the data in certain structure.</p>
<p style="text-align:justify;">Well, there is one undocumented stored procedure exists which can do the same. However, please be vary to use any undocumented procedures.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:darkred;">xp_dirtree </span><span style="color:red;">'C:\Windows'<br />
</span></code></p>
<p style="text-align:justify;">Execution of the above stored procedure will give following result. If you prefer you can insert the data in the temptable and use the same for further use.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/xpdirtree.jpg" alt="" width="420" height="263" /></p>
<p style="text-align:justify;">Here is the quick script which will insert the data into the temptable and retrieve from the same.</p>
<p><code style="font-size:12px;"><span style="color:blue;">CREATE TABLE </span><span style="color:#434343;">#TempTable </span><span style="color:gray;">(</span><span style="color:black;">Subdirectory </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">512</span><span style="color:gray;">), </span><span style="color:black;">Depth </span><span style="color:blue;">INT</span><span style="color:gray;">);<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">#TempTable </span><span style="color:gray;">(</span><span style="color:black;">Subdirectory</span><span style="color:gray;">, </span><span style="color:black;">Depth</span><span style="color:gray;">)<br />
</span><span style="color:blue;">EXEC </span><span style="color:darkred;">xp_dirtree </span><span style="color:red;">'C:\Windows'<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">Subdirectory</span><span style="color:gray;">, </span><span style="color:black;">Depth<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">#TempTable</span><span style="color:gray;">;<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:#434343;">#TempTable</span><span style="color:gray;">;</span></code></p>
<p>Reference:<strong> </strong><strong>Pinal Dave (</strong>http://blog.SQLAuthority.com<strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/sql-stored-procedure/'>SQL Stored Procedure</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sqlserver/'>SQLServer</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/16055/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/16055/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/16055/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/16055/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/16055/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/16055/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/16055/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/16055/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/16055/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/16055/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/16055/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/16055/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/16055/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/16055/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=16055&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/11/26/sql-server-get-directory-structure-using-extended-stored-procedure-xp_dirtree/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/xpdirtree.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Question to You &#8211; When to use Function and When to use Stored Procedure</title>
		<link>http://blog.sqlauthority.com/2011/06/04/sql-server-question-to-you-when-to-use-function-and-when-to-use-stored-procedure/</link>
		<comments>http://blog.sqlauthority.com/2011/06/04/sql-server-question-to-you-when-to-use-function-and-when-to-use-stored-procedure/#comments</comments>
		<pubDate>Sat, 04 Jun 2011 01:30:45 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[Readers Question]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=13105</guid>
		<description><![CDATA[This week has been very interesting week. I have asked few questions to users and have received remarkable participation on the subject. Q1) SQL SERVER – Puzzle – SELECT * vs SELECT COUNT(*) Q2) SQL SERVER – Puzzle – Statistics are not Updated but are Created Once Keeping the same spirit up, I am asking the third [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=13105&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">This week has been very interesting week. I have asked few questions to users and have received remarkable participation on the subject.</p>
<p style="text-align:justify;"><strong>Q1) <a title="Permanent Link to SQL SERVER – Puzzle – SELECT * vs SELECT COUNT(*)" href="http://blog.sqlauthority.com/2011/05/30/sql-server-puzzle-select-vs-select-count/">SQL SERVER – Puzzle – SELECT * vs SELECT COUNT(*)</a></strong></p>
<p style="text-align:justify;"><strong>Q2) <a title="Permanent Link to SQL SERVER – Puzzle – Statistics are not Updated but are Created Once" href="http://blog.sqlauthority.com/2011/06/02/sql-server-puzzle-statistics-are-not-updated-but-are-created-once/">SQL SERVER – Puzzle – Statistics are not Updated but are Created Once</a></strong></p>
<p style="text-align:justify;">Keeping the same spirit up, I am asking the third question over here.</p>
<p style="text-align:justify;">Q3) When to use User Defined Function and when to use Stored Procedure in your development?</p>
<p style="text-align:justify;">Personally, I believe that they are both different things ‑ they cannot be compared. I can say, it will be like comparing apples and oranges. Each has its own unique use. <strong><em>However</em></strong>, they can be used interchangeably at many times and in real life (i.e., production environment). I have personally seen both of these being used interchangeably many times. This is the precise reason for asking this question.</p>
<p style="text-align:justify;"><strong><em>When do you use Function and when do you use Stored Procedure? What are Pros and Cons of each of them when used instead of each other?</em></strong></p>
<p style="text-align:justify;">If you are going to answer that &#8216;To avoid repeating code, you use Function’ ‑ please think harder! Stored procedure can do the same. In SQL Server Denali, even the stored procedure can return the result just like Function in SELECT statement; so if you are going to answer with &#8216;Function can be used in SELECT, whereas Stored Procedure cannot be used’ ‑ again think harder! (<a href="http://blog.sqlauthority.com/2011/04/29/sql-server-denali-executing-stored-procedure-with-result-sets-2/" target="_blank">link</a>).</p>
<p style="text-align:justify;">Now, what do you say? I will post the answers of all the three questions with due credit next week.</p>
<p style="text-align:justify;">Reference:<strong> Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/readers-question/'>Readers Question</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/sql-puzzle/'>SQL Puzzle</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/sql-stored-procedure/'>SQL Stored Procedure</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sqlserver/'>SQLServer</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/13105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/13105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/13105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/13105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/13105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/13105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/13105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/13105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/13105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/13105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/13105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/13105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/13105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/13105/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=13105&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/06/04/sql-server-question-to-you-when-to-use-function-and-when-to-use-stored-procedure/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Denali &#8211; Executing Stored Procedure with Result Sets</title>
		<link>http://blog.sqlauthority.com/2011/04/28/sql-server-denali-executing-stored-procedure-with-result-sets/</link>
		<comments>http://blog.sqlauthority.com/2011/04/28/sql-server-denali-executing-stored-procedure-with-result-sets/#comments</comments>
		<pubDate>Thu, 28 Apr 2011 01:30:51 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[PostADay]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=12723</guid>
		<description><![CDATA[Here is a normal conversation I heard when I saw that the function (UDF) was used instead of the procedure (SP). Q: Why are you using User Defined Function instead of Stored Procedure? A: I cannot SELECT from SP, but I can from UDF. SQL Server’s next version &#8216;Denali&#8217; is coming up with a very [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=12723&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Here is a normal conversation I heard when I saw that the function (UDF) was used instead of the procedure (SP).</p>
<p style="text-align:justify;"><strong><em>Q:</em></strong><em> Why are you using User Defined Function instead of Stored Procedure?</em></p>
<p style="text-align:justify;"><strong><em>A:</em></strong><em> I cannot SELECT from SP, but I can from UDF.</em></p>
<p style="text-align:justify;">SQL Server’s next version &#8216;Denali&#8217; is coming up with a very interesting feature called WITH RESULT SET.</p>
<p style="text-align:justify;">Using this feature, you can run the stored procedure and rename the columns used in it. The usual procedure of creating TempTable, executing the stored procedure and inserting the data into the TempTable may be time-consuming, that is why Denali introduced the WITH RESULT SET feature. This feature enables you to select the data without using the TempTable. You can rename the columns during the run time as well.</p>
<p style="text-align:justify;">Here is the quick script:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks2008R2<br />
GO<br />
</span><span style="color:blue;">CREATE PROCEDURE </span><span style="color:black;">mySP </span><span style="color:gray;">(</span><span style="color:#434343;">@ShiftID </span><span style="color:blue;">INT</span><span style="color:gray;">)<br />
</span><span style="color:blue;">AS<br />
SELECT </span><span style="color:black;">[ShiftID]<br />
</span><span style="color:gray;">,</span><span style="color:black;">[Name]<br />
</span><span style="color:gray;">,</span><span style="color:black;">[StartTime]<br />
</span><span style="color:gray;">,</span><span style="color:black;">[EndTime]<br />
</span><span style="color:gray;">,</span><span style="color:black;">[ModifiedDate]<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">[HumanResources].[Shift]<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">[ShiftID] </span><span style="color:blue;">= </span><span style="color:#434343;">@ShiftID<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Executing Stored Procedure<br />
</span><span style="color:blue;">EXEC </span><span style="color:black;">mySP </span><span style="color:#434343;">@ShiftID </span><span style="color:blue;">= </span><span style="color:black;">2<br />
</span><span style="color:blue;">WITH </span><span style="color:black;">RESULT SETS<br />
</span><span style="color:gray;">(<br />
(<br />
</span><span style="color:black;">[ShiftID] TINYINT<br />
</span><span style="color:gray;">,</span><span style="color:black;">[Name] </span><span style="color:blue;">NVARCHAR</span><span style="color:gray;">(</span><span style="color:black;">50</span><span style="color:gray;">)<br />
,</span><span style="color:black;">[StartTime] </span><span style="color:blue;">DATETIME<br />
</span><span style="color:gray;">,</span><span style="color:black;">[EndTime] </span><span style="color:blue;">DATETIME<br />
</span><span style="color:gray;">,</span><span style="color:black;">[UpdateDate] </span><span style="color:blue;">DATETIME </span><span style="color:green;">-- Notice Name Change<br />
</span><span style="color:gray;">)<br />
);</span></code></p>
<p style="text-align:justify;">Reference:<strong> Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/postaday/'>PostADay</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/sql-stored-procedure/'>SQL Stored Procedure</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/12723/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/12723/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/12723/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/12723/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/12723/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/12723/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/12723/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/12723/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/12723/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/12723/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/12723/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/12723/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/12723/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/12723/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=12723&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2011/04/28/sql-server-denali-executing-stored-procedure-with-result-sets/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; System Stored Procedure sys.sp_tables</title>
		<link>http://blog.sqlauthority.com/2010/10/17/sql-server-system-stored-procedure-sys-sp_tables/</link>
		<comments>http://blog.sqlauthority.com/2010/10/17/sql-server-system-stored-procedure-sys-sp_tables/#comments</comments>
		<pubDate>Sun, 17 Oct 2010 01:30:49 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL System Table]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=10569</guid>
		<description><![CDATA[I have seen people running the following script quite often, to know the list of the tables from the database: SELECT * FROM sys.tables GO The script above provides various information from create date to file stream, and many other important information. If you need all those information, that script is the one for you. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10569&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">I have seen people running the following script quite often, to know the list of the tables from the database:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.tables<br />
GO</span></code></p>
<p style="text-align:justify;">The script above provides various information from create date to file stream, and many other important information. If you need all those information, that script is the one for you. However, if you do not need all those information, I suggest that you run the following script:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">EXEC </span><span style="color:black;">sys.</span><span style="color:darkred;">sp_tables<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;">The script above will give all the tables in the table with schema name and qualifiers. Additionally, this will return all the system catalog views together with other views. This Stored Procedure returns all the tables first in the result set, followed by views.</p>
<p style="text-align:justify;">Even though Stored Procedure returns more numbers of rows, it still performs better than the sys.table query.</p>
<p style="text-align:justify;">Let us verify it with two different methods for database AdventureWorks:</p>
<p style="text-align:justify;"><strong>1) SET STATISTICS IO ON</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SET STATISTICS </span><span style="color:black;">IO </span><span style="color:blue;">ON<br />
SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.tables<br />
GO<br />
</span><span style="color:blue;">EXEC </span><span style="color:black;">sys.</span><span style="color:darkred;">sp_tables<br />
</span><span style="color:black;">GO<br />
</span></code></p>
<p style="text-align:justify;">(81 row(s) affected) (This is for sys.tables)<br />
Table &#8216;syspalvalues&#8217;. Scan count 0, logical reads 162, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.<br />
Table &#8216;syssingleobjrefs&#8217;. Scan count 0, logical reads 324, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.<br />
Table &#8216;sysidxstats&#8217;. Scan count 81, logical reads 168, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.<br />
Table &#8216;syspalnames&#8217;. Scan count 0, logical reads 162, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.<br />
Table &#8216;sysschobjs&#8217;. Scan count 1, logical reads 16, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.</p>
<p style="text-align:justify;">(1 row(s) affected)</p>
<p style="text-align:justify;">(456 row(s) affected) (This is for sys.sp_tables)<br />
Table &#8216;sysobjrdb&#8217;. Scan count 1, logical reads 29, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.<br />
Table &#8216;sysschobjs&#8217;. Scan count 1, logical reads 16, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.</p>
<p style="text-align:justify;">(1 row(s) affected)</p>
<p style="text-align:justify;"><strong>2) Execution Plan</strong></p>
<p style="text-align:justify;"><img src="http://www.pinaldave.com/bimg/sp_tables.jpg" alt="" /></p>
<p style="text-align:justify;">You can see that the execution plan for <strong>sys.table</strong> has much higher cost of query batch.</p>
<p style="text-align:justify;">Well, if you only need to know the name of the tables, I suggest that you start using SP_TABLES; at least it takes less typing to do.</p>
<p style="text-align:justify;">Reference:<strong> Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/sql-stored-procedure/'>SQL Stored Procedure</a>, <a href='http://blog.sqlauthority.com/category/sql-system-table/'>SQL System Table</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/10569/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/10569/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/10569/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/10569/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/10569/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/10569/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/10569/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/10569/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/10569/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/10569/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/10569/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/10569/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/10569/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/10569/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10569&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/10/17/sql-server-system-stored-procedure-sys-sp_tables/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/sp_tables.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Encrypted Stored Procedure and Activity Monitor</title>
		<link>http://blog.sqlauthority.com/2010/10/10/sql-server-encrypted-stored-procedure-and-activity-monitor/</link>
		<comments>http://blog.sqlauthority.com/2010/10/10/sql-server-encrypted-stored-procedure-and-activity-monitor/#comments</comments>
		<pubDate>Sun, 10 Oct 2010 01:30:15 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=10498</guid>
		<description><![CDATA[I recently had received question if any stored procedure is encrypted can we see its definition in Activity Monitor. No. Let us do quick test. Let us create following Stored Procedure and then launch Activity Monitor and check the text. USE AdventureWorks GO -- Create Encyrpted SP CREATE PROCEDURE uspEnc WITH ENCRYPTION AS SELECT * [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10498&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I recently had received question if any stored procedure is encrypted can we see its definition in Activity Monitor.</p>
<p>No.</p>
<p>Let us do quick test. Let us create following Stored Procedure and then launch Activity Monitor and check the text.</p>
<p><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:green;">-- Create Encyrpted SP<br />
</span><span style="color:blue;">CREATE PROCEDURE </span><span style="color:black;">uspEnc<br />
</span><span style="color:blue;">WITH ENCRYPTION<br />
AS<br />
SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail<br />
</span><span style="color:blue;">UNION<br />
SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Sales.SalesOrderDetail<br />
GO<br />
</span><span style="color:green;">-- Execute SP<br />
</span><span style="color:blue;">EXEC </span><span style="color:black;">uspEnc<br />
GO<br />
</span></code></p>
<p>You will be not able to see the text of the encrypted Stored Procedure of the SQL Server.</p>
<p><img class="alignnone" src="http://www.pinaldave.com/bimg/encryptedSP.jpg" alt="" width="500" height="461" /></p>
<p>What do you think? Is there any other way we can approach this?</p>
<p>Reference:<strong> Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
<div id="_mcePaste" class="mcePaste" style="position:absolute;left:-10000px;top:515px;width:1px;height:1px;overflow:hidden;">Happy Birthday to you!</div>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/sql-stored-procedure/'>SQL Stored Procedure</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/10498/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/10498/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/10498/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/10498/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/10498/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/10498/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/10498/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/10498/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/10498/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/10498/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/10498/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/10498/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/10498/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/10498/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10498&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/10/10/sql-server-encrypted-stored-procedure-and-activity-monitor/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/encryptedSP.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER – Get Numeric Value From Alpha Numeric String – Get Numbers Only</title>
		<link>http://blog.sqlauthority.com/2010/09/25/sql-server-get-numeric-value-from-alpha-numeric-string-get-numbers-only/</link>
		<comments>http://blog.sqlauthority.com/2010/09/25/sql-server-get-numeric-value-from-alpha-numeric-string-get-numbers-only/#comments</comments>
		<pubDate>Sat, 25 Sep 2010 01:30:08 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL String]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=10366</guid>
		<description><![CDATA[I have earlier wrote article about SQL SERVER – Get Numeric Value From Alpha Numeric String – UDF for Get Numeric Numbers Only and it was very handy tool for me. Recently blog reader and SQL Expert Christofer has left excellent improvement to this logic. Here is his contribution. He has provided Stored Procedure and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10366&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">I have earlier wrote article about <strong><a href="http://blog.sqlauthority.com/2008/10/14/sql-server-get-numeric-value-from-alpha-numeric-string-udf-for-get-numeric-numbers-only/" target="_blank">SQL SERVER – Get Numeric Value From Alpha Numeric String – UDF for Get Numeric Numbers Only</a></strong> and it was very handy tool for me. Recently blog reader and SQL Expert <strong><a href="http://blog.sqlauthority.com/2008/10/14/sql-server-get-numeric-value-from-alpha-numeric-string-udf-for-get-numeric-numbers-only/#comment-89217">Christofer</a></strong> has left excellent improvement to this logic. Here is his contribution. He has provided Stored Procedure and the same can be easily converted to Function.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">CREATE PROCEDURE </span><span style="color:black;">[dbo].[CleanDataFromAlpha]<br />
</span><span style="color:#434343;">@alpha </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">50</span><span style="color:gray;">),<br />
</span><span style="color:#434343;">@decimal </span><span style="color:blue;">DECIMAL</span><span style="color:gray;">(</span><span style="color:black;">14</span><span style="color:gray;">, </span><span style="color:black;">5</span><span style="color:gray;">) </span><span style="color:black;">OUTPUT<br />
</span><span style="color:blue;">AS BEGIN<br />
SET NOCOUNT ON</span><span style="color:gray;">;<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@ErrorMsg </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">50</span><span style="color:gray;">)<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@Pos </span><span style="color:blue;">INT<br />
DECLARE </span><span style="color:#434343;">@CommaPos </span><span style="color:blue;">INT<br />
DECLARE </span><span style="color:#434343;">@ZeroExists </span><span style="color:blue;">INT<br />
DECLARE </span><span style="color:#434343;">@alphaReverse </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">50</span><span style="color:gray;">)<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@NumPos </span><span style="color:blue;">INT<br />
DECLARE </span><span style="color:#434343;">@Len </span><span style="color:blue;">INT<br />
</span><span style="color:green;">-- 1 Reverse the alpha in order to get the last position of a numeric value<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@alphaReverse </span><span style="color:blue;">= </span><span style="color:magenta;">REVERSE</span><span style="color:gray;">(</span><span style="color:#434343;">@alpha</span><span style="color:gray;">)<br />
</span><span style="color:green;">-- 2 Get the last position of a numeric figure<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@NumPos </span><span style="color:blue;">= </span><span style="color:magenta;">PATINDEX</span><span style="color:gray;">(</span><span style="color:red;">'%[0-9]%'</span><span style="color:gray;">, </span><span style="color:#434343;">@alphaReverse</span><span style="color:gray;">)<br />
</span><span style="color:green;">-- 3 Get the lenght of the string<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@Len </span><span style="color:blue;">= </span><span style="color:magenta;">LEN</span><span style="color:gray;">(</span><span style="color:#434343;">@alpha</span><span style="color:gray;">)<br />
</span><span style="color:green;">-- 4 Add a comma after the numeric data in case it's no decimal number<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@alpha </span><span style="color:blue;">= </span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:#434343;">@alpha</span><span style="color:gray;">, </span><span style="color:black;">1</span><span style="color:gray;">, (</span><span style="color:#434343;">@Len </span><span style="color:gray;">- </span><span style="color:#434343;">@NumPos </span><span style="color:gray;">+ </span><span style="color:black;">1</span><span style="color:gray;">))<br />
+ </span><span style="color:red;">','<br />
</span><span style="color:gray;">+ </span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:#434343;">@alpha</span><span style="color:gray;">, (</span><span style="color:#434343;">@Len </span><span style="color:gray;">- </span><span style="color:#434343;">@NumPos </span><span style="color:gray;">+ </span><span style="color:black;">2</span><span style="color:gray;">), </span><span style="color:black;">50</span><span style="color:gray;">)<br />
</span><span style="color:green;">-- Check if there is a zero (0) in the @alpha, then we later set the @decimal to 0<br />
-- if it's 0 after the handling, else we set @decimal to NULL<br />
-- If 0 no match, else there is a match<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@ZeroExists </span><span style="color:blue;">= CHARINDEX </span><span style="color:gray;">( </span><span style="color:red;">'0' </span><span style="color:gray;">, </span><span style="color:#434343;">@alpha </span><span style="color:gray;">,</span><span style="color:black;">1 </span><span style="color:gray;">)<br />
</span><span style="color:green;">-- Find position of , (comma)<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@CommaPos </span><span style="color:blue;">= </span><span style="color:black;">1<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@CommaPos </span><span style="color:blue;">= </span><span style="color:magenta;">PATINDEX</span><span style="color:gray;">(</span><span style="color:red;">'%,%'</span><span style="color:gray;">, </span><span style="color:#434343;">@alpha</span><span style="color:gray;">)<br />
</span><span style="color:blue;">IF </span><span style="color:gray;">(</span><span style="color:#434343;">@CommaPos </span><span style="color:blue;">= </span><span style="color:red;">''</span><span style="color:gray;">) </span><span style="color:blue;">BEGIN<br />
SET </span><span style="color:#434343;">@CommaPos </span><span style="color:blue;">= </span><span style="color:black;">20<br />
</span><span style="color:blue;">END<br />
SET </span><span style="color:#434343;">@Pos </span><span style="color:blue;">= </span><span style="color:magenta;">PATINDEX</span><span style="color:gray;">(</span><span style="color:red;">'%[^0-9]%'</span><span style="color:gray;">,</span><span style="color:#434343;">@alpha</span><span style="color:gray;">)<br />
</span><span style="color:green;">-- Replaces any aplha with '0' since we otherwice can't keep track of where the decimal<br />
-- should be put in. We assume the numeric number has no aplhe inside. The regular way<br />
-- to solve this is to replace with ”, but then we miss the way to find the place to<br />
-- put in the decimal.<br />
</span><span style="color:blue;">WHILE </span><span style="color:gray;">(</span><span style="color:#434343;">@Pos </span><span style="color:gray;">&gt; </span><span style="color:black;">0</span><span style="color:gray;">) </span><span style="color:blue;">BEGIN<br />
SET </span><span style="color:#434343;">@alpha </span><span style="color:blue;">= </span><span style="color:magenta;">STUFF</span><span style="color:gray;">(</span><span style="color:#434343;">@alpha</span><span style="color:gray;">, </span><span style="color:#434343;">@pos</span><span style="color:gray;">, </span><span style="color:black;">1</span><span style="color:gray;">, </span><span style="color:red;">'0'</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@Pos </span><span style="color:blue;">= </span><span style="color:magenta;">PATINDEX</span><span style="color:gray;">(</span><span style="color:red;">'%[^0-9]%'</span><span style="color:gray;">,</span><span style="color:#434343;">@alpha</span><span style="color:gray;">)<br />
</span><span style="color:blue;">END<br />
IF </span><span style="color:gray;">(</span><span style="color:#434343;">@alpha </span><span style="color:blue;">IS </span><span style="color:gray;">NOT NULL AND </span><span style="color:#434343;">@alpha </span><span style="color:gray;">!= </span><span style="color:red;">''</span><span style="color:gray;">) </span><span style="color:blue;">BEGIN<br />
SET </span><span style="color:#434343;">@decimal </span><span style="color:blue;">= </span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">DECIMAL</span><span style="color:gray;">(</span><span style="color:black;">14</span><span style="color:gray;">, </span><span style="color:black;">5</span><span style="color:gray;">), </span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:#434343;">@alpha</span><span style="color:gray;">, </span><span style="color:black;">1</span><span style="color:gray;">, (</span><span style="color:#434343;">@CommaPos </span><span style="color:gray;">- </span><span style="color:black;">1</span><span style="color:gray;">))<br />
+ </span><span style="color:red;">'.'<br />
</span><span style="color:gray;">+ </span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:#434343;">@alpha</span><span style="color:gray;">, (</span><span style="color:#434343;">@CommaPos </span><span style="color:gray;">+ </span><span style="color:black;">1</span><span style="color:gray;">), </span><span style="color:black;">20</span><span style="color:gray;">))<br />
</span><span style="color:blue;">END<br />
</span><span style="color:green;">-- Since we in this case don't want to set 0 if where is no numeric value, we set NULL to be safe<br />
</span><span style="color:blue;">IF </span><span style="color:gray;">(</span><span style="color:#434343;">@decimal </span><span style="color:blue;">= </span><span style="color:black;">0 </span><span style="color:gray;">AND </span><span style="color:#434343;">@ZeroExists </span><span style="color:blue;">= </span><span style="color:black;">0</span><span style="color:gray;">) </span><span style="color:blue;">BEGIN<br />
SET </span><span style="color:#434343;">@decimal </span><span style="color:blue;">= </span><span style="color:gray;">NULL<br />
</span><span style="color:blue;">END<br />
END<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;">If you run above SP as shown below it will work<br />
<code style="font-size:12px;"><span style="color:blue;">DECLARE </span><span style="color:#434343;">@myRetVal </span><span style="color:blue;">DECIMAL</span><span style="color:gray;">(</span><span style="color:black;">14</span><span style="color:gray;">,</span><span style="color:black;">5</span><span style="color:gray;">)<br />
</span><span style="color:blue;">EXEC </span><span style="color:black;">[CleanDataFromAlpha] </span><span style="color:red;">'ABC355,88ghf'</span><span style="color:gray;">, </span><span style="color:#434343;">@myRetVal </span><span style="color:black;">OUTPUT<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@myRetVal </span><span style="color:black;">ReturnValue</span></code></p>
<pre style="text-align:justify;">ReturnValue
---------------------------------------
355.88000

(1 row(s) affected)</pre>
<p style="text-align:justify;">I once again want to thanks <strong><a href="http://blog.sqlauthority.com/2008/10/14/sql-server-get-numeric-value-from-alpha-numeric-string-udf-for-get-numeric-numbers-only/#comment-89217">Christofer</a></strong> for his excellent contribution.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-function/'>SQL Function</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/sql-stored-procedure/'>SQL Stored Procedure</a>, <a href='http://blog.sqlauthority.com/category/sql-string/'>SQL String</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/10366/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/10366/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/10366/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/10366/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/10366/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/10366/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/10366/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/10366/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/10366/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/10366/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/10366/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/10366/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/10366/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/10366/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=10366&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/09/25/sql-server-get-numeric-value-from-alpha-numeric-string-get-numbers-only/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Check Advanced Server Configuration</title>
		<link>http://blog.sqlauthority.com/2010/07/29/sql-server-check-advanced-server-configuration/</link>
		<comments>http://blog.sqlauthority.com/2010/07/29/sql-server-check-advanced-server-configuration/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 01:30:51 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=9672</guid>
		<description><![CDATA[I was recently asked following question: &#8220;How I check all the advanced configuration of the SQL Server?&#8221; EXEC sp_configure 'Show Advanced Options', 1; GO RECONFIGURE; GO EXEC sp_configure; Running above script will return all the advanced server configurations. Reference: Pinal Dave (http://blog.sqlauthority.com) Filed under: Pinal Dave, SQL, SQL Authority, SQL Query, SQL Scripts, SQL Server, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9672&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I was recently asked following question:</p>
<p><em><strong>&#8220;How I check all the advanced configuration of the SQL Server?&#8221;</strong></em></p>
<p><code style="font-size:12px;"><span style="color:blue;">EXEC </span><span style="color:darkred;">sp_configure </span><span style="color:red;">'Show Advanced Options'</span><span style="color:gray;">, </span><span style="color:black;">1</span><span style="color:gray;">;<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">RECONFIGURE</span><span style="color:gray;">;<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">EXEC </span><span style="color:darkred;">sp_configure</span><span style="color:gray;">;<br />
</span></code></p>
<p>Running above script will return all the advanced server configurations.</p>
<p>Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.sqlauthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/sql-stored-procedure/'>SQL Stored Procedure</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/9672/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/9672/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/9672/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/9672/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/9672/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/9672/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/9672/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/9672/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/9672/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/9672/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/9672/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/9672/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/9672/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/9672/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9672&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/07/29/sql-server-check-advanced-server-configuration/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; TRANSACTION, DML and Schema Locks</title>
		<link>http://blog.sqlauthority.com/2010/06/21/sql-server-transaction-dml-and%c2%a0schema%c2%a0locks/</link>
		<comments>http://blog.sqlauthority.com/2010/06/21/sql-server-transaction-dml-and%c2%a0schema%c2%a0locks/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 01:30:23 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Interview Questions and Answers]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[SQL Transactions]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=9269</guid>
		<description><![CDATA[Today we will be going over a simple but interesting concept. Many a time, I have come across the lack of understanding on how the transactions work in SQL Server. Today we will go over a small but interesting observation. One of my clients had recently invited me to help them out with an interview [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9269&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Today we will be going over a simple but interesting concept. Many a time, I have come across the lack of understanding on how the transactions work in SQL Server. Today we will go over a small but interesting observation. One of my clients had recently invited me to help them out with an interview for their senior developers. I had interviewed nearly 50+ candidates in a single day. There were many different questions, but the following question was incorrectly answered most of the time.</p>
<p style="text-align:justify;">The question was to create a scenario where you can see the <strong>SCHEMA LOCK</strong>. The interview panel initially thought that this might be a very easy question for this particular interview. I, however, insisted them to keep this question for time being and then remove it from the list of interview questions only when at least 10 candidates got it right. Contrary to our expectations, we never reached a point where we had to remove this question from the list!</p>
<p style="text-align:justify;">Let us see a simple example regarding how to create a schema lock. <strong>The answer</strong> I was looking for is as follows: create a situation where the Schema is modified in the transaction and check the status of the object or session before the transactions are committed or rolled back.</p>
<p style="text-align:justify;">Run the following code in Query <strong>Session 1</strong>:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">BEGIN TRANSACTION<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">CREATE PROCEDURE </span><span style="color:black;">mySP<br />
</span><span style="color:blue;">AS<br />
SELECT </span><span style="color:black;">1<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">(</span><span style="color:red;">'mySP'</span><span style="color:gray;">) </span><span style="color:black;">ObjectID<br />
GO</span></code></p>
<p style="text-align:justify;">The above script will give us the objectID of the created stored procedure. In this case, the received ObjectID is 1300199682; this can be different for your execution.</p>
<p style="text-align:justify;">Run the following code in Query <strong>Session 2</strong>:</p>
<p style="text-align:justify;"><span style="color:blue;">USE </span>AdventureWorks</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:black;"> GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.procedures<br />
GO</span></code></p>
<p style="text-align:justify;">This query will never finish running as in Session 1, where we have created the Stored Procedure. The name is already listed in the sys.procedures, but the transactions in Session1 are not yet committed.</p>
<p style="text-align:justify;">If you run the following code, it will also not return any results even though we have received the ObjectID in Session 1.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">OBJECT_NAME</span><span style="color:gray;">(</span><span style="color:black;">1300199682</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;">Run the following code in Query <strong>Session 3</strong>:</p>
<p style="text-align:justify;">Now to confirm that a schema lock is created, we can check the dynamic management views dm_tran_locks.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.dm_tran_locks<br />
GO</span></code></p>
<p style="text-align:justify;">We can clearly see from the example that there is a Sch-M (schema modify) lock over our ObjectID.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/lockdmv.jpg" alt="" width="435" height="397" /></p>
<p style="text-align:justify;">You can specify the where condition to this DMV as we are know the ObjectID here.<br />
<code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">request_type</span><span style="color:gray;">, </span><span style="color:black;">request_mode</span><span style="color:gray;">, </span><span style="color:black;">resource_associated_entity_id</span><span style="color:gray;">, </span><span style="color:black;">request_type<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.dm_tran_locks<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">resource_associated_entity_id </span><span style="color:blue;">= </span><span style="color:black;">1300199682<br />
GO</span></code></p>
<p style="text-align:justify;">From above example, it is very clear that running DML code in the transactions create a schema modification lock until the transactions are over.</p>
<p style="text-align:justify;">If you run the COMMIT or ROLLBACK statement in Session 1, the Queries in Session 2 will complete right away.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-interview-questions-and-answers/'>SQL Interview Questions and Answers</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/sql-stored-procedure/'>SQL Stored Procedure</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sqlserver/'>SQLServer</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a> Tagged: <a href='http://blog.sqlauthority.com/tag/sql-transactions/'>SQL Transactions</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/9269/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/9269/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/9269/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/9269/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/9269/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/9269/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/9269/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/9269/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/9269/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/9269/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/9269/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/9269/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/9269/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/9269/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9269&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/06/21/sql-server-transaction-dml-and%c2%a0schema%c2%a0locks/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/lockdmv.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Stored Procedure and Transactions</title>
		<link>http://blog.sqlauthority.com/2010/06/02/sql-server-stored-procedure-and-transactions/</link>
		<comments>http://blog.sqlauthority.com/2010/06/02/sql-server-stored-procedure-and-transactions/#comments</comments>
		<pubDate>Wed, 02 Jun 2010 01:30:45 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=9147</guid>
		<description><![CDATA[I just overheard the following statement &#8211; &#8220;I do not use Transactions in SQL as I use Stored Procedure&#8220;. I just realized that there are so many misconceptions about this subject. Transactions has nothing to do with Stored Procedures. Let me demonstrate that with a simple example. USE tempdb GO -- Create 3 Test Tables CREATE TABLE TABLE1 (ID INT); [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9147&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">I just overheard the following statement &#8211; &#8220;<strong><em>I do not use Transactions in SQL as I use Stored Procedure</em></strong>&#8220;.</p>
<p style="text-align:justify;">I just realized that there are so many misconceptions about this subject. Transactions has nothing to do with Stored Procedures. Let me demonstrate that with a simple example.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">tempdb<br />
GO<br />
</span><span style="color:green;">-- Create 3 Test Tables<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">TABLE1 </span><span style="color:gray;">(</span><span style="color:black;">ID </span><span style="color:blue;">INT</span><span style="color:gray;">);<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">TABLE2 </span><span style="color:gray;">(</span><span style="color:black;">ID </span><span style="color:blue;">INT</span><span style="color:gray;">);<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">TABLE3 </span><span style="color:gray;">(</span><span style="color:black;">ID </span><span style="color:blue;">INT</span><span style="color:gray;">);<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Create SP<br />
</span><span style="color:blue;">CREATE PROCEDURE </span><span style="color:black;">TestSP<br />
</span><span style="color:blue;">AS<br />
INSERT INTO </span><span style="color:black;">TABLE1 </span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">1</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">TABLE2 </span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:red;">'a'</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">TABLE3 </span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">3</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Execute SP<br />
-- SP will error out<br />
</span><span style="color:blue;">EXEC </span><span style="color:black;">TestSP<br />
GO<br />
</span><span style="color:green;">-- Check the Values in Table<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">TABLE1</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">TABLE2</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">TABLE3</span><span style="color:gray;">;<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;">Now, the main point is: If Stored Procedure is transactional then, it should roll back complete transactions when it encounters any errors. Well, that does not happen in this case, which proves that Stored Procedure does not only provide just the transactional feature to a batch of T-SQL.</p>
<p style="text-align:justify;">Let’s see the result very quickly.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/SPTran1.jpg" alt="" width="500" height="232" /></p>
<p style="text-align:justify;">It is very clear that there were entries in table1 which are not shown in the subsequent tables. If SP was transactional in terms of T-SQL Query Batches, there would be no entries in any of the tables. If you want to use Transactions with Stored Procedure, wrap the code around with BEGIN TRAN and COMMIT TRAN.</p>
<p style="text-align:justify;">The example is as following.<br />
<code style="font-size:12px;"><span style="color:blue;">CREATE PROCEDURE </span><span style="color:black;">TestSPTran<br />
</span><span style="color:blue;">AS<br />
BEGIN TRAN<br />
INSERT INTO </span><span style="color:black;">TABLE1 </span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">11</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">TABLE2 </span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:red;">'b'</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">TABLE3 </span><span style="color:gray;">(</span><span style="color:black;">ID</span><span style="color:gray;">)<br />
</span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:black;">33</span><span style="color:gray;">)<br />
</span><span style="color:blue;">COMMIT<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Execute SP<br />
</span><span style="color:blue;">EXEC </span><span style="color:black;">TestSPTran<br />
GO<br />
</span><span style="color:green;">-- Check the Values in Tables<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">TABLE1</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">TABLE2</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">TABLE3</span><span style="color:gray;">;<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Clean up<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">Table1<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">Table2<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:black;">Table3<br />
GO</span></code></p>
<p style="text-align:justify;">In this case, there will be no entries in any part of the table. What is your opinion about this blog post? Please leave your comments about it here.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/sql-stored-procedure/'>SQL Stored Procedure</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/9147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/9147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/9147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/9147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/9147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/9147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/9147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/9147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/9147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/9147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/9147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/9147/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/9147/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/9147/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=9147&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/06/02/sql-server-stored-procedure-and-transactions/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/SPTran1.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Recompile Stored Procedure at Run Time</title>
		<link>http://blog.sqlauthority.com/2010/02/20/sql-server-recompile-stored-procedure-at-run-time/</link>
		<comments>http://blog.sqlauthority.com/2010/02/20/sql-server-recompile-stored-procedure-at-run-time/#comments</comments>
		<pubDate>Sat, 20 Feb 2010 01:30:46 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8011</guid>
		<description><![CDATA[I recently received an email from reader after reading my previous article on SQL SERVER – Plan Recompilation and Reduce Recompilation – Performance Tuning regarding how to recompile any stored procedure at run time. There are multiple ways to do this. If you want your stored procedure to always recompile at run time, you can add [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=8011&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">I recently received an email from reader after reading my previous article on <a href="http://blog.sqlauthority.com/2010/02/18/sql-server-plan-recompilation-and-reduce-recompilation-performance-tuning/" target="_blank"><strong>SQL SERVER – Plan Recompilation and Reduce Recompilation – Performance Tuning</strong></a> regarding how to recompile any stored procedure at run time. There are multiple ways to do this. If you want your stored procedure to always recompile at run time, you can add the keyword RECOMPILE when you create the stored procedure. Additionally, if the stored procedure has to be recompiled at only one time, in that case, you can add RECOMPILE word one time only and run the SP as well. Let us go over these two options.</p>
<p style="text-align:justify;">The RECOMPILE hint is used with a query and recompiles only that particular query. However, if the parameters are used in many statements in the stored procedure and we want to recompile all the statements, then instead of using the RECOMPILE option with all the queries, we have one better option that uses WITH RECOMPILE during stored procedure creation or execution.</p>
<p style="text-align:justify;"><strong> </strong></p>
<p style="text-align:justify;"><strong>Option 1:</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">CREATE PROCEDURE </span><span style="color:black;">dbo.PersonAge </span><span style="color:gray;">(</span><span style="color:#434343;">@MinAge </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:#434343;">@MaxAge </span><span style="color:blue;">INT</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WITH </span><span style="color:black;">RECOMPILE<br />
</span><span style="color:blue;">AS<br />
SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.tblPerson<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">Age </span><span style="color:gray;">&gt;= </span><span style="color:#434343;">@MinAge </span><span style="color:gray;">AND </span><span style="color:black;">Age </span><span style="color:gray;">&lt;= </span><span style="color:#434343;">@MaxAge<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;"><strong>Option 2:</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">EXEC </span><span style="color:black;">dbo.PersonAge 65</span><span style="color:gray;">,</span><span style="color:black;">70 </span><span style="color:blue;">WITH </span><span style="color:black;">RECOMPILE</span></code></p>
<p style="text-align:justify;">This method is not recommended for large stored procedures because the recompilation of so many statements may outweigh the benefit of a better execution plan.</p>
<p style="text-align:justify;">Note: Recompilation is not necessarily the best solution.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/sql-stored-procedure/'>SQL Stored Procedure</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/8011/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/8011/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/8011/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/8011/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/8011/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/8011/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/8011/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/8011/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/8011/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/8011/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/8011/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/8011/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/8011/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/8011/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=8011&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/02/20/sql-server-recompile-stored-procedure-at-run-time/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Plan Recompilation and Reduce Recompilation &#8211; Performance Tuning</title>
		<link>http://blog.sqlauthority.com/2010/02/18/sql-server-plan-recompilation-and-reduce-recompilation-performance-tuning/</link>
		<comments>http://blog.sqlauthority.com/2010/02/18/sql-server-plan-recompilation-and-reduce-recompilation-performance-tuning/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 01:30:05 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL System Table]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8008</guid>
		<description><![CDATA[Recompilation process is same as compilation and degrades server performance. In SQL Server 2000 and earlier versions, this was a serious issue but in SQL server 2005, the severity of this issue has been significantly reduced by introducing a new feature called Statement-level recompilation. When SQL Server 2005 recompiles stored procedures, only the statement that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=8008&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Recompilation process is same as compilation and degrades server performance. In SQL Server 2000 and earlier versions, this was a serious issue but in SQL server 2005, the severity of this issue has been significantly reduced by introducing a new feature called Statement-level recompilation. When SQL Server 2005 recompiles stored procedures, only the statement that causes recompilation is compiled, rather than the entire procedure. Recompilation occurs because of following reason:</p>
<ul style="text-align:justify;">
<li><strong>On schema change of objects. </strong>
<ul>
<li>Adding or dropping column to/from a table or view</li>
<li>Adding or dropping constraints, defaults, or rules to or from a table.</li>
<li>Adding or dropping an index to a table or indexed view if index is used by the plan.</li>
<li>Adding or dropping trigger from a table</li>
<li>Dropping statistics from a table that is used in plan.</li>
</ul>
</li>
<li><strong>On change of the SET options:</strong> When a compiled plan is created, the SQL server also stores the environmental setting of a connection (SET option) with it. If the same stored procedure is executed by another connection that has a different SET option, then the existing cached plan is not reused. To reduce recompilation caused by Environment change, we should not change the SET options in the connection setting and stored procedure.</li>
<li><strong>On statistics change of tables:</strong> Every time the SQL Server uses an already cached compiled plan, it checks the optimality before using it. SQL Server decides whether the plan is optimal for current amount of data in underlying tables. It could be that the data amount in underlying tables changed so much that the previously created plan is not optimized. For each table &amp; index, the SQL server maintains a modification counter and if the counter values exceed the defined threshold, the previously created compiled plan is considered stale plan and a new plan is created.</li>
</ul>
<p style="text-align:justify;"><strong>Detecting recompilations:</strong> The below query retrieves the top 10 statements for which the recompilation count is maximum. Here, plan_generation_num returns a number that indicates the recompilation count of a statement.</p>
<p style="padding-left:30px;text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT TOP </span><span style="color:black;">10<br />
qs.plan_generation_num</span><span style="color:gray;">,<br />
</span><span style="color:black;">qs.execution_count</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">DB_NAME</span><span style="color:gray;">(</span><span style="color:black;">st.dbid</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">DbName</span><span style="color:gray;">,<br />
</span><span style="color:black;">st.objectid</span><span style="color:gray;">,<br />
</span><span style="color:black;">st.</span><span style="color:blue;">TEXT<br />
FROM </span><span style="color:black;">sys.dm_exec_query_stats qs<br />
</span><span style="color:gray;">CROSS </span><span style="color:black;">APPLY sys.dm_exec_sql_text</span><span style="color:gray;">(</span><span style="color:black;">sql_handle</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">st<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">plan_generation_num </span><span style="color:blue;">DESC</span></code></p>
<p style="text-align:justify;">Another tool is to get recompilation details is the SQL Profiler. Along with the statements text, it also tells the reason of recompilation in EventClass and EventSubClass columns.</p>
<p style="text-align:justify;"><strong>Reducing recompilations: </strong>We have no option to avoid “schema change” and “SET options” based recompilation. But we have following query and stored procedure hints to avoid “statistics change” based recompilation:</p>
<ul style="text-align:justify;">
<li> <strong>KEEP PLAN hint:</strong> The modification counter threshold for a temporary table is 6. This implies that when a stored procedure that creates a temporary table inserts 6 or more rows into this table, Stored Procedures will be recompiled as soon as this table is accessed. For permanent tables, this threshold is at least 500. We can increase the first threshold for the temporary table (6) to same as that of the permanent table (500) by using the KEEP PLAN query hint in the statement where the temporary table is used. For example,</li>
</ul>
<p style="padding-left:60px;text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">TT.col4</span><span style="color:gray;">, </span><span style="color:magenta;">SUM</span><span style="color:gray;">(</span><span style="color:black;">PermTable.col1</span><span style="color:gray;">)<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.PermTable<br />
</span><span style="color:blue;">INNER JOIN </span><span style="color:#434343;">#TempTable </span><span style="color:blue;">AS </span><span style="color:black;">TT<br />
</span><span style="color:blue;">ON </span><span style="color:black;">PermTable.col1 </span><span style="color:blue;">= </span><span style="color:black;">TT.col2<br />
</span><span style="color:blue;">OPTION </span><span style="color:gray;">(</span><span style="color:black;">KEEP </span><span style="color:blue;">PLAN</span><span style="color:gray;">);</span></code></p>
<ul style="text-align:justify;">
<li> <strong>KEEPFIXED PLAN hint:</strong> This hint completely avoids “statistics change” based recompilation of a query.</li>
</ul>
<p style="padding-left:60px;text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">col1</span><span style="color:gray;">, </span><span style="color:black;">col2<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.PermTable<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">col3 </span><span style="color:gray;">&amp;</span><span style="color:black;">lt</span><span style="color:gray;">; </span><span style="color:black;">100<br />
</span><span style="color:blue;">OPTION </span><span style="color:gray;">(</span><span style="color:black;">KEEPFIXED </span><span style="color:blue;">PLAN</span><span style="color:gray;">);</span></code></p>
<ul style="text-align:justify;">
<li> <strong>Use Table variable instead of temporary tables:</strong> Because a change in cardinality of a table variable does not cause recompilations, consider using a table variable instead of a temporary table when faced with a problem of excessive recompilation. However, the use of table variables can lead to poorer query plans. Distribution statistics are not stored for table variables, and cardinality is only available during recompilation and not during initial compilation. One has to check whether this is the case and make an appropriate trade-off.</li>
</ul>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-index/'>SQL Index</a>, <a href='http://blog.sqlauthority.com/category/sql-optimization/'>SQL Optimization</a>, <a href='http://blog.sqlauthority.com/category/sql-performance/'>SQL Performance</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/sql-stored-procedure/'>SQL Stored Procedure</a>, <a href='http://blog.sqlauthority.com/category/sql-system-table/'>SQL System Table</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/8008/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/8008/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/8008/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/8008/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/8008/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/8008/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/8008/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/8008/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/8008/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/8008/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/8008/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/8008/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/8008/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/8008/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=8008&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/02/18/sql-server-plan-recompilation-and-reduce-recompilation-performance-tuning/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Stored Procedure Optimization Tips &#8211; Best Practices</title>
		<link>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/</link>
		<comments>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 01:30:11 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Coding Standards]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=8000</guid>
		<description><![CDATA[We will go over how to optimize Stored Procedure with making simple changes in the code. Please note there are many more other tips, which we will cover in future articles. Include SET NOCOUNT ON statement: With every SELECT and DML statement, the SQL server returns a message that indicates the number of affected rows [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=8000&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>We will go over how to optimize Stored Procedure with making simple changes in the code. Please note there are many more other tips, which we will cover in future articles.<strong><br />
</strong></p>
<p style="text-align:justify;"><strong> </strong></p>
<ul style="text-align:justify;">
<li><strong>Include SET NOCOUNT ON statement:</strong> With every SELECT and DML statement, the SQL server returns a message that indicates the number of affected rows by that statement. This information is mostly helpful in debugging the code, but it is useless after that. By setting SET NOCOUNT ON, we can disable the feature of returning this extra information. For stored procedures that contain several statements or contain Transact-SQL loops, setting SET NOCOUNT to ON can provide a significant performance boost because network traffic is greatly reduced.</li>
</ul>
<p style="padding-left:60px;text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">CREATE PROC </span><span style="color:black;">dbo.ProcName<br />
</span><span style="color:blue;">AS<br />
SET </span><span style="color:black;">NOCOUNT </span><span style="color:blue;">ON</span><span style="color:gray;">;<br />
</span><span style="color:green;">--Procedure code here<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">column1 </span><span style="color:blue;">FROM </span><span style="color:black;">dbo.TblTable1<br />
</span><span style="color:green;">-- Reset SET NOCOUNT to OFF<br />
</span><span style="color:blue;">SET </span><span style="color:black;">NOCOUNT </span><span style="color:blue;">OFF</span><span style="color:gray;">;<br />
</span><span style="color:black;">GO </span></code></p>
<ul style="text-align:justify;">
<li><strong>Use schema name with object name:</strong> The object name is qualified if used with schema name. Schema name should be used with the stored procedure name and with all objects referenced inside the stored procedure. This help in directly finding the complied plan instead of searching the objects in other possible schema before finally deciding to use a cached plan, if available. This process of searching and deciding a schema for an object leads to COMPILE lock on stored procedure and decreases the stored procedure’s performance. Therefore, always refer the objects with qualified name in the stored procedure like</li>
</ul>
<p style="padding-left:60px;text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:gray;">* </span><span style="color:blue;">FROM </span><span style="color:black;">dbo.MyTable </span><span style="color:green;">-- Preferred method<br />
-- Instead of<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">* </span><span style="color:blue;">FROM </span><span style="color:black;">MyTable </span><span style="color:green;">-- Avoid this method<br />
--And finally call the stored procedure with qualified name like:<br />
</span><span style="color:blue;">EXEC </span><span style="color:black;">dbo.MyProc </span><span style="color:green;">-- Preferred method<br />
--Instead of<br />
</span><span style="color:blue;">EXEC </span><span style="color:black;">MyProc </span><span style="color:green;">-- Avoid this method</span></code></p>
<ul style="text-align:justify;">
<li><strong>Do not use the prefix &#8220;sp_&#8221; in the stored procedure name</strong>: If a stored procedure name begins with “SP_,” then SQL server first searches in the master database and then in the current session database. Searching in the master database causes extra overhead and even a wrong result if another stored procedure with the same name is found in master database.</li>
</ul>
<ul style="text-align:justify;">
<li><strong>Use IF EXISTS (SELECT 1) instead of (SELECT *):</strong> To check the existence of a record in another table, we uses the IF EXISTS clause. The IF EXISTS clause returns True if any value is returned from an internal statement, either a single value “1” or all columns of a record or complete recordset. The output of the internal statement is not used. Hence, to minimize the data for processing and network transferring, we should use “1” in the SELECT clause of an internal statement, as shown below:</li>
</ul>
<p style="padding-left:60px;text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">IF </span><span style="color:gray;">EXISTS (</span><span style="color:blue;">SELECT </span><span style="color:black;">1 </span><span style="color:blue;">FROM </span><span style="color:black;">sysobjects<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">name </span><span style="color:blue;">= </span><span style="color:red;">'MyTable' </span><span style="color:gray;">AND </span><span style="color:black;">type </span><span style="color:blue;">= </span><span style="color:red;">'U'</span><span style="color:gray;">)</span></code> <strong> </strong></p>
<ul>
<li><strong>Use the sp_executesql stored procedure instead of the EXECUTE statement.</strong><br />
The sp_executesql stored procedure supports parameters. So, using the sp_executesql stored procedure instead of the EXECUTE statement improve the re-usability of your code. The execution plan of a dynamic statement can be reused only if each and every character, including case, space, comments and parameter, is same for two statements. For example, if we execute the below batch:</li>
</ul>
<p style="padding-left:60px;text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">DECLARE </span><span style="color:#434343;">@Query </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">100</span><span style="color:gray;">)<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@Age </span><span style="color:blue;">INT<br />
SET </span><span style="color:#434343;">@Age </span><span style="color:blue;">= </span><span style="color:black;">25<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@Query </span><span style="color:blue;">= </span><span style="color:red;">'SELECT * FROM dbo.tblPerson WHERE Age = ' </span><span style="color:gray;">+ </span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">3</span><span style="color:gray;">),</span><span style="color:#434343;">@Age</span><span style="color:gray;">)<br />
</span><span style="color:blue;">EXEC </span><span style="color:gray;">(</span><span style="color:#434343;">@Query</span><span style="color:gray;">)</span></code></p>
<p style="text-align:justify;">If we again execute the above batch using different @Age value, then the execution plan for SELECT statement created for @Age =25 would not be reused. However, if we write the above batch as given below,</p>
<p style="padding-left:60px;text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">DECLARE </span><span style="color:#434343;">@Query </span><span style="color:blue;">NVARCHAR</span><span style="color:gray;">(</span><span style="color:black;">100</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@Query </span><span style="color:blue;">= </span><span style="color:red;">N'SELECT * FROM dbo.tblPerson WHERE Age = @Age'<br />
</span><span style="color:blue;">EXECUTE </span><span style="color:darkred;">sp_executesql </span><span style="color:#434343;">@Query</span><span style="color:gray;">, </span><span style="color:red;">N'@Age int'</span><span style="color:gray;">, </span><span style="color:#434343;">@Age </span><span style="color:blue;">= </span><span style="color:black;">25</span></code></p>
<p style="text-align:justify;">the compiled plan of this SELECT statement will be reused for different value of @Age parameter. The reuse of the existing complied plan will result in improved performance.</p>
<ul style="text-align:justify;">
<li> <strong>Try to avoid using SQL Server cursors whenever possible:</strong> Cursor uses a lot of resources for overhead processing to maintain current record position in a recordset and this decreases the performance. If we need to process records one-by-one in a loop, then we should use the WHILE clause. Wherever possible, we should replace the cursor-based approach with SET-based approach. Because the SQL Server engine is designed and optimized to perform SET-based operation very fast. Again, please note cursor is also a kind of WHILE Loop.</li>
<li><strong>Keep the Transaction as short as possible:</strong> The length of transaction affects blocking and deadlocking. Exclusive lock is not released until the end of transaction. In higher isolation level, the shared locks are also aged with transaction. Therefore, lengthy transaction means locks for longer time and locks for longer time turns into blocking. In some cases, blocking also converts into deadlocks. So, for faster execution and less blocking, the transaction should be kept as short as possible.</li>
<li> <strong>Use TRY-Catch for error handling</strong>: Prior to SQL server 2005 version code for error handling, there was a big portion of actual code because an error check statement was written after every t-sql statement. More code always consumes more resources and time. In SQL Server 2005, a new simple way is introduced for the same purpose. The syntax is as follows:</li>
</ul>
<p style="padding-left:60px;text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">BEGIN </span><span style="color:black;">TRY<br />
</span><span style="color:green;">--Your t-sql code goes here<br />
</span><span style="color:blue;">END </span><span style="color:black;">TRY<br />
</span><span style="color:blue;">BEGIN </span><span style="color:black;">CATCH<br />
</span><span style="color:green;">--Your error handling code goes here<br />
</span><span style="color:blue;">END </span><span style="color:black;">CATCH</span></code></p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Filed under: <a href='http://blog.sqlauthority.com/category/best-practices/'>Best Practices</a>, <a href='http://blog.sqlauthority.com/category/tech/pinal-dave/'>Pinal Dave</a>, <a href='http://blog.sqlauthority.com/category/technology/sql/'>SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-authority/'>SQL Authority</a>, <a href='http://blog.sqlauthority.com/category/sql-coding-standards/'>SQL Coding Standards</a>, <a href='http://blog.sqlauthority.com/category/sql-optimization/'>SQL Optimization</a>, <a href='http://blog.sqlauthority.com/category/sql-performance/'>SQL Performance</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-query/'>SQL Query</a>, <a href='http://blog.sqlauthority.com/category/tech/sql-scripts/'>SQL Scripts</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-server/'>SQL Server</a>, <a href='http://blog.sqlauthority.com/category/sql-stored-procedure/'>SQL Stored Procedure</a>, <a href='http://blog.sqlauthority.com/category/technology/sql-tips-and-tricks/'>SQL Tips and Tricks</a>, <a href='http://blog.sqlauthority.com/category/sqlserver/'>SQLServer</a>, <a href='http://blog.sqlauthority.com/category/technology/t-sql/'>T SQL</a>, <a href='http://blog.sqlauthority.com/category/technology/'>Technology</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/8000/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/8000/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/8000/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/8000/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/8000/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/8000/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/8000/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/8000/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/8000/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/8000/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/8000/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/8000/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/8000/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/8000/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=8000&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2010/02/16/sql-server-stored-procedure-optimization-tips-best-practices/feed/</wfw:commentRss>
		<slash:comments>132</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Size of Index Table &#8211; A Puzzle to Find Index Size for Each Index on Table</title>
		<link>http://blog.sqlauthority.com/2009/11/18/sql-server-size-of-index-table-a-puzzle-to-find-index-size-for-each-index-on-table/</link>
		<comments>http://blog.sqlauthority.com/2009/11/18/sql-server-size-of-index-table-a-puzzle-to-find-index-size-for-each-index-on-table/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 01:30:16 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[Readers Question]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Puzzle]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7388</guid>
		<description><![CDATA[It is very easy to find out some basic details of any table using the following Stored Procedure. USE AdventureWorks GO EXEC sp_spaceused [HumanResources.Shift] GO Above query will return following resultset The above SP provides basic details such as rows, data size in table, and Index size of all the indexes on the table. If [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=7388&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">It is very easy to find out some basic details of any table using the following Stored Procedure.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">EXEC </span><span style="color:darkred;">sp_spaceused </span><span style="color:black;">[HumanResources.Shift]<br />
GO</span></code></p>
<p style="text-align:justify;">Above query will return following resultset</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/sp_spaceused1.jpg" alt="" width="369" height="153" /></p>
<p style="text-align:justify;">The above SP provides basic details such as rows, data size in table, and Index size of all the indexes on the table.</p>
<p style="text-align:justify;">If we look at this carefully, a total of three indexes can be found on the table HumanResources.Shift.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.indexes<br />
</span><span style="color:blue;">WHERE </span><span style="color:magenta;">OBJECT_ID </span><span style="color:blue;">= </span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">(</span><span style="color:red;">'HumanResources.Shift'</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;">The above query will give result with query listing all the index on the table.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/indexspace.jpg" alt="" width="500" height="248" /></p>
<p style="text-align:justify;">There is a small puzzle for all of you here. The puzzle is to write a query that will return the size for each index that is listed in above query. We need a query that will return an additional column in the above listed query and it should contain the size of the index. In our case, we will have three different sizes, which should add up to a total of 40 KB as shown in earlier query, where the total size is displayed.</p>
<p style="text-align:justify;">I will publish the solution with due credit on this blog.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.sqlauthority.com</strong></a><strong>)</strong></p>
<br />Posted in Pinal Dave, Readers Question, SQL, SQL Authority, SQL Index, SQL Puzzle, SQL Query, SQL Scripts, SQL Server, SQL Stored Procedure, SQL Tips and Tricks, SQLServer, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/7388/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/7388/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/7388/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/7388/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/7388/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/7388/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/7388/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/7388/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/7388/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/7388/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/7388/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/7388/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/7388/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/7388/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=7388&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/11/18/sql-server-size-of-index-table-a-puzzle-to-find-index-size-for-each-index-on-table/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/sp_spaceused1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/indexspace.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Stored Procedure are Compiled on First Run &#8211; SP taking Longer to Run First Time</title>
		<link>http://blog.sqlauthority.com/2009/11/08/sql-server-stored-procedure-are-compiled-on-first-run-sp-taking-longer-to-run-first-time/</link>
		<comments>http://blog.sqlauthority.com/2009/11/08/sql-server-stored-procedure-are-compiled-on-first-run-sp-taking-longer-to-run-first-time/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 01:30:34 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Interview Questions and Answers]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[SQL Cache]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7164</guid>
		<description><![CDATA[During the PASS summit, one of the attendees asked me the following question. Why the Stored Procedure takes long time to run for first time? The reason for the same is because Stored Procedures are compiled when it runs first time. When I answered the same, he replied that Stored Procedures are pre-compiled, and this [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=7164&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">During the PASS summit, one of the attendees asked me the following question.</p>
<p style="text-align:justify;"><strong><em>Why the Stored Procedure takes long time to run for first time?</em></strong></p>
<p style="text-align:justify;"><strong>The reason for the same is because Stored Procedures are compiled when it runs first time. </strong>When I answered the same, he replied that Stored Procedures are pre-compiled, and this should not be the case. In fact, Stored Procedures are not pre-compiled; they compile only during their first time execution.</p>
<p style="text-align:justify;">There is a misconception that stored procedures are pre-compiled. They are not pre-compiled, but compiled only during the first run. For every subsequent runs, it is for sure pre-compiled.</p>
<p style="text-align:justify;">If you create any SP, you will find that there is no cache entry for the execution of that SP.</p>
<p style="text-align:justify;">After running the SP for the first time, the entry for the cache is made in the system.</p>
<p style="text-align:justify;">If we see the following script, we can notice the different of cache when SP was created and SP was executed.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Exeercise to verify if stored procedure pre-compiled */<br />
</span><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:green;">-- Clean Cache<br />
</span><span style="color:blue;">DBCC </span><span style="color:black;">FREEPROCCACHE<br />
GO<br />
</span><span style="color:blue;">IF </span><span style="color:gray;">EXISTS (</span><span style="color:blue;">SELECT </span><span style="color:gray;">* </span><span style="color:blue;">FROM </span><span style="color:black;">sys.objects </span><span style="color:blue;">WHERE </span><span style="color:magenta;">OBJECT_ID </span><span style="color:blue;">= </span><span style="color:magenta;">OBJECT_ID</span><span style="color:gray;">(</span><span style="color:red;">N'[dbo].[CompSP]'</span><span style="color:gray;">) AND </span><span style="color:black;">type </span><span style="color:blue;">IN </span><span style="color:gray;">(</span><span style="color:red;">N'P'</span><span style="color:gray;">, </span><span style="color:red;">N'PC'</span><span style="color:gray;">))<br />
</span><span style="color:blue;">DROP PROCEDURE </span><span style="color:black;">[dbo].[CompSP]<br />
GO<br />
</span><span style="color:green;">-- Create New Stored Procedure<br />
</span><span style="color:blue;">CREATE PROCEDURE </span><span style="color:black;">CompSP<br />
</span><span style="color:blue;">AS<br />
SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Department<br />
GO<br />
</span><span style="color:green;">-- Check the Query Plan for SQL Batch<br />
-- You will find that there is no ObjectName with CompSP<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">cp.objtype </span><span style="color:blue;">AS </span><span style="color:black;">PlanType</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">OBJECT_NAME</span><span style="color:gray;">(</span><span style="color:black;">st.objectid</span><span style="color:gray;">,</span><span style="color:black;">st.dbid</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">ObjectName</span><span style="color:gray;">,<br />
</span><span style="color:black;">cp.refcounts </span><span style="color:blue;">AS </span><span style="color:black;">ReferenceCounts</span><span style="color:gray;">,<br />
</span><span style="color:black;">cp.usecounts </span><span style="color:blue;">AS </span><span style="color:black;">UseCounts</span><span style="color:gray;">,<br />
</span><span style="color:black;">st.</span><span style="color:blue;">TEXT AS </span><span style="color:black;">SQLBatch</span><span style="color:gray;">,<br />
</span><span style="color:black;">qp.query_plan </span><span style="color:blue;">AS </span><span style="color:black;">QueryPlan<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.dm_exec_cached_plans </span><span style="color:blue;">AS </span><span style="color:black;">cp<br />
</span><span style="color:gray;">CROSS </span><span style="color:black;">APPLY sys.dm_exec_query_plan</span><span style="color:gray;">(</span><span style="color:black;">cp.plan_handle</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">qp<br />
</span><span style="color:gray;">CROSS </span><span style="color:black;">APPLY sys.dm_exec_sql_text</span><span style="color:gray;">(</span><span style="color:black;">cp.plan_handle</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">st</span><span style="color:gray;">;<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">/* Execute Stored Procedure */<br />
</span><span style="color:blue;">EXEC </span><span style="color:black;">CompSP<br />
GO<br />
</span><span style="color:green;">-- Check the Query Plan for SQL Batch<br />
-- You will find that there is one entry with name ObjectName with name CompSP<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">cp.objtype </span><span style="color:blue;">AS </span><span style="color:black;">PlanType</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">OBJECT_NAME</span><span style="color:gray;">(</span><span style="color:black;">st.objectid</span><span style="color:gray;">,</span><span style="color:black;">st.dbid</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">ObjectName</span><span style="color:gray;">,<br />
</span><span style="color:black;">cp.refcounts </span><span style="color:blue;">AS </span><span style="color:black;">ReferenceCounts</span><span style="color:gray;">,<br />
</span><span style="color:black;">cp.usecounts </span><span style="color:blue;">AS </span><span style="color:black;">UseCounts</span><span style="color:gray;">,<br />
</span><span style="color:black;">st.</span><span style="color:blue;">TEXT AS </span><span style="color:black;">SQLBatch</span><span style="color:gray;">,<br />
</span><span style="color:black;">qp.query_plan </span><span style="color:blue;">AS </span><span style="color:black;">QueryPlan<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.dm_exec_cached_plans </span><span style="color:blue;">AS </span><span style="color:black;">cp<br />
</span><span style="color:gray;">CROSS </span><span style="color:black;">APPLY sys.dm_exec_query_plan</span><span style="color:gray;">(</span><span style="color:black;">cp.plan_handle</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">qp<br />
</span><span style="color:gray;">CROSS </span><span style="color:black;">APPLY sys.dm_exec_sql_text</span><span style="color:gray;">(</span><span style="color:black;">cp.plan_handle</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">st</span><span style="color:gray;">;<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;">The result set of above query is as following.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/SPComp.jpg" alt="" width="500" height="396" /></p>
<p style="text-align:justify;">The above script to find out the cache is taken from the white paper <a href="http://blog.sqlauthority.com/2009/08/29/sql-server-plan-caching-in-sql-server-2008-by-greg-low/" target="_blank"><strong>SQL SERVER – Plan Caching in SQL Server 2008 by Greg Low</strong></a>. You can also read my follow up article <a href="http://blog.sqlauthority.com/2009/09/14/sql-server-plan-caching-and-schema-change-an-interesting-observation/" target="_blank"><strong>SQL SERVER – Plan Caching and Schema Change – An Interesting Observation</strong></a>, where I have given an interesting conversation with Greg Low.</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Posted in Pinal Dave, SQL, SQL Authority, SQL Interview Questions and Answers, SQL Query, SQL Scripts, SQL Server, SQL Stored Procedure, SQL Tips and Tricks, T SQL, Technology Tagged: SQL Cache <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/7164/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/7164/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/7164/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/7164/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/7164/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/7164/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/7164/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/7164/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/7164/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/7164/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/7164/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/7164/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/7164/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/7164/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=7164&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/11/08/sql-server-stored-procedure-are-compiled-on-first-run-sp-taking-longer-to-run-first-time/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/SPComp.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Insert Values of Stored Procedure in Table &#8211; Use Table Valued Function</title>
		<link>http://blog.sqlauthority.com/2009/09/23/sql-server-insert-values-of-stored-procedure-in-table-use-table-valued-function/</link>
		<comments>http://blog.sqlauthority.com/2009/09/23/sql-server-insert-values-of-stored-procedure-in-table-use-table-valued-function/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 01:30:24 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6791</guid>
		<description><![CDATA[I recently got many emails requesting to write a simple article.  I also got a request to explain different ways to insert the values from a stored procedure into a table. Let us quickly look at the conventional way of doing the same. Please note that this only works with the stored procedure with only [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=6791&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">
<p style="text-align:justify;">I recently got many emails requesting to write a simple article.  I also got a request to explain different ways to insert the values from a stored procedure into a table. Let us quickly look at the conventional way of doing the same.</p>
<p style="text-align:justify;">Please note that this only works with the stored procedure with only one resultset. Let us create a stored procedure that returns one resultset.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Create Stored Procedure */<br />
</span><span style="color:blue;">CREATE PROCEDURE </span><span style="color:black;">TestSP<br />
</span><span style="color:blue;">AS<br />
SELECT </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">() </span><span style="color:blue;">AS </span><span style="color:black;">MyDate</span><span style="color:gray;">, </span><span style="color:black;">1 </span><span style="color:blue;">AS </span><span style="color:black;">IntValue<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">()+</span><span style="color:black;">1 </span><span style="color:blue;">AS </span><span style="color:black;">MyDate</span><span style="color:gray;">, </span><span style="color:black;">2 </span><span style="color:blue;">AS </span><span style="color:black;">IntValue<br />
GO</span></code></p>
<p style="text-align:justify;"><strong>Traditional Method:<br />
</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Create TempTable */<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:#434343;">#tempTable </span><span style="color:gray;">(</span><span style="color:black;">MyDate SMALLDATETIME</span><span style="color:gray;">, </span><span style="color:black;">IntValue </span><span style="color:blue;">INT</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">/* Run SP and Insert Value in TempTable */<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">#tempTable </span><span style="color:gray;">(</span><span style="color:black;">MyDate</span><span style="color:gray;">, </span><span style="color:black;">IntValue</span><span style="color:gray;">)<br />
</span><span style="color:blue;">EXEC </span><span style="color:black;">TestSP<br />
GO<br />
</span><span style="color:green;">/* SELECT from TempTable */<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">#tempTable<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">/* Clean up */<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:#434343;">#tempTable<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;"><strong>Alternate Method: Table Valued Function<br />
</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Create table valued function*/<br />
</span><span style="color:blue;">CREATE FUNCTION </span><span style="color:black;">dbo.TestFn</span><span style="color:gray;">()<br />
</span><span style="color:blue;">RETURNS </span><span style="color:#434343;">@retTestFn </span><span style="color:blue;">TABLE<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">MyDate SMALLDATETIME</span><span style="color:gray;">,<br />
</span><span style="color:black;">IntValue </span><span style="color:blue;">INT<br />
</span><span style="color:gray;">)<br />
</span><span style="color:blue;">AS<br />
BEGIN<br />
DECLARE </span><span style="color:#434343;">@MyDate </span><span style="color:black;">SMALLDATETIME<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@IntValue </span><span style="color:blue;">INT<br />
INSERT INTO </span><span style="color:#434343;">@retTestFn<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">() </span><span style="color:blue;">AS </span><span style="color:black;">MyDate</span><span style="color:gray;">, </span><span style="color:black;">1 </span><span style="color:blue;">AS </span><span style="color:black;">IntValue<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">()+</span><span style="color:black;">1 </span><span style="color:blue;">AS </span><span style="color:black;">MyDate</span><span style="color:gray;">, </span><span style="color:black;">2 </span><span style="color:blue;">AS </span><span style="color:black;">IntValue<br />
</span><span style="color:blue;">RETURN</span><span style="color:gray;">;<br />
</span><span style="color:blue;">END<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">/* Select data from Table Valued Function */<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.TestFn</span><span style="color:gray;">()<br />
</span><span style="color:black;">GO</span></code>
</p>
<p style="text-align:justify;">It is clear from the resultset that option 2, where I have converted stored procedures logic into the table valued function, is much better in terms of logic as it saves a large number of operations. However, this option should be used carefully. Performance of the stored procedure is “usually” better than that of functions.</p>
<p style="text-align:justify;">We will discuss in another post regarding the type of stored procedure that can be converted into a table valued function. Let me know what you all think about this post.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Posted in Pinal Dave, SQL, SQL Authority, SQL Function, SQL Query, SQL Scripts, SQL Server, SQL Stored Procedure, SQL Tips and Tricks, SQLServer, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/6791/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/6791/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/6791/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/6791/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/6791/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/6791/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/6791/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/6791/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/6791/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/6791/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/6791/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/6791/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/6791/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/6791/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=6791&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/09/23/sql-server-insert-values-of-stored-procedure-in-table-use-table-valued-function/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Converting Stored Procedure into Table Valued Function</title>
		<link>http://blog.sqlauthority.com/2009/09/17/sql-server-converting-stored-procedure-into-table-valued-function/</link>
		<comments>http://blog.sqlauthority.com/2009/09/17/sql-server-converting-stored-procedure-into-table-valued-function/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 01:30:27 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6798</guid>
		<description><![CDATA[In one of my recent articles, I mentioned the use of Table Valued Function (TVF) instead of Stored Procedure (SP). I received a follow up email asking what type of SP can be converted into a TVF. This is indeed a very interesting question! In fact, not all the SPs qualify to be converted to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=6798&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">In one of my recent articles, I mentioned the use of Table Valued Function (TVF) instead of Stored Procedure (SP). I received a follow up email asking what type of SP can be converted into a TVF. This is indeed a very interesting question! In fact, not all the SPs qualify to be converted to a TVF.</p>
<p style="text-align:justify;">Please note that I am not encouraging to convert all the SPs to TVFs. Each SPs have their own usage and need. Here, I shall discuss about the type of SP that can be converted to a TVF.</p>
<p style="text-align:justify;">First of all, you need to convert the SP that you are using to retrieve data and to insert it into any other table. Please read my original post wherein I have explained the scenario for the same. If you do not have to insert the data from the SP into another table, then there is no need to convert the SP to a TVF. This conversion will not necessarily enhance the performance.</p>
<p style="text-align:justify;">In general, SP that returns only one resultset qualifies for conversion to TVF. If there are more than one resultset, then the SP cannot be converted to TVF as the TVF always returns one resultset. Moreover, if the SP has DML statements such as DELETE, UPDATE, INSERT or MERGE, it cannot be converted to a TVF. The usage of TVF is limited to the SELECT statement only. If the SP must be executed by using EXEC, it cannot be converted to TVF.</p>
<p style="text-align:justify;">Do you know any other reason why a SP needs to be converted to a TVF?</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.sqlauthority.com</strong></a><strong>)</strong></p>
<br />Posted in Pinal Dave, SQL, SQL Authority, SQL Function, SQL Optimization, SQL Performance, SQL Query, SQL Server, SQL Stored Procedure, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/6798/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/6798/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/6798/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/6798/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/6798/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/6798/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/6798/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/6798/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/6798/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/6798/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/6798/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/6798/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/6798/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/6798/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=6798&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/09/17/sql-server-converting-stored-procedure-into-table-valued-function/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Plan Caching and Schema Change &#8211; An Interesting Observation</title>
		<link>http://blog.sqlauthority.com/2009/09/14/sql-server-plan-caching-and-schema-change-an-interesting-observation/</link>
		<comments>http://blog.sqlauthority.com/2009/09/14/sql-server-plan-caching-and-schema-change-an-interesting-observation/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 01:30:10 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[Readers Question]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Documentation]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Management Studio]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL Utility]]></category>
		<category><![CDATA[SQL White Papers]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[SQL Cache]]></category>
		<category><![CDATA[SQL Profiler]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6756</guid>
		<description><![CDATA[Last week, I had published details regarding SQL SERVER – Plan Caching in SQL Server 2008 by Greg Low on this blog. Similar to any other white paper, I have read this paper very carefully and enjoyed reading it. One particular topic in the white paper that caught my attention is definition of schema change. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=6756&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Last week, I had published details regarding <a href="http://blog.sqlauthority.com/2009/08/29/sql-server-plan-caching-in-sql-server-2008-by-greg-low/" target="_blank">SQL SERVER – Plan Caching in SQL Server 2008 by Greg Low</a> on this blog. Similar to any other white paper, I have read this paper very carefully and enjoyed reading it. One particular topic in the white paper that caught my attention is definition of schema change. I was well aware of this definition, but I have often found that users are not familiar with what exactly does a schema change mean.</p>
<p style="text-align:justify;">Many people assume that a change in the table structure is schema change. In fact, creating or dropping index on any table also qualifies as schema change.</p>
<p style="text-align:justify;">As per the white paper, “Schema change” is defined as follows:</p>
<ul style="text-align:justify;">
<li>Adding      or dropping columns to a table or view.</li>
<li>Adding      or dropping constraints, defaults, or rules to/from a table.</li>
<li><em>Adding an index to a table or an indexed view.</em></li>
<li><em>Dropping an index defined on a table or an indexed view (only      if the index is used by the query plan in question).</em></li>
<li>Dropping      a statistic (not creating or updating!) defined on a table will cause a      correctness-related recompilation of any query plans that use that table.      Such recompilations occur at that instant when the query plan in question      begins execution. Updating a statistic (both manual and auto-update) will      cause an optimality-related (data related) recompilation of any query      plans that uses this statistic.</li>
</ul>
<p style="text-align:justify;">As per the above definition, I decided to experiment with schema change. In white paper, it was mentioned that schema change will recompile the execution batch. I decided to test this out. I created a small script (given below) and decided to test it.</p>
<p style="text-align:justify;">As per the script, I created a large table and stored the procedure that was accessing that table. I created another object that was an index to the table; when SP was ran with index on table, it showed a plan different from that without the index on table. It was evident that the batch was recompiled when the index on the table was modified.</p>
<p style="text-align:justify;"><a href="http://www.pinaldave.com/sql-download/scripts/planchange.zip" target="_blank">The script is available here to download.</a></p>
<p style="text-align:justify;"><strong>Without Index execution plan:</strong></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/exindex.jpg" alt="" width="500" height="140" /></p>
<p style="text-align:justify;"><strong>With Index execution plan:</strong></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/exnoindex.jpg" alt="" width="502" height="149" /></p>
<p style="text-align:justify;">
<p>It is clear from the execution plan that it is different, and this proves the point that schema change recompiles the execution plan.</p>
<p>There is one more script provided in the white paper, which gets data from DMV related to cached plan. When I ran the following SP, it did not give me very interesting results.</p>
<p><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">cp.objtype </span><span style="color:blue;">AS </span><span style="color:black;">PlanType</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">OBJECT_NAME</span><span style="color:gray;">(</span><span style="color:black;">st.objectid</span><span style="color:gray;">,</span><span style="color:black;">st.dbid</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">ObjectName</span><span style="color:gray;">,<br />
</span><span style="color:black;">cp.refcounts </span><span style="color:blue;">AS </span><span style="color:black;">ReferenceCounts</span><span style="color:gray;">,<br />
</span><span style="color:black;">cp.usecounts </span><span style="color:blue;">AS </span><span style="color:black;">UseCounts</span><span style="color:gray;">,<br />
</span><span style="color:black;">st.</span><span style="color:blue;">TEXT AS </span><span style="color:black;">SQLBatch</span><span style="color:gray;">,<br />
</span><span style="color:black;">qp.query_plan </span><span style="color:blue;">AS </span><span style="color:black;">QueryPlan<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.dm_exec_cached_plans </span><span style="color:blue;">AS </span><span style="color:black;">cp<br />
</span><span style="color:gray;">CROSS </span><span style="color:black;">APPLY sys.dm_exec_query_plan</span><span style="color:gray;">(</span><span style="color:black;">cp.plan_handle</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">qp<br />
</span><span style="color:gray;">CROSS </span><span style="color:black;">APPLY sys.dm_exec_sql_text</span><span style="color:gray;">(</span><span style="color:black;">cp.plan_handle</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">st</span><span style="color:gray;">;</span></code></p>
<p><span style="color:gray;"><img class="alignnone" src="http://www.pinaldave.com/bimg/exusecount.jpg" alt="" width="499" height="168" /><br />
</span></p>
<p>When I ran the above script, I found that the UseCount column did not reset when QueryPlan is changed. This was very surprising as I was expecting UseCount to reset on changing the query plan. I sent an email to <a href="http://www.solidq.com.au/" target="_blank">Dr. Greg Low</a>, who is author of this white paper. He was very kind to respond back within minutes with a proper answer, which is as follows:</p>
<p><strong><em>You’ll notice in Profiler when you run the sproc after creating the index that it does in fact cause a recompile. However, in SQL Server 2008 (and 2005), these are statement level recompiles not proc level recompiles. I imagine that’s why they’re not reflected in the UseCount values ie: the plan itself isn’t recompiled and that’s what the counts are based on.</em></strong></p>
<p><img class="alignnone" src="http://www.pinaldave.com/bimg/exprofiler.jpg" alt="" width="560" height="120" /></p>
<p style="text-align:justify;">Well, this simple explanation clarified my doubts. Let me know what you think, and I strongly suggest you all to read the <a href="http://blog.sqlauthority.com/2009/08/29/sql-server-plan-caching-in-sql-server-2008-by-greg-low/" target="_blank">white paper</a> written by Dr. Greg Low.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.sqlauthority.com</strong></a><strong>)</strong></p>
<br />Posted in Pinal Dave, Readers Question, SQL, SQL Authority, SQL Documentation, SQL Query, SQL Scripts, SQL Server, SQL Server Management Studio, SQL Stored Procedure, SQL Tips and Tricks, SQL Utility, SQL White Papers, T SQL, Technology Tagged: SQL Cache, SQL Profiler <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/6756/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/6756/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/6756/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/6756/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/6756/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/6756/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/6756/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/6756/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/6756/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/6756/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/6756/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/6756/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/6756/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/6756/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=6756&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/09/14/sql-server-plan-caching-and-schema-change-an-interesting-observation/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/exindex.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/exnoindex.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/exusecount.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/exprofiler.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Get a List of Fixed Hard Drive and Free Space on Server</title>
		<link>http://blog.sqlauthority.com/2009/08/05/sql-server-get-a-list-of-fixed-hard-drive-and-free-space-on-server/</link>
		<comments>http://blog.sqlauthority.com/2009/08/05/sql-server-get-a-list-of-fixed-hard-drive-and-free-space-on-server/#comments</comments>
		<pubDate>Wed, 05 Aug 2009 01:30:46 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[undocumented SQL]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6193</guid>
		<description><![CDATA[When I am not blogging, I am typically working on SQL Server Optimization projects. Time and again, I only have access to SQL Server Management Studio that I can remotely connect to server but do not have access to Operating System, and it works just fine. At one point in optimization project, I have to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=6193&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">When I am not blogging, I am typically working on SQL Server Optimization projects. Time and again, I only have access to SQL Server Management Studio that I can remotely connect to server but do not have access to Operating System, and it works just fine.</p>
<p style="text-align:justify;">At one point in optimization project, I have to decide on index filegroup placement as well TempDB files (.ldf and .mdf) placement. It is commonly known that system gives enhanced performance when index and tempdb are on separate drives than where the main database is placed.</p>
<p style="text-align:justify;">As I do not have access to OS I use the following Stored Procedure to figure out the number of fixed drives (hard drive) a system has along with free space on each of those drives. Based on this result, I decide my strategy of where to put my indexes and tempdb.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">EXEC </span><span style="color:black;">master..</span><span style="color:darkred;">xp_fixeddrives</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/hddrive.jpg" alt="" width="299" height="179" /></p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.sqlauthority.com</a>)</strong></p>
<br />Posted in SQL, SQL Authority, SQL Query, SQL Server, SQL Stored Procedure, SQL Tips and Tricks, T SQL, Technology Tagged: undocumented SQL <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/6193/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/6193/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/6193/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/6193/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/6193/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/6193/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/6193/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/6193/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/6193/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/6193/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/6193/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/6193/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/6193/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/6193/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=6193&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/08/05/sql-server-get-a-list-of-fixed-hard-drive-and-free-space-on-server/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/hddrive.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; List All Missing Identity Values of Table in Database</title>
		<link>http://blog.sqlauthority.com/2009/07/27/sql-server-list-all-missing-identity-values-of-table-in-database/</link>
		<comments>http://blog.sqlauthority.com/2009/07/27/sql-server-list-all-missing-identity-values-of-table-in-database/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 01:30:11 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[Readers Contribution]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Identity]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6339</guid>
		<description><![CDATA[The best part of any blog is when readers ask each other questions. Better still, is when a reader takes the time to provide a detailed response. A few days ago, one of my readers, Yasmin, asked a very interesting question: How we can find the list of tables whose identity was missed (not is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=6339&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">The best part of any blog is when readers ask each other questions. Better still, is when a reader takes the time to provide a detailed response.</p>
<p style="text-align:justify;">A few days ago, one of my readers, <a href="http://blog.sqlauthority.com/2009/04/01/sql-server-reseed-identity-of-table-table-missing-identity-values-gap-in-identity-column/#comment-53353" target="_blank">Yasmin, </a>asked a very interesting question:</p>
<p style="text-align:justify;"><strong><em>How we can find the list of tables whose identity was missed (not is sequential order) within the entire database?</em></strong></p>
<p style="text-align:justify;">A big thank you to SQL Server Expert, <strong>Imran Mohammed</strong>, for his excellent response to this question. He also provided an extremely impressive script, which is well described and contains inline comments.</p>
<p style="text-align:justify;">This script is well worth keeping in your archive for future reference.</p>
<p style="text-align:justify;"><a href="http://www.pinaldave.com/best-sql-server-download.cfm?download=Get_Missing_Identity_Value_Information" target="_blank"><strong>Download Script List All Missing Identity Values of Table in Database</strong></a></p>
<p style="text-align:justify;">If you and use the script, please comment here and let me know about your experience.</p>
<p style="text-align:justify;">If you know any other way that this can be accomplished or have a different solution, please post your comment here, or send me an email and I will publish it with due credit.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<br />Posted in Pinal Dave, Readers Contribution, SQL, SQL Authority, SQL Function, SQL Query, SQL Scripts, SQL Server, SQL Stored Procedure, SQL Tips and Tricks, T SQL, Technology Tagged: Identity <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/6339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/6339/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/6339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/6339/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/6339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/6339/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/6339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/6339/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/6339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/6339/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/6339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/6339/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/6339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/6339/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=6339&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/07/27/sql-server-list-all-missing-identity-values-of-table-in-database/feed/</wfw:commentRss>
		<slash:comments>37</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; List All the Tables for All Databases Using System Tables</title>
		<link>http://blog.sqlauthority.com/2009/04/26/sql-server-list-all-the-tables-for-all-databases-using-system-tables/</link>
		<comments>http://blog.sqlauthority.com/2009/04/26/sql-server-list-all-the-tables-for-all-databases-using-system-tables/#comments</comments>
		<pubDate>Sun, 26 Apr 2009 01:30:07 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[Readers Question]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL System Table]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[undocumented SQL]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4540</guid>
		<description><![CDATA[Today we will go over very simple script which will list all the tables for all the database. sp_msforeachdb 'select "?" AS db, * from [?].sys.tables' Update: Based on comments received below I have updated this article. Thank you to all the readers. This is good example where something small like this have good participation from readers. Reference : Pinal Dave [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=4540&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Today we will go over very simple script which will list all the tables for all the database.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:darkred;">sp_msforeachdb </span><span style="color:red;">'select "?" AS db, * from [?].sys.tables'</span></code></p>
<p style="text-align:justify;"><strong>Update: </strong>Based on comments received below I have updated this article. Thank you to all the readers. This is good example where something small like this have good participation from readers.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://www.sqlauthority.com/" target="_blank">http://www.SQLAuthority.com</a>)</strong></p>
<br />Posted in Pinal Dave, Readers Question, SQL, SQL Authority, SQL Query, SQL Scripts, SQL Server, SQL Stored Procedure, SQL System Table, SQL Tips and Tricks, SQLServer, T SQL, Technology Tagged: undocumented SQL <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/4540/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/4540/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/4540/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/4540/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/4540/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/4540/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/4540/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/4540/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/4540/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/4540/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/4540/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/4540/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/4540/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/4540/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=4540&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/04/26/sql-server-list-all-the-tables-for-all-databases-using-system-tables/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; 2008 &#8211; Optimize for Ad hoc Workloads &#8211; Advance Performance Optimization</title>
		<link>http://blog.sqlauthority.com/2009/03/21/sql-server-2008-optimize-for-ad-hoc-workloads-advance-performance-optimization/</link>
		<comments>http://blog.sqlauthority.com/2009/03/21/sql-server-2008-optimize-for-ad-hoc-workloads-advance-performance-optimization/#comments</comments>
		<pubDate>Sat, 21 Mar 2009 01:30:44 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL System Table]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=3819</guid>
		<description><![CDATA[Every batch (T-SQL, SP etc) when ran creates execution plan which is stored in system for re-use. Due to this reason large number of query plans are stored in system. However, there are plenty of plans which are only used once and have never re-used again. One time ran batch plans wastes memory and resources. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=3819&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Every batch (T-SQL, SP etc) when ran creates execution plan which is stored in system for re-use. Due to this reason large number of query plans are stored in system. However, there are plenty of plans which are only used once and have never re-used again. One time ran batch plans wastes memory and resources.</p>
<p style="text-align:justify;">SQL Server 2008 has feature of optimizing ad hoc workloads. Before we move to it, let us understand the behavior of SQL Server without optimizing ad hoc workload.</p>
<p style="text-align:justify;">Please run following script for testing. Make sure to not to run whole batch together. Just run each command separately to really see the effect of subject of article.</p>
<p style="text-align:justify;"><a href="http://www.pinaldave.com/bimg/CachePlan.zip" target="_blank"><strong>Download complete script of this article here.</strong></a></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Test 0 */<br />
/* Clean Cache and Buffers */<br />
</span><span style="color:blue;">DBCC </span><span style="color:black;">FREEPROCCACHE<br />
</span><span style="color:blue;">DBCC </span><span style="color:black;">DROPCLEANBUFFERS<br />
GO<br />
</span><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:green;">/* Run Adhoc Query First Time */<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">* </span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Shift<br />
GO<br />
</span><span style="color:green;">/* Check if Adhoc query is cached.<br />
It will return one result */<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">usecounts</span><span style="color:gray;">, </span><span style="color:black;">cacheobjtype</span><span style="color:gray;">, </span><span style="color:black;">objtype</span><span style="color:gray;">, </span><span style="color:blue;">TEXT<br />
FROM </span><span style="color:black;">sys.dm_exec_cached_plans<br />
</span><span style="color:gray;">CROSS </span><span style="color:black;">APPLY sys.dm_exec_sql_text</span><span style="color:gray;">(</span><span style="color:black;">plan_handle</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">usecounts </span><span style="color:gray;">&gt; </span><span style="color:black;">0 </span><span style="color:gray;">AND<br />
</span><span style="color:blue;">TEXT </span><span style="color:gray;">LIKE </span><span style="color:red;">'%SELECT * FROM HumanResources.Shift%'<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">usecounts </span><span style="color:blue;">DESC</span><span style="color:gray;">;<br />
</span><span style="color:black;">GO</span></code>
</p>
<p style="text-align:justify;">Now let us check result of this script. It is clear from result that when we ran query once it cached its plan in memory. If we never run this again in future or if we have just ran as part of building longer query the cache plan of this query is just waste of memory.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/cachetest0.jpg" alt="" width="553" height="516" /></p>
<p style="text-align:justify;">Let us now enable the option of optimizing ad hoc workload. This feature is available in all the versions of SQL Server 2008.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:black;"> </span><span style="color:darkred;">sp_CONFIGURE </span><span style="color:red;">'show advanced options'</span><span style="color:gray;">,</span><span style="color:black;">1<br />
</span><span style="color:blue;">RECONFIGURE<br />
</span><span style="color:black;">GO</span></code></p>
<p><span style="color:darkred;">sp_CONFIGURE </span><span style="color:red;">&#8216;optimize for ad hoc workloads&#8217;</span><span style="color:gray;">,</span><span style="color:black;">1<br />
</span><span style="color:blue;">RECONFIGURE<br />
</span><span style="color:black;">GO</span>
</p>
<p style="text-align:justify;">We will now run the code for Test 1 which is almost same as Test 0. Make sure to clean the cache and buffer before running the query to create real life scenario of live case.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Test 1 */<br />
/* Clean Cache and Buffers */<br />
</span><span style="color:blue;">DBCC </span><span style="color:black;">FREEPROCCACHE<br />
</span><span style="color:blue;">DBCC </span><span style="color:black;">DROPCLEANBUFFERS<br />
GO<br />
</span><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:green;">/* Run Adhoc Query First Time */<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">* </span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Shift<br />
GO<br />
</span><span style="color:green;">/* Check if Adhoc query is cached.<br />
It will not return any result */<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">usecounts</span><span style="color:gray;">, </span><span style="color:black;">cacheobjtype</span><span style="color:gray;">, </span><span style="color:black;">objtype</span><span style="color:gray;">, </span><span style="color:blue;">TEXT<br />
FROM </span><span style="color:black;">sys.dm_exec_cached_plans<br />
</span><span style="color:gray;">CROSS </span><span style="color:black;">APPLY sys.dm_exec_sql_text</span><span style="color:gray;">(</span><span style="color:black;">plan_handle</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">usecounts </span><span style="color:gray;">&gt; </span><span style="color:black;">0 </span><span style="color:gray;">AND<br />
</span><span style="color:blue;">TEXT </span><span style="color:gray;">LIKE </span><span style="color:red;">'SELECT * FROM HumanResources.Shift%'<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">usecounts </span><span style="color:blue;">DESC</span><span style="color:gray;">;<br />
</span><span style="color:black;">GO </span></code>
</p>
<p style="text-align:justify;">We can clear see now as we have advance option enabled we do not have query cache planed stored in database.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/cachetest1.jpg" alt="" width="556" height="485" /></p>
<p style="text-align:justify;">We are interested to know now that if we run the batch more than 1 time it will cache its execution plan.  With advance option of optimizing ad hoc workload.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Test 2 */<br />
/* Clean Cache and Buffers */<br />
</span><span style="color:blue;">DBCC </span><span style="color:black;">FREEPROCCACHE<br />
</span><span style="color:blue;">DBCC </span><span style="color:black;">DROPCLEANBUFFERS<br />
GO<br />
</span><span style="color:green;">/* Run Adhoc Query two  Time */<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">* </span><span style="color:blue;">FROM </span><span style="color:black;">HumanResources.Shift<br />
GO 5<br />
</span><span style="color:green;">/* Check if Adhoc query is cached.<br />
It will return result with Adhoc Query ran two times*/<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">usecounts</span><span style="color:gray;">, </span><span style="color:black;">cacheobjtype</span><span style="color:gray;">, </span><span style="color:black;">objtype</span><span style="color:gray;">, </span><span style="color:blue;">TEXT<br />
FROM </span><span style="color:black;">sys.dm_exec_cached_plans<br />
</span><span style="color:gray;">CROSS </span><span style="color:black;">APPLY sys.dm_exec_sql_text</span><span style="color:gray;">(</span><span style="color:black;">plan_handle</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">usecounts </span><span style="color:gray;">&gt; </span><span style="color:black;">0 </span><span style="color:gray;">AND<br />
</span><span style="color:blue;">TEXT </span><span style="color:gray;">LIKE </span><span style="color:red;">'%SELECT * FROM HumanResources.Shift%'<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">usecounts </span><span style="color:blue;">DESC</span><span style="color:gray;">;<br />
</span><span style="color:black;">GO</span></code>
</p>
<p style="text-align:justify;">From our image it is quite clear that when the batch is ran for more than 1 time it caches its execution plan. This is generic behavior with or without turning on advance option.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/cachetest2.jpg" alt="" width="561" height="859" /></p>
<p style="text-align:justify;">This may be very simple to see from the top but if you are using SQL Server 2008 and have millions of ad hoc query running every day you wil realize how important this feature is. This feature improves performance by relieving memory pressure by not storing the single time used compiled plans.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.sqlauthority.com</a>)</strong></p>
<br />Posted in Best Practices, Pinal Dave, SQL, SQL Authority, SQL Optimization, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Stored Procedure, SQL System Table, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/3819/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/3819/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/3819/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/3819/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/3819/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/3819/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/3819/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/3819/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/3819/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/3819/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/3819/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/3819/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/3819/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/3819/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=3819&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/03/21/sql-server-2008-optimize-for-ad-hoc-workloads-advance-performance-optimization/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/cachetest0.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/cachetest1.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/cachetest2.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Enable and Disable Index Non Clustered Indexes Using T-SQL</title>
		<link>http://blog.sqlauthority.com/2009/02/19/sql-server-enable-and-disable-index-non-clustered-indexes-using-t-sql/</link>
		<comments>http://blog.sqlauthority.com/2009/02/19/sql-server-enable-and-disable-index-non-clustered-indexes-using-t-sql/#comments</comments>
		<pubDate>Thu, 19 Feb 2009 01:30:27 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[Readers Contribution]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL Utility]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=2710</guid>
		<description><![CDATA[Just a day before I received wonderful T-SQL Script from SQL Server Expert Imran Mohammed. What I am really impressed from this script is additional feature of changing recovery mode when SQL Server is enabling and disabling Index. When index are enabled changing SQL Server Recovery mode to simple makes whole operation faster. This stored [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=2710&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Just a day before I received wonderful T-SQL Script from SQL Server Expert <strong><em>Imran Mohammed</em></strong>. What I am really impressed from this script is additional feature of changing recovery mode when SQL Server is enabling and disabling Index. When index are enabled changing SQL Server Recovery mode to simple makes whole operation faster. This stored procedure is self documented so I am not writing much about it.</p>
<p style="text-align:justify;">I really want to encourage all blog readers and SQL Expert to actively participate like <strong><em>Imran Mohammed.</em></strong></p>
<p style="text-align:justify;">Stored Procedure <strong><a href="http://www.pinaldave.com/bimg/USP_DisableEnableNonClusteredIndexes.zip" target="_blank">USP_DisableEnableNonClusteredIndexes</a></strong> can be downloaded from here.</p>
<p style="text-align:justify;"><strong>Execute SP using following script.</strong></p>
<p><code style="font-size:12px;"><span style="color:green;">-- sample to Execute<br />
</span><span style="color:blue;">EXEC </span><span style="color:black;">USP_DisableEnableNonClusteredIndexes </span><span style="color:red;">'ADVENTUREWORKS'</span><span style="color:gray;">, </span><span style="color:red;">'N'</span><span style="color:gray;">,</span><span style="color:black;">1 </span><span style="color:green;">-- DISABLE<br />
</span><span style="color:blue;">EXEC </span><span style="color:black;">USP_DisableEnableNonClusteredIndexes </span><span style="color:red;">'ADVENTUREWORKS'</span><span style="color:gray;">, </span><span style="color:red;">'N'</span><span style="color:gray;">,</span><span style="color:black;">2 </span><span style="color:green;">-- REBUILD/ENABLE with out Changing Recovery model.<br />
</span><span style="color:blue;">EXEC </span><span style="color:black;">USP_DisableEnableNonClusteredIndexes </span><span style="color:red;">'ADVENTUREWORKS'</span><span style="color:gray;">, </span><span style="color:red;">'Y'</span><span style="color:gray;">,</span><span style="color:black;">2 </span><span style="color:green;">-- REBUILD/ENABLE with Change Recovery model.<br />
-- or<br />
</span><span style="color:blue;">EXEC </span><span style="color:black;">USP_DisableEnableNonClusteredIndexes </span><span style="color:red;">'ADVENTUREWORKS'</span><span style="color:gray;">,NULL, </span><span style="color:black;">1 </span><span style="color:green;">-- DISABLE<br />
</span><span style="color:blue;">EXEC </span><span style="color:black;">USP_DisableEnableNonClusteredIndexes </span><span style="color:red;">'ADVENTUREWORKS'</span><span style="color:gray;">, NULL ,</span><span style="color:black;">2 </span><span style="color:green;">-- REBUILD/ENABLE with out Changing Recovery model. </span></code></p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://blog.SQLAuthority.com" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
<br />Posted in Pinal Dave, Readers Contribution, SQL, SQL Authority, SQL Index, SQL Query, SQL Scripts, SQL Server, SQL Stored Procedure, SQL Tips and Tricks, SQL Utility, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/2710/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/2710/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/2710/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/2710/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/2710/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/2710/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/2710/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/2710/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/2710/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/2710/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/2710/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/2710/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/2710/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/2710/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=2710&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/02/19/sql-server-enable-and-disable-index-non-clustered-indexes-using-t-sql/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; FIX : ERROR : Msg 5834, Level 16, State 1, Line 1 The affinity mask specified conflicts with the IO affinity mask specified. Use the override option to force this configuration</title>
		<link>http://blog.sqlauthority.com/2009/01/31/sql-server-fix-error-msg-5834-level-16-state-1-line-1-the-affinity-mask-specified-conflicts-with-the-io-affinity-mask-specified-use-the-override-option-to-force-this-configuration/</link>
		<comments>http://blog.sqlauthority.com/2009/01/31/sql-server-fix-error-msg-5834-level-16-state-1-line-1-the-affinity-mask-specified-conflicts-with-the-io-affinity-mask-specified-use-the-override-option-to-force-this-configuration/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 01:30:25 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Error Messages]]></category>
		<category><![CDATA[SQL Index]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=2213</guid>
		<description><![CDATA[Yesterday I came across following error while enabling fill factor for my database server, when I was trying to write article SQL SERVER &#8211; 2008 &#8211; 2005 &#8211; Rebuild Every Index of All Tables of Database &#8211; Rebuild Index with FillFactor. I ran following T-SQL script and it gave me error. sp_configure 'show advanced options', [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=2213&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p style="text-align:justify;">Yesterday I came across following error while enabling fill factor for my database server, when I was trying to write article <strong><a href="http://blog.sqlauthority.com/2009/01/30/sql-server-2008-2005-rebuild-every-index-of-all-tables-of-database-rebuild-index-with-fillfactor/" target="_blank">SQL SERVER &#8211; 2008 &#8211; 2005 &#8211; Rebuild Every Index of All Tables of Database &#8211; Rebuild Index with FillFactor</a></strong>.</p>
<p style="text-align:justify;">I ran following T-SQL script and it gave me error.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:darkred;">sp_configure </span><span style="color:red;">'show advanced options'</span><span style="color:gray;">, </span><span style="color:black;">1<br />
GO<br />
</span><span style="color:blue;">RECONFIGURE<br />
</span><span style="color:black;">GO<br />
</span><span style="color:darkred;">sp_configure </span><span style="color:red;">'fill factor'</span><span style="color:gray;">, </span><span style="color:black;">90<br />
GO<br />
</span><span style="color:blue;">RECONFIGURE<br />
</span><span style="color:black;">GO</span></code>
</p>
<p style="text-align:justify;">In result pan following error showed up.</p>
<p style="text-align:justify;"><span style="font-size:8pt;color:red;font-family:'Courier New';">Msg 5834, Level 16, State 1, Line 1<br />
</span><span style="font-size:8pt;color:red;line-height:115%;font-family:'Courier New';">The affinity mask specified conflicts with the IO affinity mask specified. Use the override option to force this configuration.</span>
</p>
<p style="text-align:justify;"><strong>Fix/Solution/Workaround:</strong></p>
<p style="text-align:justify;">Add option OVERRIDE along with RECONFIGURE command.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:darkred;">sp_configure </span><span style="color:red;">'show advanced options'</span><span style="color:gray;">, </span><span style="color:black;">1<br />
GO<br />
</span><span style="color:blue;">RECONFIGURE WITH OVERRIDE<br />
</span><span style="color:black;">GO<br />
</span><span style="color:darkred;">sp_configure </span><span style="color:red;">'fill factor'</span><span style="color:gray;">, </span><span style="color:black;">90<br />
GO<br />
</span><span style="color:blue;">RECONFIGURE</span></code><code style="font-size:12px;"><span style="color:blue;"> WITH OVERRIDE</span></code><br />
<code style="font-size:12px;"><span style="color:black;">GO</span></code>
</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://blog.SQLAuthority.com" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
<p style="text-align:justify;">
<br />Posted in Pinal Dave, SQL, SQL Authority, SQL Error Messages, SQL Index, SQL Optimization, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Stored Procedure, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/2213/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/2213/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/2213/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/2213/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/sqlauthority.wordpress.com/2213/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/sqlauthority.wordpress.com/2213/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/sqlauthority.wordpress.com/2213/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/sqlauthority.wordpress.com/2213/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/2213/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/2213/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/2213/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/2213/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/2213/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/2213/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&amp;blog=668536&amp;post=2213&amp;subd=sqlauthority&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/01/31/sql-server-fix-error-msg-5834-level-16-state-1-line-1-the-affinity-mask-specified-conflicts-with-the-io-affinity-mask-specified-use-the-override-option-to-force-this-configuration/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
	</channel>
</rss>
