<?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; restore</title>
	<atom:link href="http://timlaqua.com/tag/restore/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>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>
	</channel>
</rss>
