<?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>Journey to SQL Authority with Pinal Dave &#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>Notes of a SQL Server MVP and Database Administrator</description>
	<lastBuildDate>Sat, 21 Nov 2009 01:30:19 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='blog.sqlauthority.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/08e35387c05b61340e885b1763a69d9f?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Journey to SQL Authority with Pinal Dave &#187; SQL Stored Procedure</title>
		<link>http://blog.sqlauthority.com</link>
	</image>
			<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 we look at this carefully, a total [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7388&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><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>
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/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&blog=668536&post=7388&subd=sqlauthority&ref=&feed=1" /></div>]]></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>7</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 should not [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7164&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><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>
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/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&blog=668536&post=7164&subd=sqlauthority&ref=&feed=1" /></div>]]></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>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/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 one [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6791&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><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>
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/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&blog=668536&post=6791&subd=sqlauthority&ref=&feed=1" /></div>]]></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>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; 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&blog=668536&post=6798&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><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>
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/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&blog=668536&post=6798&subd=sqlauthority&ref=&feed=1" /></div>]]></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>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; 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&blog=668536&post=6756&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><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>
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/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&blog=668536&post=6756&subd=sqlauthority&ref=&feed=1" /></div>]]></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>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>

		<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 decide [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6193&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><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>
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/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&blog=668536&post=6193&subd=sqlauthority&ref=&feed=1" /></div>]]></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>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/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 sequential order) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6339&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><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>
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/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&blog=668536&post=6339&subd=sqlauthority&ref=&feed=1" /></div>]]></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>28</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 (http://www.SQLAuthority.com)
Posted in Pinal [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=4540&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><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>
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/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&blog=668536&post=4540&subd=sqlauthority&ref=&feed=1" /></div>]]></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>7</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.
SQL [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=3819&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><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>
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/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&blog=668536&post=3819&subd=sqlauthority&ref=&feed=1" /></div>]]></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>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>

		<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&blog=668536&post=2710&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><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>
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/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&blog=668536&post=2710&subd=sqlauthority&ref=&feed=1" /></div>]]></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>4</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', 1
GO
RECONFIGURE
GO
sp_configure 'fill [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=2213&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><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;">
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/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&blog=668536&post=2213&subd=sqlauthority&ref=&feed=1" /></div>]]></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>
		<item>
		<title>SQL SERVER &#8211; Find Number of Rows and Disk Space Reserved &#8211; Using sp_spaceused Interesting Observation</title>
		<link>http://blog.sqlauthority.com/2009/01/14/sql-server-find-number-of-rows-and-disk-space-reserved-using-sp_spaceused-interesting-observation/</link>
		<comments>http://blog.sqlauthority.com/2009/01/14/sql-server-find-number-of-rows-and-disk-space-reserved-using-sp_spaceused-interesting-observation/#comments</comments>
		<pubDate>Wed, 14 Jan 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 Data Storage]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server DBCC]]></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=2016</guid>
		<description><![CDATA[Previously I posted SQL SERVER &#8211; Find Row Count in Table &#8211; Find Largest Table in Database &#8211; T-SQL. Today we will look into the same issue but with some additional interesting detail.
We can find the row count using another system SP sp_spaceused. This SP gives additional information regarding disk space reserved on database as [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=2016&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Previously I posted <strong><a href="http://blog.sqlauthority.com/2008/12/09/sql-server-find-table-rowcount-without-using-t-sql-and-without-opening-table/" target="_blank">SQL SERVER &#8211; Find Row Count in Table &#8211; Find Largest Table in Database &#8211; T-SQL</a></strong>. Today we will look into the same issue but with some additional interesting detail.<br />
We can find the row count using another system SP sp_spaceused. This SP gives additional information regarding disk space reserved on database as well. Well, when I ran the SP on AdventureWorks first time, I suspected that database SP is not providing me correct results.
</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/spaceused.jpg" alt="" width="330" height="257" /></p>
<p style="text-align:justify;">After a bit investigating I found that it may be possible that due to any reason may be the usage on AdventureWorks database might not be updated. I ran same SP with additional param as shown below and it provided me updated information for the same. Once I ran the SP with @updateusage param, it updated the status of the database. When I ran it again next time without param it gave the same results.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/spaceused1.jpg" alt="" width="367" height="244" /></p>
<p style="text-align:justify;">The reason for above behavior is when dellocation of space happened in database (e.g. dropping table or truncating table) it does not release the used space right away. However, when @updateusage param is used database engine scans whole database and update the sapce usage in respective tables.<br />
Let me know if you guys have any interesting observation like this.
</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>
Posted in Pinal Dave, SQL, SQL Authority, SQL Data Storage, SQL Function, SQL Query, SQL Scripts, SQL Server, SQL Server DBCC, 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/2016/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/2016/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/2016/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/2016/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/2016/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/2016/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/2016/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/2016/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/2016/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/2016/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=2016&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/01/14/sql-server-find-number-of-rows-and-disk-space-reserved-using-sp_spaceused-interesting-observation/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>

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

		<media:content url="http://www.pinaldave.com/bimg/spaceused1.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; 2008 &#8211; 2005 &#8211; Find Longest Running Query &#8211; TSQL</title>
		<link>http://blog.sqlauthority.com/2009/01/02/sql-server-2008-2005-find-longest-running-query-tsql/</link>
		<comments>http://blog.sqlauthority.com/2009/01/02/sql-server-2008-2005-find-longest-running-query-tsql/#comments</comments>
		<pubDate>Fri, 02 Jan 2009 01:30:53 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<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 Server DBCC]]></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=1941</guid>
		<description><![CDATA[UPDATE : Updated this query with bug fixed with one more enhancement SERVER &#8211; 2008 &#8211; 2005 &#8211; Find Longest Running Query &#8211; TSQL &#8211; Part 2.
Recently my company owner asked me to find which query is running longest. It was very interesting that I was not able to find any T-SQL script online which [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1941&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;"><em><strong>UPDATE : </strong></em>Updated this query with bug fixed with one more enhancement <strong><a href="http://blog.sqlauthority.com/2009/01/23/sql-server-2008-2005-find-longest-running-query-tsql-part-2/" target="_blank">SERVER &#8211; 2008 &#8211; 2005 &#8211; Find Longest Running Query &#8211; TSQL &#8211; Part 2</a></strong>.</p>
<p style="text-align:justify;">Recently my company owner asked me to find which query is running longest. It was very interesting that I was not able to find any T-SQL script online which can give me this data directly. Finally, I wrote down very quick script which gives me T-SQL which has ran on server along with average time and maximum time of that T-SQL execution. As I keep on writing I needed to know when exactly logging was started for the same T-SQL so I had added Logging start time in the query as well.</p>
<p style="text-align:justify;">The reason I started to use this script to find out longest running query as if query is changed a bit it will display it as new row and new log start time. This way when I am improving T-SQL query or Stored Procedure I can check their progress in the query and does not have to get bothered with previous data.</p>
<p style="text-align:justify;">I always run following DBCC command before I started to use my query. Following DBCC commands clears the cache of the server and starts fresh logging of the query running time.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">DBCC </span><span style="color:black;">FREEPROCCACHE</span></code></p>
<p style="text-align:justify;">Run following query to find longest running query using T-SQL.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT DISTINCT TOP </span><span style="color:black;">10<br />
t.</span><span style="color:blue;">TEXT </span><span style="color:black;">QueryName</span><span style="color:gray;">,<br />
</span><span style="color:black;">s.execution_count </span><span style="color:blue;">AS </span><span style="color:black;">ExecutionCount</span><span style="color:gray;">,<br />
</span><span style="color:black;">s.max_elapsed_time </span><span style="color:blue;">AS </span><span style="color:black;">MaxElapsedTime</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">ISNULL</span><span style="color:gray;">(</span><span style="color:black;">s.total_elapsed_time </span><span style="color:gray;">/ </span><span style="color:black;">s.execution_count</span><span style="color:gray;">, </span><span style="color:black;">0</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">AvgElapsedTime</span><span style="color:gray;">,<br />
</span><span style="color:black;">s.creation_time </span><span style="color:blue;">AS </span><span style="color:black;">LogCreatedOn</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">ISNULL</span><span style="color:gray;">(</span><span style="color:black;">s.execution_count </span><span style="color:gray;">/ </span><span style="color:magenta;">DATEDIFF</span><span style="color:gray;">(</span><span style="color:black;">s</span><span style="color:gray;">, </span><span style="color:black;">s.creation_time</span><span style="color:gray;">, </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">()), </span><span style="color:black;">0</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">FrequencyPerSec<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.dm_exec_query_stats s<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;">s.sql_handle </span><span style="color:gray;">) </span><span style="color:black;">t<br />
</span><span style="color:blue;">ORDER BY<br />
</span><span style="color:black;">s.max_elapsed_time </span><span style="color:blue;">DESC<br />
</span><span style="color:black;">GO</span></code>
</p>
<p style="text-align:justify;">You can also add WHERE clause to above T-SQL query and filter additionally.</p>
<p style="text-align:justify;">If you have not ran query like this previously on your server I strongly recommend to run this. I bet you will find surprising results.</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;">
Posted in Pinal Dave, SQL, SQL Authority, SQL Optimization, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Server DBCC, SQL Stored Procedure, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/1941/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1941/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1941/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1941/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1941/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1941/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1941/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1941/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1941/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1941/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1941&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/01/02/sql-server-2008-2005-find-longest-running-query-tsql/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; Simple Use of Cursor to Print All Stored Procedures of Database Including Schema</title>
		<link>http://blog.sqlauthority.com/2008/11/22/sql-server-simple-use-of-cursor-to-print-all-stored-procedures-of-database-including-schema/</link>
		<comments>http://blog.sqlauthority.com/2008/11/22/sql-server-simple-use-of-cursor-to-print-all-stored-procedures-of-database-including-schema/#comments</comments>
		<pubDate>Sat, 22 Nov 2008 01:30:07 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Pinal Dave]]></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 Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1550</guid>
		<description><![CDATA[I love active participation from my readers. Just a day ago I wrote article about SQL SERVER &#8211; Simple Use of Cursor to Print All Stored Procedures of Database. I just received comment from Jerry Hung who have improved on previously written article of generating text of Stored Procedure.
DECLARE @procName VARCHAR(100)
DECLARE @getprocName CURSOR
SET @getprocName = CURSOR FOR
SELECT [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1550&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">I love active participation from my readers. Just a day ago I wrote article about <strong><a href="http://blog.sqlauthority.com/2008/11/20/sql-server-simple-use-of-cursor-to-print-all-stored-procedures-of-database/" target="_blank">SQL SERVER &#8211; Simple Use of Cursor to Print All Stored Procedures of Database</a></strong>. I just received comment from <a href="http://sqlauthority.wordpress.com/wp-admin/comment.php?action=editcomment&amp;c=44313" target="_blank">Jerry Hung</a> who have improved on previously written article of generating text of Stored Procedure.</p>
<p><code style="font-size:12px;"><span style="color:blue;">DECLARE </span><span style="color:#434343;">@procName </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;">@getprocName </span><span style="color:blue;">CURSOR<br />
SET </span><span style="color:#434343;">@getprocName </span><span style="color:blue;">= CURSOR FOR<br />
SELECT </span><span style="color:black;">Name </span><span style="color:blue;">= </span><span style="color:red;">'[' </span><span style="color:gray;">+ </span><span style="color:black;">SCHEMA_NAME</span><span style="color:gray;">(</span><span style="color:black;">SCHEMA_ID</span><span style="color:gray;">) + </span><span style="color:red;">'].[' </span><span style="color:gray;">+ </span><span style="color:black;">Name </span><span style="color:gray;">+ </span><span style="color:red;">']'<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.all_objects </span><span style="color:blue;">WHERE </span><span style="color:black;">TYPE </span><span style="color:blue;">= </span><span style="color:red;">'P'<br />
</span><span style="color:gray;">AND </span><span style="color:black;">is_ms_shipped 1<br />
</span><span style="color:blue;">OPEN </span><span style="color:#434343;">@getprocName<br />
</span><span style="color:blue;">FETCH </span><span style="color:black;">NEXT<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">@getprocName </span><span style="color:blue;">INTO </span><span style="color:#434343;">@procName<br />
</span><span style="color:blue;">WHILE </span><span style="color:#434343;">@@FETCH_STATUS </span><span style="color:blue;">= </span><span style="color:black;">0<br />
</span><span style="color:blue;">BEGIN<br />
PRINT </span><span style="color:red;">'sp_HelpText ' </span><span style="color:gray;">+ </span><span style="color:#434343;">@procName<br />
</span><span style="color:blue;">EXEC </span><span style="color:darkred;">sp_HelpText </span><span style="color:#434343;">@procName<br />
</span><span style="color:blue;">FETCH </span><span style="color:black;">NEXT </span><span style="color:blue;">FROM </span><span style="color:#434343;">@getprocName </span><span style="color:blue;">INTO </span><span style="color:#434343;">@procName<br />
</span><span style="color:blue;">END<br />
CLOSE </span><span style="color:#434343;">@getprocName<br />
</span><span style="color:blue;">DEALLOCATE </span><span style="color:#434343;">@getprocName<br />
</span><span style="color:black;">GO </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>
Posted in Database, Pinal Dave, SQL, SQL Authority, SQL Documentation, 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/1550/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1550/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1550/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1550/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1550/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1550/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1550/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1550/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1550/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1550/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1550&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/11/22/sql-server-simple-use-of-cursor-to-print-all-stored-procedures-of-database-including-schema/feed/</wfw:commentRss>
		<slash:comments>4</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; Simple Use of Cursor to Print All Stored Procedures of Database</title>
		<link>http://blog.sqlauthority.com/2008/11/20/sql-server-simple-use-of-cursor-to-print-all-stored-procedures-of-database/</link>
		<comments>http://blog.sqlauthority.com/2008/11/20/sql-server-simple-use-of-cursor-to-print-all-stored-procedures-of-database/#comments</comments>
		<pubDate>Thu, 20 Nov 2008 01:30:12 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Cursor]]></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://sqlauthority.wordpress.com/?p=1544</guid>
		<description><![CDATA[SQLAuthority Blog reader YordanGeorgiev has submitted very interesting SP, which uses cursor to generate text of all the Stored Procedure of current Database. This task can be done many ways, however, this is also interesting method.
USE AdventureWorks
GO
DECLARE @procName VARCHAR(100)
DECLARE @getprocName CURSOR
SET @getprocName = CURSOR FOR
SELECT s.name
FROM sysobjects s
WHERE type = 'P'
OPEN @getprocName
FETCH NEXT
FROM @getprocName INTO @procName
WHILE @@FETCH_STATUS = [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1544&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">SQLAuthority Blog reader <a href="http://blog.sqlauthority.com/2007/01/01/sql-server-simple-example-of-cursor/#comment-44238" target="_blank">YordanGeorgiev </a>has submitted very interesting SP, which uses cursor to generate text of all the Stored Procedure of current Database. This task can be done many ways, however, this is also interesting method.</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;">DECLARE </span><span style="color:#434343;">@procName </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;">@getprocName </span><span style="color:blue;">CURSOR<br />
SET </span><span style="color:#434343;">@getprocName </span><span style="color:blue;">= CURSOR FOR<br />
SELECT </span><span style="color:black;">s.name<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sysobjects s<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">type </span><span style="color:blue;">= </span><span style="color:red;">'P'<br />
</span><span style="color:blue;">OPEN </span><span style="color:#434343;">@getprocName<br />
</span><span style="color:blue;">FETCH </span><span style="color:black;">NEXT<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">@getprocName </span><span style="color:blue;">INTO </span><span style="color:#434343;">@procName<br />
</span><span style="color:blue;">WHILE </span><span style="color:#434343;">@@FETCH_STATUS </span><span style="color:blue;">= </span><span style="color:black;">0<br />
</span><span style="color:blue;">BEGIN<br />
EXEC </span><span style="color:darkred;">sp_HelpText </span><span style="color:#434343;">@procName<br />
</span><span style="color:blue;">FETCH </span><span style="color:black;">NEXT<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">@getprocName </span><span style="color:blue;">INTO </span><span style="color:#434343;">@procName<br />
</span><span style="color:blue;">END<br />
CLOSE </span><span style="color:#434343;">@getprocName<br />
</span><span style="color:blue;">DEALLOCATE </span><span style="color:#434343;">@getprocName<br />
</span><span style="color:black;">GO </span></code>
</p>
<p style="text-align:justify;"><span style="color:black;">Just give this script a try and it will print text of all the SP in your database. If you are using Grid View for Result Pan I suggest to change it to Text View (CTRL+T) to read the text easily.</span></p>
<p style="text-align:justify;"><span style="color:black;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.SQLAuthority.com" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong><br />
</span></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Cursor, 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/1544/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1544/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1544/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1544/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1544/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1544/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1544/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1544/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1544/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1544/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1544&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/11/20/sql-server-simple-use-of-cursor-to-print-all-stored-procedures-of-database/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; Delete Backup History &#8211; Cleanup Backup History</title>
		<link>http://blog.sqlauthority.com/2008/11/11/sql-server-delete-backup-history-cleanup-backup-history/</link>
		<comments>http://blog.sqlauthority.com/2008/11/11/sql-server-delete-backup-history-cleanup-backup-history/#comments</comments>
		<pubDate>Tue, 11 Nov 2008 01:30:32 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Backup and Restore]]></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://sqlauthority.wordpress.com/?p=1491</guid>
		<description><![CDATA[SQL Server stores history of all the taken backup forever. History of all the backup is stored in msdb database. Many times older history is no more required. Following Stored Procedure can be executed with parameter which takes days of history to keep. In following example 30 is passed to keep history of month.
USE msdb
GO
DECLARE [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1491&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">SQL Server stores history of all the taken backup forever. History of all the backup is stored in msdb database. Many times older history is no more required. Following Stored Procedure can be executed with parameter which takes days of history to keep. In following example 30 is passed to keep history of month.</p>
<p><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">msdb<br />
GO<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@DaysToKeepHistory </span><span style="color:black;">DATETIME<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@DaysToKeepHistory </span><span style="color:blue;">= </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;">10</span><span style="color:gray;">), </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">dd</span><span style="color:gray;">, -</span><span style="color:black;">30</span><span style="color:gray;">, </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">()), </span><span style="color:black;">101</span><span style="color:gray;">)<br />
</span><span style="color:blue;">EXEC </span><span style="color:darkred;">sp_delete_backuphistory </span><span style="color:#434343;">@DaysToKeepHistory<br />
</span><span style="color:black;">GO </span></code></p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (<a href="http://www.sqlauthority.com/" target="_blank">http://www.SQLAuthority.com</a>)</strong><em><br />
</em></p>
Posted in Best Practices, Database, Pinal Dave, SQL, SQL Authority, SQL Backup and Restore, 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/1491/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1491/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1491/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1491/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1491/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1491/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1491/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1491/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1491/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1491/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1491&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/11/11/sql-server-delete-backup-history-cleanup-backup-history/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; Refresh Database Using T-SQL</title>
		<link>http://blog.sqlauthority.com/2008/11/07/sql-server-refresh-database-using-t-sql/</link>
		<comments>http://blog.sqlauthority.com/2008/11/07/sql-server-refresh-database-using-t-sql/#comments</comments>
		<pubDate>Fri, 07 Nov 2008 01:30:13 +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 Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server DBCC]]></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://sqlauthority.wordpress.com/?p=1480</guid>
		<description><![CDATA[Yesterday I received following questions on blog. Ashish Agarwal asked following question.
Hi Pinal,
Can we refresh a database (like we do by right clicking database node in object explorer and clicking on refresh) thru SQL Query?
If yes, can you please tell me the query?
Thanks,
Ashish Agarwal
Answer to above question is NO. It is not possible to do [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1480&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Yesterday I received following questions on blog. <a href="http://blog.sqlauthority.com/contact-me-contact-pinaldave/#comment-44079" target="_blank">Ashish Agarwal</a> asked following question.</p>
<p style="padding-left:30px;"><em>Hi Pinal,</em></p>
<p style="padding-left:30px;"><em>Can we refresh a database (like we do by right clicking database node in object explorer and clicking on refresh) thru SQL Query?<br />
If yes, can you please tell me the query?</em></p>
<p style="padding-left:30px;"><em>Thanks,<br />
Ashish Agarwal</em></p>
<p>Answer to above question is <strong>NO</strong>. It is not possible to do the same task using SQL Query.</p>
<p>However, if you have changed some SP or any other object and if they are cached in the database, database can be refreshed using DBCC commands.</p>
<p>Read my previous article about <strong><a href="http://blog.sqlauthority.com/2008/07/22/sql-server-clear-sql-server-memory-caches/" target="_blank">SQL SERVER &#8211; Clear SQL Server Memory Caches</a></strong>.</p>
<p>Reference: <strong>Pinal Dave (<a href="http://www.SQLAuthority.com" target="_blank">http://www.SQLAuthority.com</a>)</strong><em><br />
</em></p>
Posted in Pinal Dave, Readers Question, SQL, SQL Authority, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Server DBCC, SQL Stored Procedure, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/1480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1480/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1480/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1480/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1480&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/11/07/sql-server-refresh-database-using-t-sql/feed/</wfw:commentRss>
		<slash:comments>4</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 : Incorrect syntax near . You may need to set the compatibility level of the current database to a higher value to enable this feature. See help for the stored procedure sp_dbcmptlevel</title>
		<link>http://blog.sqlauthority.com/2008/10/21/sql-server-fix-error-incorrect-syntax-near-you-may-need-to-set-the-compatibility-level-of-the-current-database-to-a-higher-value-to-enable-this-feature-see-help-for-the-stored-procedure-sp_db/</link>
		<comments>http://blog.sqlauthority.com/2008/10/21/sql-server-fix-error-incorrect-syntax-near-you-may-need-to-set-the-compatibility-level-of-the-current-database-to-a-higher-value-to-enable-this-feature-see-help-for-the-stored-procedure-sp_db/#comments</comments>
		<pubDate>Tue, 21 Oct 2008 01:30:00 +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 Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Security]]></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://sqlauthority.wordpress.com/?p=1411</guid>
		<description><![CDATA[I have seen developer confused many times when they receive following error message.
Msg 325, Level 15, State 1, Line 7
Incorrect syntax near . You may need to set the compatibility level of the current database to a higher value to enable this feature. See help for the stored procedure sp_dbcmptlevel.

The reason for this error is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1411&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">I have seen developer confused many times when they receive following error message.</p>
<p style="text-align:justify;"><span style="color:#ff0000;"><em>Msg 325, Level 15, State 1, Line 7<br />
Incorrect syntax near . You may need to set the compatibility level of the current database to a higher value to enable this feature. See help for the stored procedure sp_dbcmptlevel.</em></span>
</p>
<p style="text-align:justify;">The reason for this error is when user is tring to attempt to run query or procedure or logic which is not compatible with previous version of the SQL Server. When SQL Server 2000 is upgraded to SQL Server 2005 or SQL Server 2008, the database object compatibility should be also upgraded to next version. When database compatibility is set to previous version and they are attempted with procedure of newer version they will throw above error.</p>
<p style="text-align:justify;"><strong>Fix/Workaround/Solution:</strong></p>
<p style="text-align:justify;">Change the database compatibility level using following command.</p>
<p style="text-align:justify;">For SQL Server 2005:<br />
<code style="font-size:12px;"><span style="color:blue;">EXEC </span><span style="color:darkred;">sp_dbcmptlevel </span><span style="color:red;">'DatabaseName'</span><span style="color:gray;">, </span><span style="color:black;">90</span></code></p>
<p style="text-align:justify;">For SQL Server 2008:<br />
<code style="font-size:12px;"><span style="color:blue;">EXEC </span><span style="color:darkred;">sp_dbcmptlevel </span><span style="color:red;">'DatabaseName'</span><span style="color:gray;">, </span><span style="color:black;">100</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>
Posted in Pinal Dave, SQL, SQL Authority, SQL Error Messages, SQL Query, SQL Scripts, SQL Security, 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/1411/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1411/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1411/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1411/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1411/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1411/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1411/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1411/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1411/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1411/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1411&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/10/21/sql-server-fix-error-incorrect-syntax-near-you-may-need-to-set-the-compatibility-level-of-the-current-database-to-a-higher-value-to-enable-this-feature-see-help-for-the-stored-procedure-sp_db/feed/</wfw:commentRss>
		<slash:comments>4</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; Introduction to CLR &#8211; Simple Example of CLR Stored Procedure</title>
		<link>http://blog.sqlauthority.com/2008/10/19/sql-server-introduction-to-clr-simple-example-of-clr-stored-procedure/</link>
		<comments>http://blog.sqlauthority.com/2008/10/19/sql-server-introduction-to-clr-simple-example-of-clr-stored-procedure/#comments</comments>
		<pubDate>Sun, 19 Oct 2008 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 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[SQL Utility]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[CLR]]></category>

		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1390</guid>
		<description><![CDATA[CLR is abbreviation of Common Language Runtime. In SQL Server 2005 and later version of it database objects can be created which are created in CLR. Stored Procedures, Functions, Triggers can be coded in CLR. CLR is faster than T-SQL in many cases. CLR is mainly used to accomplish task which are not possible by [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1390&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">CLR is abbreviation of Common Language Runtime. In SQL Server 2005 and later version of it database objects can be created which are created in CLR. Stored Procedures, Functions, Triggers can be coded in CLR. CLR is faster than T-SQL in many cases. CLR is mainly used to accomplish task which are not possible by T-SQL or can use lots of resources. CLR can be usually implemented where there is intense string operation, thread management or iteration methods which can be complicated for T-SQL. Implementing CLR provides more security to Extended Stored Procedure.</p>
<p style="text-align:justify;">Let us create one very simple CLR where we will print current system datetime.</p>
<p style="text-align:justify;"><strong>1) Open Microsoft Visual Studio &gt;&gt; Click New Project &gt;&gt; Select Visual C# &gt;&gt; Database &gt;&gt; SQL Server Project</strong></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/clr/clr1.gif" alt="" width="500" height="429" /></p>
<p style="text-align:justify;"><strong>2) Either choose from existing database connection as reference or click on Add New Reference. In my example I have selected Add New Reference. </strong></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/clr/clr2.gif" alt="" width="364" height="374" /></p>
<p style="text-align:justify;"><strong>3) If you have selected existing reference skip to next step or add database reference as displayed in image.</strong></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/clr/clr3.gif" alt="" width="451" height="657" /></p>
<p style="text-align:justify;"><strong>4) Once database reference is added following project will be displayed in Solution Explorer. Right click on Solution Explorer &gt;&gt; Click on Add &gt;&gt; Stored Procedure.</strong></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/clr/clr4.gif" alt="" width="434" height="699" /></p>
<p style="text-align:justify;"><strong>5) Add new stored procedure template from following screen.</strong></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/clr/clr5.gif" alt="" width="500" height="383" /></p>
<p style="text-align:justify;"><strong>6) Once template added it will look like following image. </strong></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/clr/clr6.gif" alt="" width="437" height="431" /></p>
<p style="text-align:justify;"><strong>7) Now where it suggest to //Put your code here. Replace it with code displayed in the image. Once the code is complete do following two steps.<br />
a) Click on menu bar &gt;&gt; Build &gt;&gt; Build ProjectName<br />
b) Click on menu bar &gt;&gt; Build &gt;&gt; Deploy ProjectName<br />
Building and Deploying project should give successful message.<br />
</strong></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/clr/clr7.gif" alt="" width="500" height="386" /><br />
<span class="kwrd">using</span> System;<br />
<span class="kwrd">using</span> System.Data;<br />
<span class="kwrd">using</span> System.Data.SqlClient;<br />
<span class="kwrd">using</span> System.Data.SqlTypes;<br />
<span class="kwrd">using</span> Microsoft.SqlServer.Server;</p>
<p style="text-align:justify;"><span class="kwrd">public</span> <span class="kwrd">partial</span> <span class="kwrd">class</span> StoredProcedures<br />
{<br />
[Microsoft.SqlServer.Server.SqlProcedure]<br />
<span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> CLRSPTest()<br />
{<br />
SqlPipe sp;<br />
sp = SqlContext.Pipe;<br />
String strCurrentTime = <span class="str">&#8220;Current System DateTime is: &#8220;</span><br />
+ System.DateTime.Now.ToString();<br />
sp.Send(strCurrentTime);<br />
}<br />
};<br />
<strong>8) Now open SQL Server Management Studio and run following script in Query Editor. It should return current system datetime. Running it again the time will change.</strong>
</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/clr/clr8.gif" alt="" width="387" height="162" /></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:black;">dbo.CLRSPTest<br />
GO</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>
Posted in Pinal Dave, Software Development, SQL, SQL Authority, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Stored Procedure, SQL Tips and Tricks, SQL Utility, T SQL, Technology Tagged: CLR <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/1390/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1390/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1390/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1390/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1390/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1390/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1390/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1390/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1390/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1390/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1390&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/10/19/sql-server-introduction-to-clr-simple-example-of-clr-stored-procedure/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>

		<media:content url="http://www.pinaldave.com/blogfolder/clr/clr1.gif" medium="image" />

		<media:content url="http://www.pinaldave.com/blogfolder/clr/clr2.gif" medium="image" />

		<media:content url="http://www.pinaldave.com/blogfolder/clr/clr3.gif" medium="image" />

		<media:content url="http://www.pinaldave.com/blogfolder/clr/clr4.gif" medium="image" />

		<media:content url="http://www.pinaldave.com/blogfolder/clr/clr5.gif" medium="image" />

		<media:content url="http://www.pinaldave.com/blogfolder/clr/clr6.gif" medium="image" />

		<media:content url="http://www.pinaldave.com/blogfolder/clr/clr7.gif" medium="image" />

		<media:content url="http://www.pinaldave.com/blogfolder/clr/clr8.gif" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Renaming SP is Not Good Idea &#8211; Renaming Stored Procedure Does Not Update sys.procedures</title>
		<link>http://blog.sqlauthority.com/2008/09/28/sql-server-renaming-stored-procedure-does-not-update-sysprocedures/</link>
		<comments>http://blog.sqlauthority.com/2008/09/28/sql-server-renaming-stored-procedure-does-not-update-sysprocedures/#comments</comments>
		<pubDate>Sun, 28 Sep 2008 01:30:39 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></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 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://sqlauthority.wordpress.com/?p=1195</guid>
		<description><![CDATA[I have written many articles about renaming a tables, columns and procedures SQL SERVER &#8211; How to Rename a Column Name or Table Name, here I found something interesting about renaming the stored procedures and felt like sharing it with you all.
The interesting fact is that when we rename a stored procedure using SP_Rename command, the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1195&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">I have written many articles about renaming a tables, columns and procedures<a title="Permanent Link to SQL SERVER - How to Rename a Column Name or Table Name" rel="bookmark"> </a><a href="http://blog.sqlauthority.com/2008/08/26/sql-server-how-to-rename-a-column-name-or-table-name/" target="_blank"><strong>SQL SERVER &#8211; How to Rename a Column Name or Table Name</strong></a>, here I found something interesting about renaming the stored procedures and felt like sharing it with you all.</p>
<p style="text-align:justify;">The interesting fact is that when we rename a stored procedure using SP_Rename command, the Stored Procedure is successfully renamed. But when we try to text the procedure using sp_helptext, the procedure will be having the old name instead of new name.</p>
<p style="text-align:justify;">Example,</p>
<p style="text-align:justify;">-          <em>Create a Stored Procedure in AdventureWorks Database named </em>SP_Employee<em>.</em></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
</span></code><code style="font-size:12px;"><span style="color:blue;">GO</span></code><code style="font-size:12px;"><span style="color:black;"><br />
</span><span style="color:blue;">CREATE PROCEDURE </span><span style="color:darkred;">sp_Employee<br />
</span><span style="color:blue;">AS<br />
SELECT </span><span style="color:gray;">* </span><span style="color:blue;">FROM </span><span style="color:black;">dbo.Employee<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">FName </span><span style="color:gray;">LIKE </span><span style="color:red;">'%i%'<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">EMPID<br />
</span></code><code style="font-size:12px;"><span style="color:blue;">GO</span></code>
</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/sp_createProcedure.jpg" alt="" width="500" height="359" /></p>
<p style="text-align:justify;">-          <em>Rename the Stored Procedure </em><em>SP_Employee to SP_Getemployee</em><em></em></p>
<p style="text-align:justify;">After Creating Stored Procedure, now we want to rename a stored procedure. So using sp_rename we can change the name of Stored Procedure as shown below:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:darkred;">sp_rename </span><span style="color:red;">'SP_Employee'</span><span style="color:gray;">, </span><span style="color:red;">'SP_GetEmployee'</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/sp_rename.jpg" alt="" width="500" height="272" /></p>
<p style="text-align:justify;">-          <em>Use sp_helptext to see the stored procedure</em></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
</span></code><code style="font-size:12px;"><span style="color:blue;">GO</span></code><code style="font-size:12px;"><span style="color:black;"><br />
</span><span style="color:darkred;">sp_helptext sp_getemployee</span></code>
</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/sp_helptext.jpg" alt="" width="500" height="284" /></p>
<p style="text-align:justify;">We can see the name of the stored procedure is the old name and not new name but when we try to access the old name there is an error that sp not found.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/error.gif" alt="" width="500" height="92" /></p>
<p style="text-align:justify;"><strong><em>Conclusion</em></strong></p>
<p style="text-align:justify;">This happens because when the store procedure is renamed,  the sys.procedures system table is not getting updated. The only solution to this is to drop the stored procedure and re-create it with the new name.</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>
Posted in Pinal Dave, SQL, SQL Authority, SQL Documentation, 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/1195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1195/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1195/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1195&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/09/28/sql-server-renaming-stored-procedure-does-not-update-sysprocedures/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/blogfolder/sp_createProcedure.jpg" medium="image" />

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

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

		<media:content url="http://www.pinaldave.com/blogfolder/error.gif" medium="image" />
	</item>
	</channel>
</rss>