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

<channel>
	<title>tim laqua dot com &#187; sql</title>
	<atom:link href="http://timlaqua.com/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://timlaqua.com</link>
	<description>Thoughts and Code from Tim Laqua</description>
	<lastBuildDate>Sun, 09 May 2010 15:25:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Locating Rampant Database Growth</title>
		<link>http://timlaqua.com/2010/03/locating-rampant-database-growth/</link>
		<comments>http://timlaqua.com/2010/03/locating-rampant-database-growth/#comments</comments>
		<pubDate>Fri, 19 Mar 2010 17:40:02 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[tsql]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=358</guid>
		<description><![CDATA[Every now and then you get a helpful alert from one of your database servers about disk space being low (you're monitoring that, right?), or a bunch of autogrowth alerts (you're monitoring that too, right?) - but what happens when you get these for a database that you don't expect growth in? Further, what happens [...]]]></description>
			<content:encoded><![CDATA[<p>Every now and then you get a helpful alert from one of your database servers about disk space being low (you're monitoring that, right?), or a bunch of autogrowth alerts (you're monitoring that too, right?) - but what happens when you get these for a database that you don't expect growth in?  Further, what happens when that database is growing rampantly (say like 1GB/hr in my case) and it's a canned database from a 3rd party product?  This time it was the database that SolarWinds uses for collection - and apparently it was collecting a lot of something that it wasn't collecting before.</p>
<ol>
<li>you send out an email asking the end users of the system (IT in this case) if anything changed</li>
<li>adjust so said system stops trying to fill up your drives</li>
</ol>
<p>Unfortunately, there's often quite a few possibilities for "why" a database is growing - and when it's a canned product, you don't always have the best understanding of why it does what it does when it does it.  As a Database Admin, you can help diagnose the problem by letting everyone know what exactly is growing:<br />
<span id="more-358"></span><br />
First, figure out what table is growing:<br />
<em>spSpaceUsed.sql</em></p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #808080; font-style: italic;">#Temp_SpSpaceUsed</span>
	<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>name<span style="color: #66cc66;">&#93;</span> VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#91;</span>rows<span style="color: #66cc66;">&#93;</span> INT
	<span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#91;</span>reserved<span style="color: #66cc66;">&#93;</span> VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">DATA</span><span style="color: #66cc66;">&#93;</span>	VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#91;</span>index_size<span style="color: #66cc66;">&#93;</span> VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#91;</span>unused<span style="color: #66cc66;">&#93;</span> VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#91;</span>CreatedTime<span style="color: #66cc66;">&#93;</span> DATETIME <span style="color: #993333; font-weight: bold;">DEFAULT</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
EXEC sp_msforeachtable <span style="color: #ff0000;">'INSERT INTO #Temp_SpSpaceUsed ([name],[rows],[reserved],[data],[index_size],[unused]) EXEC sp_spaceused [?]'</span>
&nbsp;
WAITFOR DELAY <span style="color: #ff0000;">'00:01:00'</span>
&nbsp;
EXEC sp_msforeachtable <span style="color: #ff0000;">'INSERT INTO #Temp_SpSpaceUsed ([name],[rows],[reserved],[data],[index_size],[unused]) EXEC sp_spaceused [?]'</span>
&nbsp;
;WITH SpSpaceUsed <span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #66cc66;">&#40;</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span> 
		<span style="color: #66cc66;">&#91;</span>name<span style="color: #66cc66;">&#93;</span>
		<span style="color: #66cc66;">,</span>CAST<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">LEFT</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">DATA</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">,</span> LEN<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">DATA</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> INT<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">DATA</span><span style="color: #66cc66;">&#93;</span>
		<span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#91;</span>CreatedTime<span style="color: #66cc66;">&#93;</span>
		<span style="color: #66cc66;">,</span>ROW_NUMBER<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> OVER <span style="color: #66cc66;">&#40;</span>PARTITION <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #66cc66;">&#91;</span>name<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #66cc66;">&#91;</span>CreatedTime<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">ASC</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>RowNumber<span style="color: #66cc66;">&#93;</span>
	<span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #808080; font-style: italic;">#Temp_SpSpaceUsed	</span>
<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> 
	 a<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>name<span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">,</span>b<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">DATA</span> <span style="color: #66cc66;">-</span> a<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">DATA</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>GrowthKB<span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">,</span><span style="color: #66cc66;">&#40;</span>CAST<span style="color: #66cc66;">&#40;</span>b<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">DATA</span> <span style="color: #66cc66;">-</span> a<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">DATA</span> <span style="color: #993333; font-weight: bold;">AS</span> FLOAT<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> DateDiff<span style="color: #66cc66;">&#40;</span>MILLISECOND<span style="color: #66cc66;">,</span> a<span style="color: #66cc66;">.</span>CreatedTime<span style="color: #66cc66;">,</span> b<span style="color: #66cc66;">.</span>CreatedTime<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">1000.0</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">60.0</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">60.0</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#91;</span>GrowthKBPerHour<span style="color: #66cc66;">&#93;</span>
<span style="color: #993333; font-weight: bold;">FROM</span> SpSpaceUsed a
	<span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> SpSpaceUsed b
		<span style="color: #993333; font-weight: bold;">ON</span> a<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>name<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> b<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>name<span style="color: #66cc66;">&#93;</span>
			<span style="color: #993333; font-weight: bold;">AND</span> a<span style="color: #66cc66;">.</span>RowNumber <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>
			<span style="color: #993333; font-weight: bold;">AND</span> b<span style="color: #66cc66;">.</span>RowNumber <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">2</span>
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span>
	GrowthKBPerHour <span style="color: #993333; font-weight: bold;">DESC</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #808080; font-style: italic;">#Temp_SpSpaceUsed</span></pre></div></div>

<p>Next, look at the culprit (assuming there's one table doing all the damage - I've found that's usually the case) and try to decipher the column names.  Finally, run a few queries against the table - WITH(NOLOCK) of course - hey, this table is pretty busy.  Now try and see where all the action is coming from.  In this case, one of the columns was [IP] so I just did a COUNT(1) by IP for the last hour's records.  Sure enough, 99% of the records were coming from a single IP, sent that IP off to the guys who know what IPs mean, and they knew exactly which change caused the growth.</p>
<p>Bottom line - nobody knows everything about everything, but we can learn pretty quick with the right information.</p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2010/03/locating-rampant-database-growth/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Script sp_configure Output To Migrate Settings Between SQL Servers</title>
		<link>http://timlaqua.com/2010/02/script-sp_configure-output-to-migrate-settings-between-sql-servers/</link>
		<comments>http://timlaqua.com/2010/02/script-sp_configure-output-to-migrate-settings-between-sql-servers/#comments</comments>
		<pubDate>Sat, 27 Feb 2010 23:22:30 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sql server]]></category>
		<category><![CDATA[tsql]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=355</guid>
		<description><![CDATA[Sometimes when setting up a new server, all you want is for it to work and be configured exactly like the old server. In some cases, the new server is almost identical in every way already. If you've worked much with SQL Server, you know very well that there are a billion switches and knobs [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes when setting up a new server, all you want is for it to work and be configured exactly like the old server.  In some cases, the new server is almost identical in every way already.  If you've worked much with SQL Server, you know very well that there are a billion switches and knobs and every time you do this, you forget at least one.  I had a similar situation recently and figured I'd be lazy about it and just turned the output of sp_configure in to a script I could execute on the new server:<br />
<span id="more-355"></span><br />
<em>scriptSpConfigure.sql - run this on the old/source server</em></p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'show advanced options'</span> , <span style="color: #000;">1</span>;
GO
<span style="color: #0000FF;">RECONFIGURE</span>;
GO
<span style="color: #0000FF;">DECLARE</span> @spConfigureOutput <span style="color: #0000FF;">TABLE</span>
	<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>name<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>
	,<span style="color: #808080;">&#91;</span>minimum<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">INT</span>
	,<span style="color: #808080;">&#91;</span>maximum<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">INT</span>
	,<span style="color: #808080;">&#91;</span>config_value<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">INT</span>
	,<span style="color: #808080;">&#91;</span>run_value<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">INT</span><span style="color: #808080;">&#41;</span>
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @spConfigureOutput
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span>
&nbsp;
<span style="color: #0000FF;">SELECT</span>	<span style="color: #FF0000;">'EXEC sp_configure '</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> name <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">', '</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>config_value <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">FROM</span>	@spConfigureOutput
GO
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'show advanced options'</span> , <span style="color: #000;">0</span>;
GO
<span style="color: #0000FF;">RECONFIGURE</span>;
GO</pre></div></div>

<p>Copy the selected rows and paste in to another SSMS window, add the show advanced options switch above and below - should end up with something like this after you <strong>remove all the settings you don't care about or don't want on the new server</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'show advanced options'</span> , <span style="color: #000;">1</span>;
GO
<span style="color: #0000FF;">RECONFIGURE</span>;
GO
&nbsp;
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'Ad Hoc Distributed Queries'</span>, <span style="color: #000;">1</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'Agent XPs'</span>, <span style="color: #000;">1</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'clr enabled'</span>, <span style="color: #000;">1</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'cost threshold for parallelism'</span>, <span style="color: #000;">5</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'cursor threshold'</span>, <span style="color: #808080;">-</span><span style="color: #000;">1</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'Database Mail XPs'</span>, <span style="color: #000;">1</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'default full-text language'</span>, <span style="color: #000;">1033</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'default trace enabled'</span>, <span style="color: #000;">1</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'fill factor (%)'</span>, <span style="color: #000;">80</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'ft crawl bandwidth (max)'</span>, <span style="color: #000;">100</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'ft notify bandwidth (max)'</span>, <span style="color: #000;">100</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'max full-text crawl range'</span>, <span style="color: #000;">4</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'max server memory (MB)'</span>, <span style="color: #000;">10000</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'max text repl size (B)'</span>, <span style="color: #000;">65536</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'min memory per query (KB)'</span>, <span style="color: #000;">1024</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'min server memory (MB)'</span>, <span style="color: #000;">10000</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'nested triggers'</span>, <span style="color: #000;">1</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'network packet size (B)'</span>, <span style="color: #000;">4096</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'PH timeout (s)'</span>, <span style="color: #000;">60</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'query wait (s)'</span>, <span style="color: #808080;">-</span><span style="color: #000;">1</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'remote access'</span>, <span style="color: #000;">1</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'remote login timeout (s)'</span>, <span style="color: #000;">20</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'remote query timeout (s)'</span>, <span style="color: #000;">900</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'scan for startup procs'</span>, <span style="color: #000;">1</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'server trigger recursion'</span>, <span style="color: #000;">1</span>
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'SMO and DMO XPs'</span>, <span style="color: #000;">1</span>
&nbsp;
<span style="color: #0000FF;">EXEC</span> <span style="color: #AF0000;">SP_CONFIGURE</span> <span style="color: #FF0000;">'show advanced options'</span> , <span style="color: #000;">0</span>;
<span style="color: #0000FF;">RECONFIGURE</span>;
GO;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2010/02/script-sp_configure-output-to-migrate-settings-between-sql-servers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Script RESTORE DATABASE &#8230; WITH MOVE Stub</title>
		<link>http://timlaqua.com/2010/02/script-restore-database-with-move-stub/</link>
		<comments>http://timlaqua.com/2010/02/script-restore-database-with-move-stub/#comments</comments>
		<pubDate>Sat, 27 Feb 2010 18:29:26 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[restore]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sql server]]></category>
		<category><![CDATA[tsql]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=349</guid>
		<description><![CDATA[I think most people setup the drives on their Development servers to match their Production servers - this is so that restores go smoothly and files go where they're supposed to, things match up nicely, etc. Unfortunately, when you create a full backup all the backup file contains is the logical name of all the [...]]]></description>
			<content:encoded><![CDATA[<p>I think most people setup the drives on their Development servers to match their Production servers - this is so that restores go smoothly and files go where they're supposed to, things match up nicely, etc.  Unfortunately, when you create a full backup all the backup file contains is the logical name of all the files - no physical paths.  This means that if the database doesn't exist on the destination server yet, the engine has absolutely no idea where to put the files or what to name them.</p>
<p>The following script can be executed in the context of the source (original) database that was backed up to script out the MOVE statements if your plan is to put the files in the same place on the destination server.<br />
<span id="more-349"></span><br />
<em>scriptRestoreWithMove.sql - run this in the context of the source database</em></p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">NOCOUNT</span> <span style="color: #0000FF;">ON</span>
&nbsp;
<span style="color: #0000FF;">DECLARE</span> @MoveOption <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">TABLE</span>
	<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>Id<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">INT</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>,<span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>
	,<span style="color: #808080;">&#91;</span>MoveOption<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
&nbsp;
<span style="color: #0000FF;">PRINT</span> <span style="color: #FF0000;">'RESTORE DATABASE '</span> <span style="color: #808080;">+</span> <span style="color: #FF00FF;">DB_NAME</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">DB_ID</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
<span style="color: #008080;">-- Edit this to match where you're restoring from</span>
<span style="color: #0000FF;">PRINT</span> <span style="color: #FF0000;">'FROM DISK = '</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> <span style="color: #FF00FF;">DB_NAME</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">DB_ID</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'.bak'</span><span style="color: #FF0000;">''</span>  
<span style="color: #0000FF;">PRINT</span> <span style="color: #FF0000;">'WITH'</span>
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @MoveOption <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>MoveOption<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span>
    <span style="color: #FF0000;">'MOVE '</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> a.<span style="color: #202020;">name</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">' TO '</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> a.<span style="color: #202020;">FILENAME</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">''</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>MOVE <span style="color: #0000FF;">OPTION</span><span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">FROM</span>
    dbo.<span style="color: #202020;">sysfiles</span> a
&nbsp;
<span style="color: #0000FF;">DECLARE</span> 
	 @LastId <span style="color: #0000FF;">INT</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span>
	,@MoveOptionText <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span>
&nbsp;
<span style="color: #0000FF;">WHILE</span> <span style="color: #808080;">EXISTS</span> <span style="color: #808080;">&#40;</span><span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">1</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">FROM</span> @MoveOption <span style="color: #0000FF;">WHERE</span> <span style="color: #808080;">&#91;</span>Id<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&gt;</span> @LastId<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">BEGIN</span>
	<span style="color: #0000FF;">SELECT</span> 		<span style="color: #0000FF;">TOP</span> <span style="color: #000;">1</span>
			 @MoveOptionText <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>MoveOption<span style="color: #808080;">&#93;</span> 
			,@LastId <span style="color: #808080;">=</span> <span style="color: #808080;">&#91;</span>Id<span style="color: #808080;">&#93;</span>
	<span style="color: #0000FF;">FROM</span>		@MoveOption
	<span style="color: #0000FF;">WHERE</span>		<span style="color: #808080;">&#91;</span>Id<span style="color: #808080;">&#93;</span> <span style="color: #808080;">&gt;</span> @LastId
	<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span>	<span style="color: #808080;">&#91;</span>Id<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">ASC</span>
&nbsp;
	<span style="color: #0000FF;">PRINT</span> <span style="color: #0000FF;">CASE</span> <span style="color: #0000FF;">WHEN</span> @LastId <span style="color: #808080;">=</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">''</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #FF0000;">','</span> <span style="color: #0000FF;">END</span> <span style="color: #808080;">+</span> @MoveOptionText
<span style="color: #0000FF;">END</span>
&nbsp;
<span style="color: #0000FF;">PRINT</span> <span style="color: #FF0000;">'GO'</span>
&nbsp;
<span style="color: #0000FF;">PRINT</span> <span style="color: #FF0000;">'ALTER DATABASE '</span> <span style="color: #808080;">+</span> <span style="color: #FF00FF;">DB_NAME</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">DB_ID</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' SET MULTI_USER'</span>
<span style="color: #0000FF;">PRINT</span> <span style="color: #FF0000;">'GO'</span></pre></div></div>

<p><em>And the output looks like this:</em></p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">RESTORE</span> <span style="color: #0000FF;">DATABASE</span> AdventureWorksDW
<span style="color: #0000FF;">FROM</span> <span style="color: #0000FF;">DISK</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'AdventureWorksDW.bak'</span>
<span style="color: #0000FF;">WITH</span>
MOVE <span style="color: #FF0000;">'AdventureWorksDW_Data'</span> <span style="color: #0000FF;">TO</span> <span style="color: #FF0000;">'D:<span style="color: #000099; font-weight: bold;">\A</span>dventureWorksDW<span style="color: #000099; font-weight: bold;">\A</span>dventureWorksDW_Data.mdf'</span>
,MOVE <span style="color: #FF0000;">'AdventureWorksDW_Log'</span> <span style="color: #0000FF;">TO</span> <span style="color: #FF0000;">'T:<span style="color: #000099; font-weight: bold;">\A</span>dventureWorksDW<span style="color: #000099; font-weight: bold;">\A</span>dventureWorksDW_Log.LDF'</span>
GO
<span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">DATABASE</span> AdventureWorksDW <span style="color: #0000FF;">SET</span> MULTI_USER
GO</pre></div></div>

<p>If you end up wanting to do something similar with existing databases, make sure to add a command before the RESTORE to set SINGLE_USER mode.</p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2010/02/script-restore-database-with-move-stub/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Clone Analysis Services Partitions with PowerShell</title>
		<link>http://timlaqua.com/2010/01/clone-analysis-services-partitions-with-powershell/</link>
		<comments>http://timlaqua.com/2010/01/clone-analysis-services-partitions-with-powershell/#comments</comments>
		<pubDate>Fri, 15 Jan 2010 20:45:20 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[amo]]></category>
		<category><![CDATA[analysis services]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[business intelligence]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[t-sql]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=322</guid>
		<description><![CDATA[Most of us with large Analysis Services cubes partition our cubes by month or year or some other time-based slice and we have all, at one point or another, developed some way to create partitions for new months on-demand. Often, the solution to this seems to be a C# console application or SSIS package using [...]]]></description>
			<content:encoded><![CDATA[<p>Most of us with large Analysis Services cubes partition our cubes by month or year or some other time-based slice and we have all, at one point or another, developed some way to create partitions for new months on-demand.  Often, the solution to this seems to be a C# console application or SSIS package using AMO to create a new partition based off an existing partition.  The problem I see with this is that maintaining it requires opening up the project or package, making changes, re-compiling, deploying, testing, deploying to production, verifying, etc.  It also requires that whoever is going to maintain it is comfortable with C#.</p>
<p>To simplify the maintenance and get rid of the "black box" factor that utility apps like this tend to have, I put together a PowerShell script to do the same thing and a stored procedure to call the script.  Really, it doesn't matter what you use as you're most likely using an almost identical chunk of code to get your new partition created - my argument is that using PowerShell instead of C# or SSIS reduces the cost of maintenance, improves readability, and facilitates better understanding throughout your team.<br />
<span id="more-322"></span><br />
<em>The PowerShell Script: cloneSSASPartition.ps1</em></p>

<div class="wp_syntax"><div class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">param</span><span style="color: #000000;">&#40;</span>
   <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span> <span style="color: #800080;">$ServerName</span><span style="color: pink;">,</span> 		<span style="color: pink;">&lt;</span><span style="color: #008000;"># The name of the Analysis Services Instance #&gt;</span>
   <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span> <span style="color: #800080;">$DatabaseName</span><span style="color: pink;">,</span> 		<span style="color: pink;">&lt;</span><span style="color: #008000;"># The name of the Database our Cube is in #&gt;</span>
   <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span> <span style="color: #800080;">$CubeName</span><span style="color: pink;">,</span>     		<span style="color: pink;">&lt;</span><span style="color: #008000;"># The name of the Cube that our Measure Group is in #&gt;</span>
   <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span> <span style="color: #800080;">$MeasureGroupName</span><span style="color: pink;">,</span> 	<span style="color: pink;">&lt;</span><span style="color: #008000;"># The name of the Measure Group that our Partition is in #&gt;</span>
   <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span> <span style="color: #800080;">$SourcePartitionName</span><span style="color: pink;">,</span> 	<span style="color: pink;">&lt;</span><span style="color: #008000;"># The name of the Partition we want to merge in to another partition #&gt;</span>
   <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span> <span style="color: #800080;">$NewPartitionName</span><span style="color: pink;">,</span> 	<span style="color: pink;">&lt;</span><span style="color: #008000;"># The desired name of the new partition #&gt;</span>
   <span style="color: #000000;">&#91;</span><span style="color: #008080;">string</span><span style="color: #000000;">&#93;</span> <span style="color: #800080;">$NewPartitionQuery</span> 	<span style="color: pink;">&lt;</span><span style="color: #008000;"># The Query source for the new partition (will use the same datasource as the SourcePartition #&gt;</span>
<span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #000000;">&#91;</span><span style="color: #008080;">System.Reflection.Assembly</span><span style="color: #000000;">&#93;</span>::<span style="color: #800000;">LoadWithPartialName</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Microsoft.AnalysisServices&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">&gt;</span><span style="color: #800080;">$NULL</span>
&nbsp;
<span style="color: #800080;">$server</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> Microsoft.AnalysisServices.Server
<span style="color: #800080;">$server</span>.Connect<span style="color: #000000;">&#40;</span><span style="color: #800080;">$ServerName</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #800080;">$database</span> <span style="color: pink;">=</span> <span style="color: #800080;">$server</span>.Databases.GetByName<span style="color: #000000;">&#40;</span><span style="color: #800080;">$DatabaseName</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$cube</span> <span style="color: pink;">=</span> <span style="color: #800080;">$database</span>.Cubes.GetByName<span style="color: #000000;">&#40;</span><span style="color: #800080;">$CubeName</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$measureGroup</span> <span style="color: pink;">=</span> <span style="color: #800080;">$cube</span>.MeasureGroups.FindByName<span style="color: #000000;">&#40;</span><span style="color: #800080;">$MeasureGroupName</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$sourcePartition</span> <span style="color: pink;">=</span> <span style="color: #800080;">$measureGroup</span>.Partitions.GetByName<span style="color: #000000;">&#40;</span><span style="color: #800080;">$SourcePartitionName</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$sourceQueryBinding</span> <span style="color: pink;">=</span> <span style="color: #800080;">$sourcePartition</span>.Source
&nbsp;
<span style="color: #800080;">$newPartition</span> <span style="color: pink;">=</span> <span style="color: #800080;">$sourcePartition</span>.Clone<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$newPartition</span>.ID <span style="color: pink;">=</span> <span style="color: #800080;">$NewPartitionName</span>
<span style="color: #800080;">$newPartition</span>.Name <span style="color: pink;">=</span> <span style="color: #800080;">$NewPartitionName</span>
<span style="color: #800080;">$measureGroup</span>.Partitions.Add<span style="color: #000000;">&#40;</span><span style="color: #800080;">$newPartition</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">&gt;</span> <span style="color: #800080;">$NULL</span>
<span style="color: #800080;">$newPartition</span>.Source <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> Microsoft.AnalysisServices.QueryBinding<span style="color: #000000;">&#40;</span><span style="color: #800080;">$sourceQueryBinding</span>.DataSourceID<span style="color: pink;">,</span> <span style="color: #800080;">$NewPartitionQuery</span><span style="color: #000000;">&#41;</span>
<span style="color: #800080;">$newPartition</span>.Update<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
<span style="color: #800080;">$server</span>.Disconnect<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></div></div>

<p><em>The stored procedure to call the script: [Utility_CloneSSASPartition_S01] - assumes you placed cloneSSASPartition.ps1 at the root of the C: drive.  If you put it elsewhere, update the proc to reflect the actual location.</em></p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SET</span> ANSI_<span style="color: #808080;">NULL</span>S <span style="color: #0000FF;">ON</span>
GO
&nbsp;
<span style="color: #0000FF;">SET</span> QUOTED_IDENTIFIER <span style="color: #0000FF;">ON</span>
GO
&nbsp;
<span style="color: #008080;">/*************************************************************************************
***
*** Procedure:  		[Utility_CloneSSASPartition]
*** Purpose:		Executes PowerShell script to clone a SSAS partition
***			
***			
*** Author:		tl
*** Date Created:		2010-01-15
*** 
*** Revision History
*** Date		Author			Description
*** 2010-01-15	tl			Created
*************************************************************************************/</span>
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">PROCEDURE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Utility_CloneSSASPartition_S01<span style="color: #808080;">&#93;</span>
<span style="color: #808080;">&#40;</span>
	@ServerName <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>,
	@DatabaseName <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>,
	@CubeName <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>,
	@MeasureGroupName <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>,
	@SourcePartitionName <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>,
	@NewPartitionName <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>,
	@NewPartitionQuery <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">AS</span> 
&nbsp;
<span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">NOCOUNT</span> <span style="color: #0000FF;">ON</span>
<span style="color: #0000FF;">DECLARE</span> @cmd <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">8000</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SET</span> @cmd <span style="color: #808080;">=</span> 
	<span style="color: #FF0000;">'powershell.exe -Command &quot;&amp; {'</span>
	<span style="color: #808080;">+</span> <span style="color: #FF0000;">' C:<span style="color: #000099; font-weight: bold;">\c</span>loneSSASPartition.ps1'</span>
	<span style="color: #808080;">+</span> <span style="color: #FF0000;">' -ServerName:<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span> <span style="color: #808080;">+</span> @ServerName <span style="color: #808080;">+</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span>
	<span style="color: #808080;">+</span> <span style="color: #FF0000;">' -DatabaseName:<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span> <span style="color: #808080;">+</span> @DatabaseName <span style="color: #808080;">+</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span>
	<span style="color: #808080;">+</span> <span style="color: #FF0000;">' -CubeName:<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span> <span style="color: #808080;">+</span> @CubeName <span style="color: #808080;">+</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span>
	<span style="color: #808080;">+</span> <span style="color: #FF0000;">' -MeasureGroupName:<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span> <span style="color: #808080;">+</span> @MeasureGroupName <span style="color: #808080;">+</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span>
	<span style="color: #808080;">+</span> <span style="color: #FF0000;">' -SourcePartitionName:<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span> <span style="color: #808080;">+</span> @SourcePartitionName <span style="color: #808080;">+</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span>
	<span style="color: #808080;">+</span> <span style="color: #FF0000;">' -NewPartitionName:<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span> <span style="color: #808080;">+</span> @NewPartitionName <span style="color: #808080;">+</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span>
	<span style="color: #808080;">+</span> <span style="color: #FF0000;">' -NewPartitionQuery:<span style="color: #000099; font-weight: bold;">\&quot;</span>'</span> <span style="color: #808080;">+</span> @NewPartitionQuery <span style="color: #808080;">+</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\&quot;</span>}&quot;'</span>
&nbsp;
<span style="color: #0000FF;">PRINT</span> @cmd
<span style="color: #0000FF;">EXEC</span> xp_cmdshell @cmd</pre></div></div>

<p>And, finally, we execute it</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">EXEC</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Utility_CloneSSASPartition_S01<span style="color: #808080;">&#93;</span>
	 @ServerName <span style="color: #808080;">=</span> <span style="color: #FF0000;">'TESTSERVER'</span>
	,@DatabaseName <span style="color: #808080;">=</span> <span style="color: #FF0000;">'TestDatabase'</span>
	,@CubeName <span style="color: #808080;">=</span> <span style="color: #FF0000;">'TestCube'</span>
	,@MeasureGroupName <span style="color: #808080;">=</span> <span style="color: #FF0000;">'Test Measure Group'</span>
	,@SourcePartitionName <span style="color: #808080;">=</span> <span style="color: #FF0000;">'Test Partition - 200912'</span>
	,@NewPartitionName <span style="color: #808080;">=</span> <span style="color: #FF0000;">'Test Partition - 201001'</span>
	,@NewPartitionQuery <span style="color: #808080;">=</span> <span style="color: #FF0000;">'SELECT * FROM dbo.FactTableMonth('</span><span style="color: #FF0000;">'1/1/2010'</span><span style="color: #FF0000;">', '</span><span style="color: #FF0000;">'2/1/2010'</span><span style="color: #FF0000;">')'</span></pre></div></div>

<p>Again, I'm not saying there's anything "wrong" with using C# or SSIS Packages to do this sort of thing, I'm just pointing out that C# isn't always the answer - especially when someone other than you needs to maintain this thing in the future.  Keep it simple.</p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2010/01/clone-analysis-services-partitions-with-powershell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Trending SQL Server Agent Job Duration by Hour</title>
		<link>http://timlaqua.com/2009/12/trending-sql-server-agent-job-duration-by-hour/</link>
		<comments>http://timlaqua.com/2009/12/trending-sql-server-agent-job-duration-by-hour/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 03:07:38 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[agent job]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sql server 2008]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=302</guid>
		<description><![CDATA[Earlier today I noticed a SQL Server Agent job taking a little longer than usual (or what I thought was longer than usual). Let's face it, we're not staring at the Job Activity monitor all day, so unless you've written a report to monitor job run times - on occasion you ask yourself "is that [...]]]></description>
			<content:encoded><![CDATA[<p>Earlier today I noticed a SQL Server Agent job taking a little longer than usual (or what I thought was longer than usual).  Let's face it, we're not staring at the Job Activity monitor all day, so unless you've written a report to monitor job run times - on occasion you ask yourself "is that a normal run time for this thing?"  The job I was curious about happened to be a job that runs throughout the day and should only have real work to do once or twice an hour - and it should run roughly the same amount of time on any given business day for a given hour (i.e. at 1:00 PM on any given business day, this thing should do the same amount of work).</p>
<p>So I came up with the following query to PIVOT the run duration on the hour the job executed:<span id="more-302"></span></p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">DECLARE</span> @JobName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'Agent Job Name'</span>
&nbsp;
;WITH JobHistory <span style="color: #0000FF;">AS</span>
<span style="color: #808080;">&#40;</span>
  <span style="color: #0000FF;">SELECT</span>
     a.<span style="color: #202020;">run_date</span>
    ,a.<span style="color: #202020;">run_time</span> <span style="color: #808080;">/</span> <span style="color: #000;">10000</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">HOUR</span><span style="color: #808080;">&#93;</span>
    ,<span style="color: #808080;">&#40;</span>a.<span style="color: #202020;">run_duration</span> <span style="color: #808080;">/</span> <span style="color: #000;">10000</span> <span style="color: #808080;">*</span> <span style="color: #000;">60</span> <span style="color: #808080;">*</span> <span style="color: #000;">60</span>	<span style="color: #008080;">-- Hours</span>
     <span style="color: #808080;">+</span> a.<span style="color: #202020;">run_duration</span> <span style="color: #808080;">%</span> <span style="color: #000;">10000</span> <span style="color: #808080;">/</span> <span style="color: #000;">100</span> <span style="color: #808080;">*</span> <span style="color: #000;">60</span>	<span style="color: #008080;">-- Minutes</span>
     <span style="color: #808080;">+</span> a.<span style="color: #202020;">run_duration</span> <span style="color: #808080;">%</span> <span style="color: #000;">100</span>			<span style="color: #008080;">-- Seconds</span>
     <span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #000;">60.0</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>DurationMinutes<span style="color: #808080;">&#93;</span>
  <span style="color: #0000FF;">FROM</span>
    msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">sysjobhistory</span> a <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span>
    <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">sysjobs</span> b <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span>
    <span style="color: #0000FF;">ON</span> 
      a.<span style="color: #808080;">&#91;</span>job_id<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>job_id<span style="color: #808080;">&#93;</span>
      <span style="color: #808080;">AND</span> b.<span style="color: #808080;">&#91;</span>name<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> @JobName
      <span style="color: #808080;">AND</span> step_id <span style="color: #808080;">=</span> <span style="color: #000;">0</span>
      <span style="color: #808080;">AND</span> run_status <span style="color: #808080;">=</span> <span style="color: #000;">1</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span>
  JobHistory	
  PIVOT
  <span style="color: #808080;">&#40;</span> <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span>DurationMinutes<span style="color: #808080;">&#41;</span>
    <span style="color: #0000FF;">FOR</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">HOUR</span><span style="color: #808080;">&#93;</span> 
    <span style="color: #808080;">IN</span>  <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>00<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>01<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>02<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>03<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>04<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>05<span style="color: #808080;">&#93;</span>
        ,<span style="color: #808080;">&#91;</span>06<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>07<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>08<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span>09<span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">10</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">11</span><span style="color: #808080;">&#93;</span>
        ,<span style="color: #808080;">&#91;</span><span style="color: #000;">12</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">13</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">14</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">15</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">16</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">17</span><span style="color: #808080;">&#93;</span>
        ,<span style="color: #808080;">&#91;</span><span style="color: #000;">18</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">19</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">20</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">21</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">22</span><span style="color: #808080;">&#93;</span>,<span style="color: #808080;">&#91;</span><span style="color: #000;">23</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span>
  <span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> p</pre></div></div>

<p><em>UPDATE: Added <strong>AND run_status = 1</strong></em><br/></p>
<p>Then made a table (not sure why, but I can't resist doing the heatmap thing):<br />
<a href="http://timlaqua.com/wp-content/uploads/2009/12/agentJobDurationTable.png"><img src="http://timlaqua.com/wp-content/uploads/2009/12/agentJobDurationTable-300x52.png" alt="" title="agentJobDurationTable" width="300" height="52" class="alignnone size-medium wp-image-304" /></a><br/><em>click the image to enlarge</em></p>
<p>And, of course, the chart so you can point at a picture should you have to explain something to somebody in the future:<br />
<a href="http://timlaqua.com/wp-content/uploads/2009/12/agentJobDurationChart.png"><img src="http://timlaqua.com/wp-content/uploads/2009/12/agentJobDurationChart-300x146.png" alt="" title="agentJobDurationChart" width="300" height="146" class="alignnone size-medium wp-image-305" /></a><br/><em>click the image to enlarge</em></p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2009/12/trending-sql-server-agent-job-duration-by-hour/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Determining when RESTORE DATABASE command will complete (SQL Server 2008)</title>
		<link>http://timlaqua.com/2009/09/determining-when-restore-database-command-will-complete-sql-server-2008/</link>
		<comments>http://timlaqua.com/2009/09/determining-when-restore-database-command-will-complete-sql-server-2008/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 17:48:22 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[mssql]]></category>
		<category><![CDATA[RESTORE DATABASE]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sql server 2008]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=238</guid>
		<description><![CDATA[Ah, I see you just started restoring that 1TB monster and now everyone wants to know when it's going to be finished, where you're at in the process, etc. Fear not, Microsoft is very good at making up fictional numbers for us to use as rough estimates! I usually add 10-20% on top of these [...]]]></description>
			<content:encoded><![CDATA[<p>Ah, I see you just started restoring that 1TB monster and now everyone wants to know when it's going to be finished, where you're at in the process, etc.  Fear not, Microsoft is very good at making up fictional numbers for us to use as rough estimates!  I usually add 10-20% on top of these estimates just incase the database gremlins wander by to ruin your day again.  Or incase you encounter "storage issues."</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> 
	 percent_complete <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>PercentComplete<span style="color: #808080;">&#93;</span>
	,estimated_completion_time<span style="color: #808080;">/</span><span style="color: #000;">1000.0</span><span style="color: #808080;">/</span><span style="color: #000;">60.0</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>RemainingMinutes<span style="color: #808080;">&#93;</span>
	,total_elapsed_time<span style="color: #808080;">/</span><span style="color: #000;">1000.0</span><span style="color: #808080;">/</span><span style="color: #000;">60.0</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>ElapsedMinutes<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#40;</span>estimated_completion_time<span style="color: #808080;">+</span>total_elapsed_time<span style="color: #808080;">&#41;</span><span style="color: #808080;">/</span><span style="color: #000;">1000.0</span><span style="color: #808080;">/</span><span style="color: #000;">60.0</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>TotalMinutes<span style="color: #808080;">&#93;</span>
	,<span style="color: #FF00FF;">DATEADD</span><span style="color: #808080;">&#40;</span>MILLISECOND, estimated_completion_time, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>EstimatedTimeOfCompletion<span style="color: #808080;">&#93;</span>
	,st.<span style="color: #0000FF;">TEXT</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>CommandSQL<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">dm_exec_requests</span> r
	cross apply sys.<span style="color: #202020;">dm_exec_sql_text</span><span style="color: #808080;">&#40;</span>r.<span style="color: #202020;">sql_handle</span><span style="color: #808080;">&#41;</span> st
<span style="color: #0000FF;">WHERE</span> command <span style="color: #808080;">LIKE</span> <span style="color: #FF0000;">'%RESTORE DATABASE%'</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2009/09/determining-when-restore-database-command-will-complete-sql-server-2008/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Determining how long a database will be IN RECOVERY (SQL Server 2008)</title>
		<link>http://timlaqua.com/2009/09/determining-how-long-a-database-will-be-in-recovery-sql-server-2008/</link>
		<comments>http://timlaqua.com/2009/09/determining-how-long-a-database-will-be-in-recovery-sql-server-2008/#comments</comments>
		<pubDate>Fri, 25 Sep 2009 14:16:16 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[errorlog]]></category>
		<category><![CDATA[in recovery]]></category>
		<category><![CDATA[kill]]></category>
		<category><![CDATA[mssql]]></category>
		<category><![CDATA[rollback]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sql server 2008]]></category>
		<category><![CDATA[xp_readerrorlog]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=233</guid>
		<description><![CDATA[So, your MSSQL service crashed in the middle of a big transaction? Or you bumped the service while it was rolling back some gigantic schema change (like say a column add on a 800 million row table)? Well, as you prepare your resume in preparation for the fallout from this debockle, you can use the [...]]]></description>
			<content:encoded><![CDATA[<p>So, your MSSQL service crashed in the middle of a big transaction?  Or you bumped the service while it was rolling back some gigantic schema change (like say a column add on a 800 million row table)?  Well, as you prepare your resume in preparation for the fallout from this debockle, you can use the following query to see how much time you have left.  Or, I should say, how much time it thinks you have left... which seems to swing wildly up and down...  microsoft math ftw.</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;">&nbsp;
<span style="color: #0000FF;">DECLARE</span> @DBName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">64</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'databasename'</span>
&nbsp;
<span style="color: #0000FF;">DECLARE</span> @ErrorLog <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">TABLE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>LogDate<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">CHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">24</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#91;</span>ProcessInfo<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">64</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @ErrorLog
<span style="color: #0000FF;">EXEC</span> sys.<span style="color: #202020;">xp_readerrorlog</span> <span style="color: #000;">0</span>, <span style="color: #000;">1</span>, <span style="color: #FF0000;">'Recovery of database'</span>, @DBName
&nbsp;
<span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">TOP</span> <span style="color: #000;">5</span>
	 <span style="color: #808080;">&#91;</span>LogDate<span style="color: #808080;">&#93;</span>
	,<span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span>, <span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">') is '</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #000;">4</span>,<span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">' complete ('</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">-</span> <span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">') is '</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">-</span> <span style="color: #000;">4</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> PercentComplete
	,<span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span>, <span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'approximately'</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #000;">13</span>,<span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">' seconds remain'</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">-</span> <span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'approximately'</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">-</span> <span style="color: #000;">13</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">FLOAT</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">/</span><span style="color: #000;">60.0</span> <span style="color: #0000FF;">AS</span> MinutesRemaining
	,<span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span>, <span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'approximately'</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #000;">13</span>,<span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">' seconds remain'</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">-</span> <span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'approximately'</span>, <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">-</span> <span style="color: #000;">13</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">FLOAT</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">/</span><span style="color: #000;">60.0</span><span style="color: #808080;">/</span><span style="color: #000;">60.0</span> <span style="color: #0000FF;">AS</span> HoursRemaining
	,<span style="color: #808080;">&#91;</span><span style="color: #0000FF;">TEXT</span><span style="color: #808080;">&#93;</span>
&nbsp;
<span style="color: #0000FF;">FROM</span> @ErrorLog <span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> <span style="color: #808080;">&#91;</span>LogDate<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">DESC</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2009/09/determining-how-long-a-database-will-be-in-recovery-sql-server-2008/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Maintaining a Type 1 Slowly Changing Dimension (SCD) using T-SQL</title>
		<link>http://timlaqua.com/2009/05/maintaining-a-type-1-slowly-changing-dimension-scd-using-t-sql/</link>
		<comments>http://timlaqua.com/2009/05/maintaining-a-type-1-slowly-changing-dimension-scd-using-t-sql/#comments</comments>
		<pubDate>Sat, 23 May 2009 16:37:35 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[business intelligence]]></category>
		<category><![CDATA[data warehouse]]></category>
		<category><![CDATA[scd]]></category>
		<category><![CDATA[slowly changing dimension]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[ssis]]></category>
		<category><![CDATA[t-sql]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=224</guid>
		<description><![CDATA[A few days ago, one of our SSIS packages that maintained a Type 1 Slowly Changing Dimension (SCD) of about 1 million rows crept up to 15 minutes of runtime. Now this doesn't sound too bad, but this is part of our hourly batches, so 15 minutes is 25% of our entire processing window. The [...]]]></description>
			<content:encoded><![CDATA[<p>A few days ago, one of our SSIS packages that maintained a Type 1 Slowly Changing Dimension (SCD) of about 1 million rows crept up to 15 minutes of runtime.  Now this doesn't sound too bad, but this is part of our hourly batches, so 15 minutes is 25% of our entire processing window.  The package was using the Slowly Changing Dimension Wizard transformation - we were doing the standard OLEDB Source (which basically represented how the SCD "should" look) and then sending it to the SCD transform and letting it figure out what needed to be inserted and updated.  One option was to switch to lookups instead of the SCD wizard to speed things up, maybe even some fancy checksum voodoo for the updates (see <a href="http://blog.stevienova.com/2008/11/22/ssis-slowly-changing-dimensions-with-checksum/">http://blog.stevienova.com/2008/11/22/ssis-slowly-changing-dimensions-with-checksum/</a> for an example).  Then after thinking about it a little more - why are we sending a million rows down the pipeline every hour?  We know only a small percentage of these are new - and another small percentage needs to be updated.  Well, we can just write a quick SQL query to get us just those sets and the package would be much more efficient!  </p>
<p>Wait a tick - why would we give the rows to SSIS if all it is going to do insert one set and update the other?  Let's just do it all in T-SQL:<span id="more-224"></span></p>
<p><em>The following tables and dim are fictional - I just need to make up a star schema DIM to illustrate the approach</em></p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #Temp_SCDInserts <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>PartId<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">INT</span><span style="color: #808080;">&#41;</span>
&nbsp;
<span style="color: #008080;">-- INSERTS for new Parts</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> #Temp_SCDInserts
<span style="color: #0000FF;">SELECT</span>   
	<span style="color: #808080;">&#91;</span>PartId<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">FROM</span> 
	Part a <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span>
	<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> PartType b <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span> 
		<span style="color: #0000FF;">ON</span> a.<span style="color: #202020;">PartTypeId</span> <span style="color: #808080;">=</span> b.<span style="color: #202020;">PartTypeId</span>
	<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> PartSupplier c <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span> 
		<span style="color: #0000FF;">ON</span> a.<span style="color: #202020;">PartSupplierId</span> <span style="color: #808080;">=</span> c.<span style="color: #202020;">PartSupplierId</span>
	<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> PartSupplierCategory d <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span> 
		<span style="color: #0000FF;">ON</span> c.<span style="color: #202020;">PartSupplierCategoryId</span> <span style="color: #808080;">=</span> d.<span style="color: #202020;">PartSupplierCategoryId</span>
<span style="color: #0000FF;">EXCEPT</span>
<span style="color: #0000FF;">SELECT</span>
	<span style="color: #808080;">&#91;</span>PartId<span style="color: #808080;">&#93;</span>	
<span style="color: #0000FF;">FROM</span>
	DW.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">Dim_Part</span> <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span>
&nbsp;
<span style="color: #008080;">-- UPDATES</span>
<span style="color: #0000FF;">SELECT</span> 
	 dim.<span style="color: #808080;">&#91;</span>PartKeyId<span style="color: #808080;">&#93;</span>
	,a.<span style="color: #808080;">&#91;</span>PartName<span style="color: #808080;">&#93;</span>
	,b.<span style="color: #808080;">&#91;</span>PartTypeId<span style="color: #808080;">&#93;</span>
	,b.<span style="color: #808080;">&#91;</span>PartTypeName<span style="color: #808080;">&#93;</span>
	,c.<span style="color: #808080;">&#91;</span>PartSupplierId<span style="color: #808080;">&#93;</span>
	,c.<span style="color: #808080;">&#91;</span>PartSupplierName<span style="color: #808080;">&#93;</span>
	,d.<span style="color: #808080;">&#91;</span>PartSupplierCategoryId<span style="color: #808080;">&#93;</span>
	,d.<span style="color: #808080;">&#91;</span>PartSupplierCategoryName<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">INTO</span> #Temp_SCDUpdates
<span style="color: #0000FF;">FROM</span> DW.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">Dim_Part</span> dim <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span>
	<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> Part a <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span>
		<span style="color: #0000FF;">ON</span> dim.<span style="color: #202020;">PartId</span> <span style="color: #808080;">=</span> a.<span style="color: #202020;">PartId</span> <span style="color: #008080;">-- Business Key</span>
	<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> PartType b <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span> 
		<span style="color: #0000FF;">ON</span> a.<span style="color: #202020;">PartTypeId</span> <span style="color: #808080;">=</span> b.<span style="color: #202020;">PartTypeId</span>
	<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> PartSupplier c <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span> 
		<span style="color: #0000FF;">ON</span> a.<span style="color: #202020;">PartSupplierId</span> <span style="color: #808080;">=</span> c.<span style="color: #202020;">PartSupplierId</span>
	<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> PartSupplierCategory d <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span> 
		<span style="color: #0000FF;">ON</span> c.<span style="color: #202020;">PartSupplierCategoryId</span> <span style="color: #808080;">=</span> d.<span style="color: #202020;">PartSupplierCategoryId</span>
<span style="color: #0000FF;">WHERE</span>
	dim.<span style="color: #202020;">PartName</span> <span style="color: #808080;">&lt;&gt;</span> a.<span style="color: #202020;">PartName</span>
	<span style="color: #808080;">OR</span> dim.<span style="color: #202020;">PartTypeId</span> <span style="color: #808080;">&lt;&gt;</span> b.<span style="color: #202020;">PartTypeId</span>
	<span style="color: #808080;">OR</span> dim.<span style="color: #202020;">PartTypeName</span> <span style="color: #808080;">&lt;&gt;</span> b.<span style="color: #202020;">PartTypeName</span>
	<span style="color: #808080;">OR</span> dim.<span style="color: #202020;">PartSupplierId</span> <span style="color: #808080;">&lt;&gt;</span> c.<span style="color: #202020;">PartSupplierId</span>
	<span style="color: #808080;">OR</span> dim.<span style="color: #202020;">PartSupplierName</span> <span style="color: #808080;">&lt;&gt;</span> c.<span style="color: #202020;">PartSupplierName</span>
	<span style="color: #808080;">OR</span> dim.<span style="color: #202020;">PartSupplierCategoryId</span> <span style="color: #808080;">&lt;&gt;</span> d.<span style="color: #202020;">PartSupplierCategoryId</span>
	<span style="color: #808080;">OR</span> dim.<span style="color: #202020;">PartSupplierCategoryName</span>	<span style="color: #808080;">&lt;&gt;</span> d.<span style="color: #202020;">PartSupplierCategoryName</span>
&nbsp;
<span style="color: #008080;">-- INSERT new records</span>
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> <span style="color: #808080;">&#91;</span>DW<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Dim_Part<span style="color: #808080;">&#93;</span>
	<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>PartId<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#91;</span>PartName<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#91;</span>PartTypeId<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#91;</span>PartTypeName<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#91;</span>PartSupplierId<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#91;</span>PartSupplierName<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#91;</span>PartSupplierCategoryId<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#91;</span>PartSupplierCategoryName<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> 
	 a.<span style="color: #808080;">&#91;</span>PartId<span style="color: #808080;">&#93;</span>
	,a.<span style="color: #808080;">&#91;</span>PartName<span style="color: #808080;">&#93;</span>
	,b.<span style="color: #808080;">&#91;</span>PartTypeId<span style="color: #808080;">&#93;</span>
	,b.<span style="color: #808080;">&#91;</span>PartTypeName<span style="color: #808080;">&#93;</span>
	,c.<span style="color: #808080;">&#91;</span>PartSupplierId<span style="color: #808080;">&#93;</span>
	,c.<span style="color: #808080;">&#91;</span>PartSupplierName<span style="color: #808080;">&#93;</span>
	,d.<span style="color: #808080;">&#91;</span>PartSupplierCategoryId<span style="color: #808080;">&#93;</span>
	,d.<span style="color: #808080;">&#91;</span>PartSupplierCategoryName<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">FROM</span> #Temp_SCDInserts i
	<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> Part a <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span>
		<span style="color: #0000FF;">ON</span> i.<span style="color: #202020;">PartId</span> <span style="color: #808080;">=</span> a.<span style="color: #202020;">PartId</span> <span style="color: #008080;">-- Business Key</span>
	<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> PartType b <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span> 
		<span style="color: #0000FF;">ON</span> a.<span style="color: #202020;">PartTypeId</span> <span style="color: #808080;">=</span> b.<span style="color: #202020;">PartTypeId</span>
	<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> PartSupplier c <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span> 
		<span style="color: #0000FF;">ON</span> a.<span style="color: #202020;">PartSupplierId</span> <span style="color: #808080;">=</span> c.<span style="color: #202020;">PartSupplierId</span>
	<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> PartSupplierCategory d <span style="color: #0000FF;">WITH</span><span style="color: #808080;">&#40;</span>NOLOCK<span style="color: #808080;">&#41;</span> 
		<span style="color: #0000FF;">ON</span> c.<span style="color: #202020;">PartSupplierCategoryId</span> <span style="color: #808080;">=</span> d.<span style="color: #202020;">PartSupplierCategoryId</span>
&nbsp;
<span style="color: #008080;">-- UPDATE existing records</span>
<span style="color: #0000FF;">UPDATE</span> <span style="color: #808080;">&#91;</span>DW<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Dim_Part<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">SET</span>
	 <span style="color: #808080;">&#91;</span>PartName<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>PartName<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#91;</span>PartTypeId<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>PartTypeId<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#91;</span>PartTypeName<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>PartTypeName<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#91;</span>PartSupplierId<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>PartSupplierId<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#91;</span>PartSupplierName<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>PartSupplierName<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#91;</span>PartSupplierCategoryId<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>PartSupplierCategoryId<span style="color: #808080;">&#93;</span>
	,<span style="color: #808080;">&#91;</span>PartSupplierCategoryName<span style="color: #808080;">&#93;</span> <span style="color: #808080;">=</span> b.<span style="color: #808080;">&#91;</span>PartSupplierCategoryName<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">FROM</span> 
	<span style="color: #808080;">&#91;</span>DW<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Dim_Part<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> a
	<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> #Temp_SCDUpdates <span style="color: #0000FF;">AS</span> b 
		<span style="color: #0000FF;">ON</span> a.<span style="color: #202020;">PartKeyId</span> <span style="color: #808080;">=</span> b.<span style="color: #202020;">PartKeyId</span>  <span style="color: #008080;">-- Join on Business Keys or Surrogate Key</span>
&nbsp;
&nbsp;
<span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #Temp_SCDInserts
<span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #Temp_SCDUpdates</pre></div></div>

<p>The results?  Our 15 minute SSIS package has been replaced with a few lines of T-SQL and it now runs in less than 90 seconds.  Nice.</p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2009/05/maintaining-a-type-1-slowly-changing-dimension-scd-using-t-sql/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Parsing QueryString (GET) Variables From a URL using T-SQL</title>
		<link>http://timlaqua.com/2009/05/parsing-querystring-get-variables-from-a-url-using-t-sql/</link>
		<comments>http://timlaqua.com/2009/05/parsing-querystring-get-variables-from-a-url-using-t-sql/#comments</comments>
		<pubDate>Sat, 23 May 2009 14:59:33 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[string parsing]]></category>
		<category><![CDATA[t-sql]]></category>
		<category><![CDATA[table valued function]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=216</guid>
		<description><![CDATA[First of all - I know, don't do this. The application that put the URL in the column in the first place is MUCH better at handling URLs and ideally, you would just add columns for the GET variables you're after and have the application put those in. Parsing them out after the fact from [...]]]></description>
			<content:encoded><![CDATA[<p>First of all - I know, don't do this.  The application that put the URL in the column in the first place is MUCH better at handling URLs and ideally, you would just add columns for the GET variables you're after and have the application put those in.  Parsing them out after the fact from a VARCHAR field is insane.</p>
<p>So now we're here and we need to do that thing that we said we shouldn't do.  My approach is to use a Table Valued Function that only returns one column - the value of the variable or NULL if it can't find the variable in the querystring.<span id="more-216"></span></p>
<p><em>Example</em></p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span>
	a.<span style="color: #808080;">&#91;</span>Id<span style="color: #808080;">&#93;</span>,
	b.<span style="color: #808080;">&#91;</span>GetVariableValue<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Foo<span style="color: #808080;">&#93;</span>,
	c.<span style="color: #808080;">&#91;</span>GetVariableValue<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>Bar<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">FROM</span>
	<span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>SourceTable<span style="color: #808080;">&#93;</span> a
	<span style="color: #808080;">CROSS</span> APPLY <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Utility_UrlParseGetVariable_F01<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>URL<span style="color: #808080;">&#93;</span>, <span style="color: #FF0000;">'foo'</span>, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> b
	<span style="color: #808080;">CROSS</span> APPLY <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Utility_UrlParseGetVariable_F01<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>URL<span style="color: #808080;">&#93;</span>, <span style="color: #FF0000;">'bar'</span>, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span> c</pre></div></div>

<p><em>Table Valued Function</em></p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">/*************************************************************************************
***
*** Procedure:  		[Utility_UrlParseGetVariable_F01]
*** Purpose:		    Attempts to parse a given GET variable out of a URL string
***				
***			
*** Author:		      tl
*** Date Created:	  2009-05-22
*** 
*** Revision History
*** Date		Author			Description
*** 2009-05-22	tl				Created
*************************************************************************************/</span>
<span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">FUNCTION</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>Utility_UrlParseGetVariable_F01<span style="color: #808080;">&#93;</span> 
<span style="color: #808080;">&#40;</span>
	@Url <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">2048</span><span style="color: #808080;">&#41;</span>,
	@VariableName <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">RETURNS</span> @GetVariable <span style="color: #0000FF;">TABLE</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>GetVariableValue<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">AS</span>
<span style="color: #0000FF;">BEGIN</span>
&nbsp;
	<span style="color: #0000FF;">DECLARE</span> @<span style="color: #FF00FF;">SUBSTRING</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">2048</span><span style="color: #808080;">&#41;</span>
	<span style="color: #0000FF;">DECLARE</span> @variableValue <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span>
&nbsp;
	<span style="color: #0000FF;">SET</span> @VariableName <span style="color: #808080;">=</span> @VariableName <span style="color: #808080;">+</span> <span style="color: #FF0000;">'='</span>
&nbsp;
	<span style="color: #008080;">-- Case Insensitive - munge both inputs</span>
	<span style="color: #0000FF;">SET</span> @VariableName <span style="color: #808080;">=</span> <span style="color: #FF00FF;">LOWER</span><span style="color: #808080;">&#40;</span>@VariableName<span style="color: #808080;">&#41;</span>
	<span style="color: #0000FF;">SET</span> @Url <span style="color: #808080;">=</span> <span style="color: #FF00FF;">LOWER</span><span style="color: #808080;">&#40;</span>@Url<span style="color: #808080;">&#41;</span>
&nbsp;
	<span style="color: #0000FF;">IF</span> <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span>@VariableName,@Url<span style="color: #808080;">&#41;</span> <span style="color: #808080;">&gt;</span> <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>
	<span style="color: #0000FF;">BEGIN</span>
		<span style="color: #0000FF;">SET</span> @<span style="color: #FF00FF;">SUBSTRING</span> <span style="color: #808080;">=</span> <span style="color: #0000FF;">RIGHT</span><span style="color: #808080;">&#40;</span>@Url, <span style="color: #FF00FF;">LEN</span><span style="color: #808080;">&#40;</span>@Url<span style="color: #808080;">&#41;</span> <span style="color: #808080;">-</span> <span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span>@VariableName,@Url<span style="color: #808080;">&#41;</span> <span style="color: #808080;">-</span> <span style="color: #FF00FF;">LEN</span><span style="color: #808080;">&#40;</span>@VariableName<span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>
		<span style="color: #0000FF;">IF</span> <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'&amp;'</span>, @<span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">&gt;</span> <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>
			<span style="color: #0000FF;">SET</span> @variableValue <span style="color: #808080;">=</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">LEFT</span><span style="color: #808080;">&#40;</span>@<span style="color: #FF00FF;">SUBSTRING</span>, <span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'&amp;'</span>, @<span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">-</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
		<span style="color: #0000FF;">ELSE</span>
			<span style="color: #0000FF;">SET</span> @variableValue <span style="color: #808080;">=</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>@<span style="color: #FF00FF;">SUBSTRING</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">255</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>
&nbsp;
		<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @GetVariable <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>GetVariableValue<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span>@variableValue<span style="color: #808080;">&#41;</span>
	<span style="color: #0000FF;">END</span>
	<span style="color: #0000FF;">ELSE</span>
		<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @GetVariable <span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>GetVariableValue<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">NULL</span><span style="color: #808080;">&#41;</span>
&nbsp;
	<span style="color: #0000FF;">RETURN</span>
<span style="color: #0000FF;">END</span></pre></div></div>

<p><em>Issues</em></p>
<ul>
<li>The current TVF munges everything to lower case - the return is lower case as well.  This works fine for me as I was parsing out INT values anyway (so I use ISNULL(...,0) inside a cast) for the actual value.</li>
<li>The way it's written, if you look for the variable 'bar' and the querystring actually has a variable called 'foobar' before it - this function will take the value of 'foobar' and return it (because it's just doing a CHARINDEX for 'bar=' in the querystring</li>
</ul>
<p><em>Why not RegEx via CLR?</em><br />
Because one of the DBAs asked me not to <img src='http://timlaqua.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />   RegEx via CLR is a cleaner and more accurate way to do it.  Better yet, you could use CLR and create a Uri object, then use <code>HttpUtility.ParseQueryString(uri.Query).Name('bar')</code> to get the appropriate value.</p>
]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2009/05/parsing-querystring-get-variables-from-a-url-using-t-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quick Analysis of Cached Query Plans in SQL Server 2005</title>
		<link>http://timlaqua.com/2009/05/quick-analysis-of-cached-query-plans-in-sql-server-2005/</link>
		<comments>http://timlaqua.com/2009/05/quick-analysis-of-cached-query-plans-in-sql-server-2005/#comments</comments>
		<pubDate>Fri, 15 May 2009 21:55:03 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Scripts & Code]]></category>
		<category><![CDATA[bi]]></category>
		<category><![CDATA[business intelligence]]></category>
		<category><![CDATA[hash match]]></category>
		<category><![CDATA[merge join]]></category>
		<category><![CDATA[nested loop]]></category>
		<category><![CDATA[query plan]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[statistics]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://timlaqua.com/?p=207</guid>
		<description><![CDATA[I run this one pretty frequently when we need to figure out what procs are killing a complex ETL process and what exactly about them is making the server cry. So basically, if it's on the development server, I'll do a DBCC FREEPROCCACHE and a DBCC DROPCLEANBUFFERS, run the entire set of ETLs, then run [...]]]></description>
			<content:encoded><![CDATA[<p>I run this one pretty frequently when we need to figure out what procs are killing a complex ETL process and what exactly about them is making the server cry.  So basically, if it's on the development server, I'll do a <code>DBCC FREEPROCCACHE</code> and a <code>DBCC DROPCLEANBUFFERS</code>, run the entire set of ETLs, then run this query and then dig deeper in to the query plans that look suspect (high *Scan counts usually, sometimes lots of Hash Matches or Merge Joins).  On a production server, the clearing of the proc cache and dropcleanbuffers can be problematic so I'll often just run the query after a scheduled ETL run.  If you want to see the query plans mapped out visually, click on the query_plan value and SSMS will open up the XML.  Then save that XML file as a .sqlplan file.  Once you have that, close the XML and then open the .sqlplan file.<span id="more-207"></span></p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span>
	 DatabaseName
	,ObjectName
	,<span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>query_plan.<span style="color: #0000FF;">VALUE</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'
		declare namespace QP=&quot;http://schemas.microsoft.com/sqlserver/2004/07/showplan&quot;;   
		sum((/QP:ShowPlanXML/QP:BatchSequence/QP:Batch/QP:Statements/QP:StmtSimple/QP:QueryPlan/@CompileTime))'</span>,<span style="color: #FF0000;">'NVARCHAR(MAX)'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">FLOAT</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">/</span><span style="color: #000;">1000.0</span><span style="color: #808080;">/</span><span style="color: #000;">60.0</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>CompileTime<span style="color: #808080;">&#93;</span>
	,query_plan
	,query_plan.<span style="color: #0000FF;">VALUE</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'
		declare namespace QP=&quot;http://schemas.microsoft.com/sqlserver/2004/07/showplan&quot;;   
		count((//QP:ColumnsWithNoStatistics))'</span>,<span style="color: #FF0000;">'INT'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>ColumnsWithNoStatistics<span style="color: #808080;">&#93;</span>
	,query_plan.<span style="color: #0000FF;">VALUE</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'
		declare namespace QP=&quot;http://schemas.microsoft.com/sqlserver/2004/07/showplan&quot;;   
		count((//QP:TableScan))'</span>,<span style="color: #FF0000;">'INT'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>TableScan<span style="color: #808080;">&#93;</span>
	,query_plan.<span style="color: #0000FF;">VALUE</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'
		declare namespace QP=&quot;http://schemas.microsoft.com/sqlserver/2004/07/showplan&quot;;   
		count((//QP:IndexScan))'</span>,<span style="color: #FF0000;">'INT'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>IndexScan<span style="color: #808080;">&#93;</span>
	,query_plan.<span style="color: #0000FF;">VALUE</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'
		declare namespace QP=&quot;http://schemas.microsoft.com/sqlserver/2004/07/showplan&quot;;   
		count((//QP:RelOp[@PhysicalOp=&quot;Nested Loops&quot;]))'</span>,<span style="color: #FF0000;">'INT'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>NestedLoop<span style="color: #808080;">&#93;</span>
	,query_plan.<span style="color: #0000FF;">VALUE</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'
		declare namespace QP=&quot;http://schemas.microsoft.com/sqlserver/2004/07/showplan&quot;;   
		count((//QP:RelOp[@PhysicalOp=&quot;Hash Match&quot;]))'</span>,<span style="color: #FF0000;">'INT'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>HashMatch<span style="color: #808080;">&#93;</span>
	,query_plan.<span style="color: #0000FF;">VALUE</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'
		declare namespace QP=&quot;http://schemas.microsoft.com/sqlserver/2004/07/showplan&quot;;   
		count((//QP:RelOp[@PhysicalOp=&quot;Merge Join&quot;]))'</span>,<span style="color: #FF0000;">'INT'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>MergeJoin<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">FROM</span> 
	<span style="color: #808080;">&#40;</span>	
		<span style="color: #0000FF;">SELECT</span>
			<span style="color: #FF00FF;">OBJECT_NAME</span><span style="color: #808080;">&#40;</span>st.<span style="color: #202020;">objectid</span>,st.<span style="color: #202020;">dbid</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> ObjectName,
			<span style="color: #FF00FF;">DB_NAME</span><span style="color: #808080;">&#40;</span>st.<span style="color: #202020;">dbid</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> DatabaseName,
			st.<span style="color: #202020;">dbid</span>,
			st.<span style="color: #202020;">objectid</span>,
			qs.<span style="color: #202020;">plan_handle</span>
		<span style="color: #0000FF;">FROM</span> 
			sys.<span style="color: #202020;">dm_exec_query_stats</span> <span style="color: #0000FF;">AS</span> qs
			<span style="color: #808080;">CROSS</span> APPLY sys.<span style="color: #202020;">dm_exec_sql_text</span><span style="color: #808080;">&#40;</span>qs.<span style="color: #202020;">sql_handle</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> st
		<span style="color: #0000FF;">WHERE</span>
			ObjectId <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
		<span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span>
			st.<span style="color: #202020;">dbid</span>,
			st.<span style="color: #202020;">objectid</span>,
			qs.<span style="color: #202020;">plan_handle</span>
	<span style="color: #808080;">&#41;</span> a
	<span style="color: #808080;">CROSS</span> APPLY sys.<span style="color: #202020;">dm_exec_query_plan</span><span style="color: #808080;">&#40;</span>plan_handle<span style="color: #808080;">&#41;</span> qp
<span style="color: #0000FF;">WHERE</span> DatabaseName <span style="color: #808080;">=</span> <span style="color: #FF0000;">'NameOfDatabase'</span>
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> DatabaseName, ObjectName</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://timlaqua.com/2009/05/quick-analysis-of-cached-query-plans-in-sql-server-2005/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
