<?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>Fransiscus Setiawan &#187; SQL Server</title>
	<atom:link href="http://fransiscuss.com/category/sql-server/feed/" rel="self" type="application/rss+xml" />
	<link>http://fransiscuss.com</link>
	<description>My notepad of knowledge and my passion in development</description>
	<lastBuildDate>Tue, 22 May 2012 05:59:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='fransiscuss.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Fransiscus Setiawan &#187; SQL Server</title>
		<link>http://fransiscuss.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://fransiscuss.com/osd.xml" title="Fransiscus Setiawan" />
	<atom:link rel='hub' href='http://fransiscuss.com/?pushpress=hub'/>
		<item>
		<title>Table Spool (Lazy Spool) in SQL Server 2005</title>
		<link>http://fransiscuss.com/2012/05/22/table-spool-lazy-spool-in-sql-server-2005/</link>
		<comments>http://fransiscuss.com/2012/05/22/table-spool-lazy-spool-in-sql-server-2005/#comments</comments>
		<pubDate>Tue, 22 May 2012 05:59:36 +0000</pubDate>
		<dc:creator>fransiscuss</dc:creator>
				<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=404</guid>
		<description><![CDATA[I have a web app that recently timing out and the timing out exception is actually coming from the SQL Server. When I run the execution plan I found that there is one item called Table Spool (Lazy Spool) which is costing about 20%. I thought it was caused by my full text search but [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fransiscuss.com&#038;blog=26182965&#038;post=404&#038;subd=fransiscuss&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have a web app that recently timing out and the timing out exception is actually coming from the SQL Server. When I run the execution plan I found that there is one item called Table Spool (Lazy Spool) which is costing about 20%. I thought it was caused by my full text search but when I drilled down further more is because of DISTINCT keyword. So I decided to change to use GROUP BY instead. In my case it is only a single column so It won&#8217;t make any difference at all. Once I&#8217;ve changed that my web application running fast and no more timeout</p>
<p>I got this explanation from <a href="http://forums.teradata.com/forum/database/spool-treatment-for-distinct-versus-group-by" target="_blank">this </a>website</p>
<p>Explain Distinct:<br />
3) We do an all-AMPs RETRIEVE step from &#8230; by way of an<br />
all-rows scan with no residual conditions into Spool x<br />
(group_amps), which is redistributed by hash code to all AMPs.<br />
Then we do a SORT to order Spool 1 by the sort key in spool field1<br />
eliminating duplicate rows.</p>
<p>First there&#8217;s a redistribution, then duplicate rows are removed:<br />
Efficient, if there are just a few rows per value [per AMP].<br />
Spool size is always about the same, but may be extremely skewed → 2646: No more Spool Space</p>
<p>Explain Group By:<br />
3) We do an all-AMPs SUM step to aggregate from &#8230; by way<br />
of an all-rows scan with no residual conditions, and the grouping<br />
identifier in field 1025. Aggregate Intermediate Results are<br />
computed globally, then placed in Spool x.</p>
<p>First each AMP removes duplicate rows locally (first aggregate) and hashes/redistributes the resulting<br />
rows, then there&#8217;s a second aggregation to remove duplicate rows:<br />
Efficient, if there are lots of rows per value [per AMP].<br />
Large number of rows per value Spool → small spool size<br />
Small number of rows per value Spool → large spool size<br />
Spool is never skewed.</p>
<p>Other interesting fact quoted from this <a href="http://stackoverflow.com/questions/9720988/sql-server-2005-table-spool-lazy-spool-performance" target="_blank">article/discussion</a></p>
<p><a href="http://www.simple-talk.com/sql/learn-sql-server/showplan-operator-of-the-week---lazy-spool/" rel="nofollow">http://www.simple-talk.com/sql/learn-sql-server/showplan-operator-of-the-week&#8212;lazy-spool/</a></p>
<p>http://www.sql-server-performance.com/forum/threads/table-spool-lazy-spool.15647/</p>
<p><strong>INDEXING</strong>: Take a look at your indices to make sure that they&#8217;re all covering the columns that you&#8217;re selecting out of the tables. You&#8217;ll want to aim to get all the columns included in JOINs and WHERE clauses within the indices. All other columns that are in the SELECT statements should be INCLUDEd, or covered, by the index.</p>
<p><strong>OPERATORS</strong>: See if you can get rid of the not equals (&#8220;&lt;&gt;&#8221;) operators, in favor of a single greater than or less than operator. Can this statement <code>and T.CurrentHorizon &lt;&gt; 0</code> be changed to this <code>and T.CurrentHorizon &gt; 0</code>?</p>
<p><strong>JOINS</strong>: Get rid of the subqueries that are JOINing to tables outside of themselves. For instance, this line <code>and FV2.elementId = FV.elementID</code> might be causing some problems. There&#8217;s no reason you can&#8217;t move that out of a subquery and into a JOIN to <code>dbo.aowCollectedFact FV</code>, given that you&#8217;re GROUPing (DISTINCT) in the main query already.</p>
<p><strong>DISTINCT</strong>: Change it to a GROUP BY. I&#8217;ve got no reason other than, because it&#8217;s good practice and takes two minutes.</p>
<p><strong>LAST NOTE</strong>: The exception to all the above might be to leave the final subquery, the <code>IF NOT EXISTS</code>, as a subquery. If you change it to a JOIN, it&#8217;ll have to be a <code>LEFT JOIN...WHERE NULL</code> statement, which can actually cause spooling operations. No great way to get around that one.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/fransiscuss.wordpress.com/404/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/fransiscuss.wordpress.com/404/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/fransiscuss.wordpress.com/404/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/fransiscuss.wordpress.com/404/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/fransiscuss.wordpress.com/404/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/fransiscuss.wordpress.com/404/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/fransiscuss.wordpress.com/404/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/fransiscuss.wordpress.com/404/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/fransiscuss.wordpress.com/404/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/fransiscuss.wordpress.com/404/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/fransiscuss.wordpress.com/404/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/fransiscuss.wordpress.com/404/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/fransiscuss.wordpress.com/404/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/fransiscuss.wordpress.com/404/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fransiscuss.com&#038;blog=26182965&#038;post=404&#038;subd=fransiscuss&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/2012/05/22/table-spool-lazy-spool-in-sql-server-2005/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/70e5c1d49eb30cdc979601f6e85e962a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">fransiscuss</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL Statistical information &#8211; Index usage, etc and also tips on how to find the most costly SQL Server queries using DMV&#8217;s</title>
		<link>http://fransiscuss.com/2011/11/07/sql-statistical-information-index-usage-etc-and-also-tips-on-how-to-find-the-most-costly-sql-server-queries-using-dmvs/</link>
		<comments>http://fransiscuss.com/2011/11/07/sql-statistical-information-index-usage-etc-and-also-tips-on-how-to-find-the-most-costly-sql-server-queries-using-dmvs/#comments</comments>
		<pubDate>Mon, 07 Nov 2011 02:49:06 +0000</pubDate>
		<dc:creator>fransiscuss</dc:creator>
				<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=309</guid>
		<description><![CDATA[This is just an information for the users that might not know this feature in SQL Server Management Studio, eventhough I’m sure most of people should have known this feature Basically SQL Server stores all the database usage information including index usage, disk usage, top transaction etc. *This information is not available from the backup [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fransiscuss.com&#038;blog=26182965&#038;post=309&#038;subd=fransiscuss&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is just an information for the users that might not know this feature in SQL Server Management Studio, eventhough I’m sure most of people should have known this feature</p>
<p>Basically SQL Server stores all the database usage information including index usage, disk usage, top transaction etc. *This information is not available from the backup file</p>
<p>1. Right click your database, Go to Reports, Go to Standard Reports and you can find all the out of the box reports</p>
<p><a href="http://fransiscuss.files.wordpress.com/2011/11/image003.png"><img class="alignnone size-full wp-image-312" title="image003" src="http://fransiscuss.files.wordpress.com/2011/11/image003.png?w=600&h=479" alt="" width="600" height="479" /></a></p>
<p>Clicking one of the report will give you detailed information – in this sample, I’m using Index Usage Statistics</p>
<p><a href="http://fransiscuss.files.wordpress.com/2011/11/image006.png"><img class="alignnone size-full wp-image-313" title="image006" src="http://fransiscuss.files.wordpress.com/2011/11/image006.png?w=600&h=610" alt="" width="600" height="610" /></a></p>
<p>How to find the top 20 most expensive queries by the total execution time, run this query against the database</p>
<pre><code>SELECT</code> <code>TOP</code> <code>20</code>
<code> qs.sql_handle,</code>
<code> qs.execution_count,</code>
<code> qs.total_worker_time AS</code> <code>Total_CPU,</code>
<code> total_CPU_inSeconds = --Converted from microseconds</code>
<code> qs.total_worker_time/1000000,</code>
<code> average_CPU_inSeconds = --Converted from microseconds</code>
<code> (qs.total_worker_time/1000000) / qs.execution_count,</code>
<code> qs.total_elapsed_time,</code>
<code> total_elapsed_time_inSeconds = --Converted from microseconds</code>
<code> qs.total_elapsed_time/1000000,</code>
<code> st.text,</code>
<code> qp.query_plan</code>
<code>FROM</code>
<code> sys.dm_exec_query_stats AS</code> <code>qs</code>
<code> CROSS</code> <code>APPLY sys.dm_exec_sql_text(qs.sql_handle) AS</code> <code>st</code>
<code> CROSS</code> <code>apply sys.dm_exec_query_plan (qs.plan_handle) AS</code> <code>qp</code>
<code>ORDER</code> <code>BY</code> <code>qs.total_worker_time DESC</code></pre>
<p>-It will give you this result set</p>
<p><a href="http://fransiscuss.files.wordpress.com/2011/11/image004.png"><img class="alignnone size-full wp-image-310" title="image004" src="http://fransiscuss.files.wordpress.com/2011/11/image004.png?w=600&h=196" alt="" width="600" height="196" /></a></p>
<p>- Clicking the query_plan will give you detailed query plan</p>
<p><a href="http://fransiscuss.files.wordpress.com/2011/11/image005.png"><img class="alignnone size-full wp-image-311" title="image005" src="http://fransiscuss.files.wordpress.com/2011/11/image005.png?w=600&h=441" alt="" width="600" height="441" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/fransiscuss.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/fransiscuss.wordpress.com/309/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/fransiscuss.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/fransiscuss.wordpress.com/309/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/fransiscuss.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/fransiscuss.wordpress.com/309/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/fransiscuss.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/fransiscuss.wordpress.com/309/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/fransiscuss.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/fransiscuss.wordpress.com/309/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/fransiscuss.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/fransiscuss.wordpress.com/309/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/fransiscuss.wordpress.com/309/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/fransiscuss.wordpress.com/309/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fransiscuss.com&#038;blog=26182965&#038;post=309&#038;subd=fransiscuss&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/2011/11/07/sql-statistical-information-index-usage-etc-and-also-tips-on-how-to-find-the-most-costly-sql-server-queries-using-dmvs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/70e5c1d49eb30cdc979601f6e85e962a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">fransiscuss</media:title>
		</media:content>

		<media:content url="http://fransiscuss.files.wordpress.com/2011/11/image003.png" medium="image">
			<media:title type="html">image003</media:title>
		</media:content>

		<media:content url="http://fransiscuss.files.wordpress.com/2011/11/image006.png" medium="image">
			<media:title type="html">image006</media:title>
		</media:content>

		<media:content url="http://fransiscuss.files.wordpress.com/2011/11/image004.png" medium="image">
			<media:title type="html">image004</media:title>
		</media:content>

		<media:content url="http://fransiscuss.files.wordpress.com/2011/11/image005.png" medium="image">
			<media:title type="html">image005</media:title>
		</media:content>
	</item>
		<item>
		<title>Usage of Pivot in SQL Server</title>
		<link>http://fransiscuss.com/2011/11/07/usage-of-pivot-in-sql-server/</link>
		<comments>http://fransiscuss.com/2011/11/07/usage-of-pivot-in-sql-server/#comments</comments>
		<pubDate>Mon, 07 Nov 2011 02:39:40 +0000</pubDate>
		<dc:creator>fransiscuss</dc:creator>
				<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=303</guid>
		<description><![CDATA[The usage of PIVOT keyword In SQL Server we can transform a horizontal rows to be vertically represented by using Pivot Keyword Run this query to have sample data CREATE TABLE OrderItems(Order_No INT, ProductName VARCHAR(255), TotalQty INT) GO INSERT INTO OrderItems(Order_No, ProductName, TotalQty) VALUES (112, &#8216;Sprite&#8217;, 5) GO INSERT INTO OrderItems(Order_No, ProductName, TotalQty) VALUES (112, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fransiscuss.com&#038;blog=26182965&#038;post=303&#038;subd=fransiscuss&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The usage of PIVOT keyword</p>
<p>In SQL Server we can transform a horizontal rows to be vertically represented by using Pivot Keyword</p>
<p>Run this query to have sample data</p>
<p>CREATE TABLE OrderItems(Order_No INT, ProductName VARCHAR(255), TotalQty INT)</p>
<p>GO</p>
<p>INSERT INTO OrderItems(Order_No, ProductName, TotalQty)</p>
<p>VALUES (112, &#8216;Sprite&#8217;, 5)</p>
<p>GO</p>
<p>INSERT INTO OrderItems(Order_No, ProductName, TotalQty)</p>
<p>VALUES (112, &#8216;Coke&#8217;, 7)</p>
<p>GO</p>
<p>INSERT INTO OrderItems(Order_No, ProductName, TotalQty)</p>
<p>VALUES (112, &#8216;Lemonade&#8217;, 2)</p>
<p>GO</p>
<p>INSERT INTO OrderItems(Order_No, ProductName, TotalQty)</p>
<p>VALUES (113, &#8216;Apple Juice&#8217;, 8 )</p>
<p>GO</p>
<p>INSERT INTO OrderItems(Order_No, ProductName, TotalQty)</p>
<p>VALUES (113, &#8216;Diet Coke&#8217;, 2)</p>
<p>GO</p>
<p>INSERT INTO OrderItems(Order_No, ProductName, TotalQty)</p>
<p>VALUES (114, &#8216;Coke&#8217;, 15)</p>
<p>GO</p>
<p>INSERT INTO OrderItems(Order_No, ProductName, TotalQty)</p>
<p>VALUES (114, &#8216;Lemonade&#8217;, 13)</p>
<p>GO</p>
<p>Running simple select from OrderItems will give you this table</p>
<p><a href="http://fransiscuss.files.wordpress.com/2011/11/image001.png"><img class="alignnone size-full wp-image-304" title="image001" src="http://fransiscuss.files.wordpress.com/2011/11/image001.png?w=600" alt=""   /></a></p>
<p>Now we want to look it statistically – maybe for reporting purpose. Run the query below</p>
<p>SELECT Order_No, Sprite, Coke, Lemonade, [Apple Juice], [Diet Coke]</p>
<p>FROM (</p>
<p>SELECT Order_No, ProductName, TotalQty</p>
<p>FROM OrderItems) ItemList</p>
<p>PIVOT (SUM(TotalQty) FOR ProductName IN (Sprite, Coke, Lemonade, [Apple Juice], [Diet Coke])) AS PivotResult</p>
<p>ORDER BY Order_No</p>
<p>GO</p>
<p>It will give you this result set</p>
<p><a href="http://fransiscuss.files.wordpress.com/2011/11/image002.png"><img class="alignnone size-full wp-image-305" title="image002" src="http://fransiscuss.files.wordpress.com/2011/11/image002.png?w=600" alt=""   /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/fransiscuss.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/fransiscuss.wordpress.com/303/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/fransiscuss.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/fransiscuss.wordpress.com/303/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/fransiscuss.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/fransiscuss.wordpress.com/303/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/fransiscuss.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/fransiscuss.wordpress.com/303/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/fransiscuss.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/fransiscuss.wordpress.com/303/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/fransiscuss.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/fransiscuss.wordpress.com/303/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/fransiscuss.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/fransiscuss.wordpress.com/303/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fransiscuss.com&#038;blog=26182965&#038;post=303&#038;subd=fransiscuss&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/2011/11/07/usage-of-pivot-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/70e5c1d49eb30cdc979601f6e85e962a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">fransiscuss</media:title>
		</media:content>

		<media:content url="http://fransiscuss.files.wordpress.com/2011/11/image001.png" medium="image">
			<media:title type="html">image001</media:title>
		</media:content>

		<media:content url="http://fransiscuss.files.wordpress.com/2011/11/image002.png" medium="image">
			<media:title type="html">image002</media:title>
		</media:content>
	</item>
		<item>
		<title>using BCP to transfer a large amount of data in SQL Server</title>
		<link>http://fransiscuss.com/2011/01/11/using-bcp-to-transfer-a-large-amount-of-data-in-sql-server/</link>
		<comments>http://fransiscuss.com/2011/01/11/using-bcp-to-transfer-a-large-amount-of-data-in-sql-server/#comments</comments>
		<pubDate>Tue, 11 Jan 2011 00:01:59 +0000</pubDate>
		<dc:creator>fransiscuss</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[BCP]]></category>
		<category><![CDATA[Data Transfer]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=281</guid>
		<description><![CDATA[I would like to transfer hundred millions of records between table in different database server. I tried to use DTS export/import and it&#8217;s not fast enough for my needs but i forget that there is a BCP command in SQL which is SQL Server format file. The speed in my local machine is (22795.13 rows [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fransiscuss.com&#038;blog=26182965&#038;post=281&#038;subd=fransiscuss&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I would like to transfer hundred millions of records between table in different database server. I tried to use DTS export/import and it&#8217;s not fast enough for my needs but i forget that there is a BCP command in SQL which is SQL Server format file.</p>
<p>The speed in my local machine is (22795.13 rows per sec)</p>
<p>Sample of the command: (You need to run it in command prompt), you can find bcp.exe in folder (C:\program files\Microsoft SQL Server\100\Tools\Binn) &#8211; The 100 is for SQL Server 2008, for SQL server 2005 it&#8217;s 90</p>
<p>Sample</p>
<p>1. You need to generate the BCP File from the source table</p>
<p><strong>bcp MyDatabase.dbo.MyTable out C:\BCP_MyTable -n -S localhost -T -e[BCP_MyTable_ERROR]</strong></p>
<p>*Using -T is for trusted connection</p>
<p>2. You need to import the BCP file to the destination table</p>
<p><strong>bcp</strong><strong> </strong><strong> MyDatabase.dbo.MyTable in C:\BCP_MyTable -n -S localhost -T -e[BCP_MyTable_ERROR]</strong><strong></strong></p>
<p>for further command line reference click <a href="http://msdn.microsoft.com/en-us/library/ms162802.aspx" target="_blank">here </a></p>
<p>*UPDATE:</p>
<p>you can configure your server to to reduce the amount of transaction log during bulk copy/insert transaction by executing</p>
<p><span id="ContentPlaceHolder1_lblDescription" style="color:darkblue;">EXEC SP_DBOPTION MyDatabase, &#8216;SELECT INTO/BULKCOPY&#8217;, TRUE</span></p>
<p><strong>bcp</strong><strong> </strong><strong> MyDatabase.dbo.MyTable in C:\BCP_MyTable <span style="color:#ff0000;">/b 20000 </span>-n -S localhost -T -e[BCP_MyTable_ERROR]</strong></p>
<p>What I did was to add extra parameter of <span style="color:#ff0000;"><strong>/b</strong></span> and the number <span style="color:#ff0000;"><strong>20000</strong></span> (you should play around with the number to see the best one fit your situation) after it means the number of the records per transaction. Please be careful if you don&#8217;t put <strong><span style="color:#ff0000;">/b</span></strong> parameter, sql server by default will commit all the records at once. but if you put <strong><span style="color:#ff0000;">/b</span></strong> parameter then it will commit the transaction per x amount of records specified. If you transfer large data, you shouldn&#8217;t commit all at once because it will take sometime. In my case I transferred 18 millions of records</p>
<p>I&#8217;d recommend you to save the BCP file and copy the file over if it&#8217;s in different server because you need to be careful with the bandwidth of the network and the risk if the network connection is interrupted</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/fransiscuss.wordpress.com/281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/fransiscuss.wordpress.com/281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/fransiscuss.wordpress.com/281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/fransiscuss.wordpress.com/281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/fransiscuss.wordpress.com/281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/fransiscuss.wordpress.com/281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/fransiscuss.wordpress.com/281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/fransiscuss.wordpress.com/281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/fransiscuss.wordpress.com/281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/fransiscuss.wordpress.com/281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/fransiscuss.wordpress.com/281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/fransiscuss.wordpress.com/281/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/fransiscuss.wordpress.com/281/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/fransiscuss.wordpress.com/281/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fransiscuss.com&#038;blog=26182965&#038;post=281&#038;subd=fransiscuss&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/2011/01/11/using-bcp-to-transfer-a-large-amount-of-data-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/70e5c1d49eb30cdc979601f6e85e962a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">fransiscuss</media:title>
		</media:content>
	</item>
		<item>
		<title>Restore SQL Server database through network path</title>
		<link>http://fransiscuss.com/2010/11/10/restore-sql-server-database-through-network-path/</link>
		<comments>http://fransiscuss.com/2010/11/10/restore-sql-server-database-through-network-path/#comments</comments>
		<pubDate>Wed, 10 Nov 2010 05:51:08 +0000</pubDate>
		<dc:creator>fransiscuss</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=278</guid>
		<description><![CDATA[I had a problem where I need to restore database using shared folder on the network. I tried to copy it locally to my local harddrive but then the connection is not stable enough. Hence I decided to create a script Single Media USE MASTER DECLARE @fromdatabase varchar(50) DECLARE @todatabase varchar(50) DECLARE @sql as nvarchar(4000) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fransiscuss.com&#038;blog=26182965&#038;post=278&#038;subd=fransiscuss&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I had a problem where I need to restore database using shared folder on the network. I tried to copy it locally to my local harddrive but then the connection is not stable enough. Hence I decided to create a script</p>
<p><strong>Single Media</strong></p>
<pre>
USE MASTER
DECLARE @fromdatabase varchar(50)

DECLARE @todatabase varchar(50)
DECLARE @sql as nvarchar(4000)
DECLARE @BackupPath as nvarchar(250)



/***************************************/
/* Change this string to match the database
	name you want to create a backup of  */
Set @fromdatabase = 'DailySupport-1'
Set @todatabase = 'MyDatabaseName'
/***************************************/



/***************************************/
/*  Which db server are you using?     */
/***************************************/

Set @BackupPath='\\sharednetwork\SQL\DatabasesBackup\'

/***************************************/
/***************************************/


/** Kill off any existing connections **/
SET @sql = ' use master'
SET @sql = @sql + ' EXEC sp_KILLSPIDS ' + @todatabase + ';'
Print @sql
EXEC sp_executesql @sql


/** Perform the restore **/
SET @sql = 'RESTORE DATABASE ' + @todatabase
SET @sql = @sql + ' FROM DISK = N''' + @BackupPath + '' + @fromdatabase + '.bak'''
SET @sql = @sql + ' WITH FILE = 1,NOUNLOAD, REPLACE, STATS=10 ;'
Print @sql
EXEC sp_executesql @sql
</pre>
<p><strong>Multiple Media</strong><strong>(Sometimes the backup source is stripped multiple files hence if you try to run it with above script you will get this error &#8220;The media set has 2 media families but only 1 are provided. All members must be provided.&#8221;)</strong></p>
<pre>
USE MASTER
DECLARE @fromdatabase varchar(50)
DECLARE @fromdatabase2 varchar(50)

DECLARE @todatabase varchar(50)
DECLARE @sql as nvarchar(4000)
DECLARE @BackupPath as nvarchar(250)



/***************************************/
/* Change this string to match the database
	name you want to create a backup of  */
Set @fromdatabase = 'DailySupport-1'
SET @fromdatabase2 = 'DailySupport-2'
Set @todatabase = 'MyDatabaseName'
/***************************************/



/***************************************/
/*  Which db server are you using?     */
/***************************************/

Set @BackupPath='\\sharednetwork\SQL\DatabasesBackup\'

/***************************************/
/***************************************/


/** Kill off any existing connections **/
SET @sql = ' use master'
SET @sql = @sql + ' EXEC sp_KILLSPIDS ' + @todatabase + ';'
Print @sql
EXEC sp_executesql @sql


/** Perform the restore **/
SET @sql = 'RESTORE DATABASE ' + @todatabase
SET @sql = @sql + ' FROM DISK = N''' + @BackupPath + '' + @fromdatabase + '.bak'''
SET @sql = @sql + ', DISK = N''' + @BackupPath + '' + @fromdatabase2 + '.bak'''
SET @sql = @sql + ' WITH FILE = 1,NOUNLOAD, REPLACE, STATS=10 ;'
Print @sql
EXEC sp_executesql @sql
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/fransiscuss.wordpress.com/278/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/fransiscuss.wordpress.com/278/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/fransiscuss.wordpress.com/278/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/fransiscuss.wordpress.com/278/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/fransiscuss.wordpress.com/278/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/fransiscuss.wordpress.com/278/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/fransiscuss.wordpress.com/278/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/fransiscuss.wordpress.com/278/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/fransiscuss.wordpress.com/278/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/fransiscuss.wordpress.com/278/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/fransiscuss.wordpress.com/278/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/fransiscuss.wordpress.com/278/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/fransiscuss.wordpress.com/278/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/fransiscuss.wordpress.com/278/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fransiscuss.com&#038;blog=26182965&#038;post=278&#038;subd=fransiscuss&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/2010/11/10/restore-sql-server-database-through-network-path/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/70e5c1d49eb30cdc979601f6e85e962a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">fransiscuss</media:title>
		</media:content>
	</item>
		<item>
		<title>Read and Update XML Node of XML datatype in SQL Server</title>
		<link>http://fransiscuss.com/2010/07/22/read-and-update-xml-node-of-xml-datatype-in-sql-server/</link>
		<comments>http://fransiscuss.com/2010/07/22/read-and-update-xml-node-of-xml-datatype-in-sql-server/#comments</comments>
		<pubDate>Thu, 22 Jul 2010 00:04:18 +0000</pubDate>
		<dc:creator>fransiscuss</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[XML SQL Server]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=265</guid>
		<description><![CDATA[Select Node value This is a simple explanation on how to select a particular node from xml data type of a record in SQL server. Sample XML 00003479 Brodie's HomeSton NULL AU941170 2010-07-22T09:42:20 vcAuditorDesc,chUpdateStaffCode,dtUpdateDateTime,dtUpdateDateTime So what I want to do is basically to get the value of ChangedColumns node select xmlDetail.value('/iReference[1]/ChangedColumns[1]/.','VARCHAR(255)') from tblReferenceHistory WHERE intReferenceHistoryID [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fransiscuss.com&#038;blog=26182965&#038;post=265&#038;subd=fransiscuss&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong><br />
Select Node value<br />
</strong><br />
This is a simple explanation on how to select a particular node from xml data type of a record in SQL server.</p>
<p>Sample XML</p>
<pre>

  00003479
  Brodie's HomeSton
  NULL
  
  
  AU941170
  2010-07-22T09:42:20
  vcAuditorDesc,chUpdateStaffCode,dtUpdateDateTime,dtUpdateDateTime

</pre>
<p>So what I want to do is basically to get the value of ChangedColumns node</p>
<pre>
select  xmlDetail.value('/iReference[1]/ChangedColumns[1]/.','VARCHAR(255)')
from tblReferenceHistory
WHERE intReferenceHistoryID = 125
</pre>
<p><strong><br />
Update Existing Node value<br />
</strong></p>
<p>Sample XML</p>
<pre>

  00003479
  Brodie's HomeSton
  NULL
  
  
  AU941170
  2010-07-22T09:42:20
  

</pre>
<p>Changed column has no value in it and you want to update it. Basically you can&#8217;t edit the existing node value so what you need to do is to readd it and remove the first instance of that node</p>
<pre>
DECLARE @ChangedColumns VARCHAR(255)

SELECT @ChangedColumns = xmlDetail.value('/iReference[1]/ChangedColumns[1]/.','VARCHAR(255)')
FROM tblReferenceHistory
WHERE intReferenceHistoryID = 125

-- first update - add a new ... node
UPDATE tblReferenceHistory
SET xmlDetail.modify('insert {sql:variable("@ChangedColumns")} as last into (/iReference)[1]')
WHERE intReferenceHistoryID = 124

-- second update - remove the empty  node
UPDATE dbo.tblReferenceHistory
SET xmlDetail.modify('delete (/iReference[1]/ChangedColumns[1])[1]')
WHERE intReferenceHistoryID = 124

SELECT xmlDetail.value('/iReference[1]/ChangedColumns[1]/.','VARCHAR(255)')
FROM tblReferenceHistory
WHERE intReferenceHistoryID = 124
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/fransiscuss.wordpress.com/265/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/fransiscuss.wordpress.com/265/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/fransiscuss.wordpress.com/265/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/fransiscuss.wordpress.com/265/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/fransiscuss.wordpress.com/265/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/fransiscuss.wordpress.com/265/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/fransiscuss.wordpress.com/265/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/fransiscuss.wordpress.com/265/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/fransiscuss.wordpress.com/265/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/fransiscuss.wordpress.com/265/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/fransiscuss.wordpress.com/265/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/fransiscuss.wordpress.com/265/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/fransiscuss.wordpress.com/265/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/fransiscuss.wordpress.com/265/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fransiscuss.com&#038;blog=26182965&#038;post=265&#038;subd=fransiscuss&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/2010/07/22/read-and-update-xml-node-of-xml-datatype-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/70e5c1d49eb30cdc979601f6e85e962a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">fransiscuss</media:title>
		</media:content>
	</item>
		<item>
		<title>How to set variable from Dynamic SQL</title>
		<link>http://fransiscuss.com/2010/07/09/how-to-set-variable-from-dynamic-sql/</link>
		<comments>http://fransiscuss.com/2010/07/09/how-to-set-variable-from-dynamic-sql/#comments</comments>
		<pubDate>Fri, 09 Jul 2010 05:07:04 +0000</pubDate>
		<dc:creator>fransiscuss</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=259</guid>
		<description><![CDATA[Here is the scenario, You have a stored procedure that builds dynamic insert SQL which means the &#8220;INSERT&#8221; statement is being composed on the fly based on the table name but at the same time you need to get SCOPE_IDENTITY or the last identity number inserted for further processing (e.g History table) DECLARE @nvcSQL NVARCHAR(MAX) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fransiscuss.com&#038;blog=26182965&#038;post=259&#038;subd=fransiscuss&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Here is the scenario, You have a stored procedure that builds dynamic insert SQL which means the &#8220;INSERT&#8221; statement is being composed on the fly based on the table name but at the same time you need to get SCOPE_IDENTITY or the last identity number inserted for further processing (e.g History table)</p>
<pre>
	DECLARE @nvcSQL			NVARCHAR(MAX)
	DECLARE @pvcTableName	VARCHAR(255)
	DECLARE @pvcColumn		VARCHAR(255)
	DECLARE @pvcValue		NVARCHAR(MAX)

	--This is used to actually get the primary key for identity record
	DECLARE @dvcPrimaryKey VARCHAR(255)
	SET @dvcPrimaryKey = ''

	DECLARE @dintPrimaryKey INT
	SELECT @dvcPrimaryKey = ISNULL(NAME,'') FROM SYS.COLUMNS WHERE OBJECT_NAME(OBJECT_ID) = @pvcTableName AND Is_Identity = 1


	-- Only execute when there is a @pvcValue.
	IF @pvcValue  ''
	BEGIN
		SELECT @nvcSQL = 'INSERT INTO ' + @pvcTableName + '(' + @pvcColumn+ ') VALUES (' + @pvcValue + ')'
		SELECT @nvcSQL = @nvcSQL + ';' + 'SELECT @dintPrimaryKey = SCOPE_IDENTITY()'
		--PRINT @nvcSQL

		EXEC sp_executesql @query = @nvcSQL, @params = N'@dintPrimaryKey INT OUTPUT', @dintPrimaryKey = @dintPrimaryKey OUTPUT
		--EXEC (@nvcSQL)
	END

	IF @dvcPrimaryKey  ''
	BEGIN
		SELECT (@dvcPrimaryKey + ' = ' + CAST(@dintPrimaryKey AS VARCHAR(10)))  AS PrimaryKey
	END
	ELSE
	BEGIN
		SELECT '' AS PrimaryKey
	END
</pre>
<p>Question: why do we need to use sp_executesql instead of EXEC?because EXEC just execute a SQL command without returning/expecting any result from it</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/fransiscuss.wordpress.com/259/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/fransiscuss.wordpress.com/259/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/fransiscuss.wordpress.com/259/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/fransiscuss.wordpress.com/259/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/fransiscuss.wordpress.com/259/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/fransiscuss.wordpress.com/259/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/fransiscuss.wordpress.com/259/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/fransiscuss.wordpress.com/259/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/fransiscuss.wordpress.com/259/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/fransiscuss.wordpress.com/259/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/fransiscuss.wordpress.com/259/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/fransiscuss.wordpress.com/259/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/fransiscuss.wordpress.com/259/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/fransiscuss.wordpress.com/259/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fransiscuss.com&#038;blog=26182965&#038;post=259&#038;subd=fransiscuss&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/2010/07/09/how-to-set-variable-from-dynamic-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/70e5c1d49eb30cdc979601f6e85e962a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">fransiscuss</media:title>
		</media:content>
	</item>
		<item>
		<title>Shrink all databases and Set it to Simple in SQL Server</title>
		<link>http://fransiscuss.com/2010/01/06/shrink-all-databases-and-set-it-to-simple-in-sql-server/</link>
		<comments>http://fransiscuss.com/2010/01/06/shrink-all-databases-and-set-it-to-simple-in-sql-server/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 05:02:44 +0000</pubDate>
		<dc:creator>fransiscuss</dc:creator>
				<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=249</guid>
		<description><![CDATA[Normally, I need to run this script against dev sql server in order for me to free up some space create table #temp_dbs_table ( [db_name] sysname not null primary key, [mod] tinyint not null default 1 ) insert into #temp_dbs_table ([db_name]) select name from master..sysdatabases where dbid &#62; 4 --- skip master, tempdb, model and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fransiscuss.com&#038;blog=26182965&#038;post=249&#038;subd=fransiscuss&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Normally, I need to run this script against dev sql server in order for me to free up some space</p>
<pre>
create table #temp_dbs_table
(
[db_name] sysname not null primary key,
[mod] tinyint not null default 1
)

insert into #temp_dbs_table ([db_name])
select
name
from
master..sysdatabases
where
dbid &gt; 4 --- skip master, tempdb, model and msdb databases

declare @db_name sysname

set @db_name = ''

while @db_name is not null
begin
set @db_name = NULL

select top 1 @db_name = [db_name] from #temp_dbs_table where [mod] = 1

if @db_name is NULL
break

print '--------------------------------------------------'

print '&gt; Database: ' + @db_name

print '&gt; Changing recovery mode to simple'

declare @n_cmd nvarchar(4000)

set @n_cmd = 'alter database [' + @db_name + '] set recovery simple'

exec sp_executesql @n_cmd

print '&gt; Shrinking database'

set @n_cmd = 'dbcc shrinkdatabase([' + @db_name + '])'

exec sp_executesql @n_cmd

update #temp_dbs_table set [mod] = 0 where [db_name] = @db_name
end

drop table #temp_dbs_table
</pre>
<p>I got this script from <a href="http://www.sqlservercentral.com/scripts/Backup+%2F+Restore/61952/" target="_blank">here</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/fransiscuss.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/fransiscuss.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/fransiscuss.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/fransiscuss.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/fransiscuss.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/fransiscuss.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/fransiscuss.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/fransiscuss.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/fransiscuss.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/fransiscuss.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/fransiscuss.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/fransiscuss.wordpress.com/249/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/fransiscuss.wordpress.com/249/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/fransiscuss.wordpress.com/249/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fransiscuss.com&#038;blog=26182965&#038;post=249&#038;subd=fransiscuss&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/2010/01/06/shrink-all-databases-and-set-it-to-simple-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/70e5c1d49eb30cdc979601f6e85e962a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">fransiscuss</media:title>
		</media:content>
	</item>
		<item>
		<title>TRY&#8230;.CATCH Rollback Transaction In SQL Server 2005</title>
		<link>http://fransiscuss.com/2009/05/23/trycatch-rollback-transaction-in-sql-server-2005/</link>
		<comments>http://fransiscuss.com/2009/05/23/trycatch-rollback-transaction-in-sql-server-2005/#comments</comments>
		<pubDate>Sat, 23 May 2009 01:16:53 +0000</pubDate>
		<dc:creator>fransiscuss</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[ROLLBACK]]></category>
		<category><![CDATA[TRANSACTION]]></category>
		<category><![CDATA[TRY CATCH]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=215</guid>
		<description><![CDATA[This feature has been sometime in SQL Server 2005 in SQL Server 2000 you normally use @@TRANCOUNT to detect any exception but now in SQL Server 2005 you can use try catch block. In this snippet, there are 2 INSERT statement and 1 UPDATE statement. I&#8217;m expecting when there is any failure (e.g the UPDATE [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fransiscuss.com&#038;blog=26182965&#038;post=215&#038;subd=fransiscuss&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This feature has been sometime in SQL Server 2005 in SQL Server 2000 you normally use @@TRANCOUNT to detect any exception but now in SQL Server 2005 you can use try catch block.</p>
<p>In this snippet, there are 2 INSERT statement and 1 UPDATE statement. I&#8217;m expecting when there is any failure (e.g the UPDATE statement fails) then all the preceding INSERT/UPDATE/DELETE within the TRANSACTION block will be canceled</p>
<p>e.g</p>
<pre>
BEGIN TRY
BEGIN TRANSACTION transABC

INSERT INTO TABLEA (col1,col2,col3) VALUES ('a','b','c')
INSERT INTO TABLEB (col1,col2,col3) VALUES ('a','b','c')

UPDATE TABLEA SET col2='abcde' WHERE col1 = 'a'

COMMIT TRANSACTION transABC
END TRY

BEGIN CATCH
IF @@TRANCOUNT &gt; 0
ROLLBACK TRANSACTION transABC --RollBack in case of Error

-- Raise an error with the details of the exception
DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int
SELECT @ErrMsg = ERROR_MESSAGE(),
@ErrSeverity = ERROR_SEVERITY()

RAISERROR(@ErrMsg, @ErrSeverity, 1)

END CATCH
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/fransiscuss.wordpress.com/215/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/fransiscuss.wordpress.com/215/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/fransiscuss.wordpress.com/215/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/fransiscuss.wordpress.com/215/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/fransiscuss.wordpress.com/215/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/fransiscuss.wordpress.com/215/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/fransiscuss.wordpress.com/215/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/fransiscuss.wordpress.com/215/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/fransiscuss.wordpress.com/215/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/fransiscuss.wordpress.com/215/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/fransiscuss.wordpress.com/215/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/fransiscuss.wordpress.com/215/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/fransiscuss.wordpress.com/215/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/fransiscuss.wordpress.com/215/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fransiscuss.com&#038;blog=26182965&#038;post=215&#038;subd=fransiscuss&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/2009/05/23/trycatch-rollback-transaction-in-sql-server-2005/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/70e5c1d49eb30cdc979601f6e85e962a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">fransiscuss</media:title>
		</media:content>
	</item>
		<item>
		<title>Index in Table Variable</title>
		<link>http://fransiscuss.com/2009/05/14/index-in-table-variable/</link>
		<comments>http://fransiscuss.com/2009/05/14/index-in-table-variable/#comments</comments>
		<pubDate>Thu, 14 May 2009 07:32:32 +0000</pubDate>
		<dc:creator>fransiscuss</dc:creator>
				<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://fransiscuss.com/?p=212</guid>
		<description><![CDATA[One biggest advantage of table variable for me is you can&#8217;t put index on the columns. I was having a problem where doing self join in table variable (it has 1000+ records) is taking really really long time. One way I found to optimize it is to add primary key assuming every single record is [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fransiscuss.com&#038;blog=26182965&#038;post=212&#038;subd=fransiscuss&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>One biggest advantage of table variable for me is you can&#8217;t put index on the columns. I was having a problem where doing self join in table variable (it has 1000+ records) is taking really really long time. One way I found to optimize it is to add <strong>primary key</strong> assuming every single record is unique on the table variable. The performance improvement is really significant by adding the primary key on table variable. And <strong>by changing table variable to temp table</strong>, it improves the performance more than by twice</p>
<pre>
CREATE TABLE #dtblJobsBillDates
(
		chProcessID			CHAR(03)		COLLATE database_default NOT NULL,
		intAssgnmtID		INTEGER									 NOT NULL,
		chBillOfficeCode	CHAR(03)		COLLATE database_default NOT NULL,
		chBillEntityCode	CHAR(03)		COLLATE database_default NOT NULL,
		chBillNo			CHAR(08)		COLLATE database_default NOT NULL,
		PRIMARY KEY (intAssgnmtID, chBillNo, chProcessID, chBillOfficeCode, chBillEntityCode)
)
</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/fransiscuss.wordpress.com/212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/fransiscuss.wordpress.com/212/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/fransiscuss.wordpress.com/212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/fransiscuss.wordpress.com/212/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/fransiscuss.wordpress.com/212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/fransiscuss.wordpress.com/212/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/fransiscuss.wordpress.com/212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/fransiscuss.wordpress.com/212/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/fransiscuss.wordpress.com/212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/fransiscuss.wordpress.com/212/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/fransiscuss.wordpress.com/212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/fransiscuss.wordpress.com/212/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/fransiscuss.wordpress.com/212/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/fransiscuss.wordpress.com/212/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fransiscuss.com&#038;blog=26182965&#038;post=212&#038;subd=fransiscuss&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://fransiscuss.com/2009/05/14/index-in-table-variable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/70e5c1d49eb30cdc979601f6e85e962a?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">fransiscuss</media:title>
		</media:content>
	</item>
	</channel>
</rss>
