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

<channel>
	<title>Shepherding Code</title>
	<atom:link href="http://ginganinja.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://ginganinja.wordpress.com</link>
	<description>Coding musings of Dave Arkell</description>
	<lastBuildDate>Fri, 11 Sep 2009 13:55:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='ginganinja.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Shepherding Code</title>
		<link>http://ginganinja.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://ginganinja.wordpress.com/osd.xml" title="Shepherding Code" />
	<atom:link rel='hub' href='http://ginganinja.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Sql Server Backup Script</title>
		<link>http://ginganinja.wordpress.com/2009/09/11/sql-server-backup-script/</link>
		<comments>http://ginganinja.wordpress.com/2009/09/11/sql-server-backup-script/#comments</comments>
		<pubDate>Fri, 11 Sep 2009 11:05:51 +0000</pubDate>
		<dc:creator>ginganinja</dc:creator>
				<category><![CDATA[Scripts]]></category>

		<guid isPermaLink="false">http://ginganinja.wordpress.com/2009/09/11/sql-server-backup-script/</guid>
		<description><![CDATA[Well, hello, this is my first ‘real’ post.  Hopefully there’ll be more.. A problem I came up against a while ago was with trying to copy a database from an old development sql server to a new one, but being able to change the table structure of the original one, and still be able to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ginganinja.wordpress.com&amp;blog=6837789&amp;post=8&amp;subd=ginganinja&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Well, hello, this is my first ‘real’ post.  Hopefully there’ll be more..</p>
<p>A problem I came up against a while ago was with trying to copy a database from an old development sql server to a new one, but being able to change the table structure of the original one, and still be able to copy it all across.</p>
<p>I created a script that could adjust to changes on the database structure, and use bcp (bulk copy) so that it would be nice and speedy. The bcp script will remember the id’s that are on the source database so that it all works correctly.</p>
<p>I decided to use Powershell because I have been looking into it a bit, and it’s already on Vista.</p>
<p>So here is the main Powershell script:</p>
<div id="codeSnippetWrapper" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:'Courier New', courier, monospace;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;cursor:text;border:silver 1px solid;margin:20px 0 10px;padding:4px;">
<div id="codeSnippet" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum1" style="color:#606060;">   1:</span> $destServer = <span style="color:#006080;">"DESTINATION SERVER"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum2" style="color:#606060;">   2:</span> $sourceServer = <span style="color:#006080;">"SOURCE SERVER"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum3" style="color:#606060;">   3:</span> $destDBase = <span style="color:#006080;">"DESTINATION DATABASE"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum4" style="color:#606060;">   4:</span> $sourceDBase = <span style="color:#006080;">"SOURCE DATABASE"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum5" style="color:#606060;">   5:</span> $ScriptsPath = <span style="color:#006080;">"SCRIPTS LOCATION"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum6" style="color:#606060;">   6:</span> $ExportLocation = <span style="color:#006080;">"EXPORTED FILES LOCATION"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum7" style="color:#606060;">   7:</span> $NumberofTables = 31 <span style="color:#008000;"># The number of tables in you database</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum8" style="color:#606060;">   8:</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum9" style="color:#606060;">   9:</span> cd <span style="color:#006080;">'c:\Program Files\Microsoft Sql Server\90\Tools\Binn'</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum10" style="color:#606060;">  10:</span> <span style="color:#008000;"># All this recreates the database</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum11" style="color:#606060;">  11:</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum12" style="color:#606060;">  12:</span> SqlCmd -S $destServer -E -i <span style="color:#006080;">"$ScriptsPath\DetachDBase.sql"</span> -v dbasename = $destDBase</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum13" style="color:#606060;">  13:</span> SqlCmd -S $destServer -E -i <span style="color:#006080;">"$ScriptsPath\reattachDBase.sql"</span> -v dbasename = $destDBase</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum14" style="color:#606060;">  14:</span> SqlCmd -S $destServer -E -i <span style="color:#006080;">"$ScriptsPath\DropDBase.sql"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum15" style="color:#606060;">  15:</span> SqlCmd -S $destServer -E -i <span style="color:#006080;">"$ScriptsPath\CreateDBase.sql"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum16" style="color:#606060;">  16:</span> <span style="color:#008000;"># This is generated by Sql Server Management Studio using the 'Generate Scripts' facility</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum17" style="color:#606060;">  17:</span> SqlCmd -S $destServer -E -d $destDBase -i <span style="color:#006080;">"$ScriptsPath\CreateTablesAndSPs.sql"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum18" style="color:#606060;">  18:</span> Sqlcmd -S $sourceServer -E -d $sourceDBase -i <span style="color:#006080;">"$ScriptsPath\CreateImportExportScript.sql"</span> -v dbasename = $sourceDBase ExportLocation = $ExportLocation serverName = $sourceServer -o <span style="color:#006080;">"$ScriptsPath\ExportScript.bat"</span> -w 250 -h -1 -k 1 <span style="color:#008000;"># specifying width stops the commands going onto separate lines</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum19" style="color:#606060;">  19:</span> <span style="color:#008000;"># Couldn't work out how to filter any number of rows affected (that is - the number of tables in the database)</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum20" style="color:#606060;">  20:</span> <span style="color:#008000;"># I replace ..User with ..[User] because I stupidly had a user table in my database</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum21" style="color:#606060;">  21:</span> <span style="color:#008000;"># The last replace removes extra spaces</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum22" style="color:#606060;">  22:</span> (Get-Content <span style="color:#006080;">"$ScriptsPath\ExportScript.bat"</span>) | where {$_ <span style="color:#cc6633;">-ne</span> <span style="color:#006080;">"($NumberOfTables rows affected)"</span>} | Foreach-Object {$_ -replace <span style="color:#006080;">"..User "</span>, <span style="color:#006080;">"..[User] "</span>} | Foreach-Object {$_ -replace <span style="color:#006080;">"  "</span>, <span style="color:#006080;">""</span>} | Set-Content <span style="color:#006080;">"$ScriptsPath\ExportScript.bat"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum23" style="color:#606060;">  23:</span> Sqlcmd -S $sourceServer -E -d $sourceDBase -i <span style="color:#006080;">"$ScriptsPath\CreateImportExportScript.sql"</span> -v dbasename = $destDBase ExportLocation = $ExportLocation serverName = $destServer -o <span style="color:#006080;">"$ScriptsPath\ImportScript.bat"</span> -w 250 -h -1 -k 1</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum24" style="color:#606060;">  24:</span> (Get-Content <span style="color:#006080;">"$ScriptsPath\ImportScript.bat"</span>) | where {$_ <span style="color:#cc6633;">-ne</span> <span style="color:#006080;">"($NumberOfTables rows affected)"</span>} | Foreach-Object {$_ -replace <span style="color:#006080;">"..User "</span>, <span style="color:#006080;">"..[User] "</span>} | Foreach-Object {$_ -replace <span style="color:#006080;">"  "</span>, <span style="color:#006080;">""</span>} | Set-Content <span style="color:#006080;">"$ScriptsPath\ImportScript.bat"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum25" style="color:#606060;">  25:</span> cd <span style="color:#006080;">"$ScriptsPath"</span></pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum26" style="color:#606060;">  26:</span> .\ExportScript.bat</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum27" style="color:#606060;">  27:</span> .\ImportScript.bat</pre>
<p><!--CRLF--></div>
</div>
<p>The script references a number of other scripts.  The first 5 are created using Sql Server Management Studio.  Note these scripts are for the destination databse.  So we have a detach, reattach, drop, and create script.  The dettach and reattach ensure that there are no other connections to the database.</p>
<p>The 5th script is created by right-clicking on the <strong>source </strong>database in Sql Server Management Studio, and selecting ‘Tasks’ –&gt; ‘Generate Scripts’.</p>
<p>Unfortunately I do not know how to generate these scripts automatically from the command line – anyone got any ideas to make the script (almost) perfect?</p>
<p>There are is one other script that does the ‘dirty’ work.  CreateImportExportScript.sql:</p>
<div id="codeSnippetWrapper" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:97.5%;">
<div id="codeSnippet" style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:97.5%;font-family:'Courier New', courier, monospace;direction:ltr;max-height:200px;font-size:8pt;overflow:auto;cursor:text;border:silver 1px solid;margin:20px 0 10px;padding:4px;">
<pre style="text-align:left;line-height:12pt;background-color:white;width:100%;"><span id="lnum1" style="color:#606060;">   1:</span> <span style="color:#0000ff;">select</span> <span style="color:#006080;">'"c:\Program Files\Microsoft Sql Server\90\Tools\binn\bcp.exe" $(dbasename)..'</span>+name+<span style="color:#006080;">' out "$(ExportLocation)'</span>+name+<span style="color:#006080;">'.tab" -n -S $(serverName) -E -T'</span> <span style="color:#0000ff;">from</span> sysObjects</pre>
<p><!--CRLF--></p>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span id="lnum2" style="color:#606060;">   2:</span> <span style="color:#0000ff;">where</span> xtype = <span style="color:#006080;">'U'</span></pre>
<p><!--CRLF--></div>
</div>
<p>So to summarise what the powershell script does:</p>
<p>Dettach, reattach, drop, and recreate the database, and then generate the tables and stored procedures. Then generate import and export scripts.  Before using them, the script cleans up the scripts so that they will work (removing the number of lines returned and excess spaces).</p>
<p>I think that this is probably the quickest way of getting a database copy up and running from an existing database.</p>
<p>Enjoy!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ginganinja.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ginganinja.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ginganinja.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ginganinja.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ginganinja.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ginganinja.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ginganinja.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ginganinja.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ginganinja.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ginganinja.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ginganinja.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ginganinja.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ginganinja.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ginganinja.wordpress.com/8/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ginganinja.wordpress.com&amp;blog=6837789&amp;post=8&amp;subd=ginganinja&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ginganinja.wordpress.com/2009/09/11/sql-server-backup-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/970e3daa6f32f6a6fd7c5ce2fec9618d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ginganinja</media:title>
		</media:content>
	</item>
		<item>
		<title>Hello world!</title>
		<link>http://ginganinja.wordpress.com/2009/03/05/hello-world/</link>
		<comments>http://ginganinja.wordpress.com/2009/03/05/hello-world/#comments</comments>
		<pubDate>Thu, 05 Mar 2009 12:52:58 +0000</pubDate>
		<dc:creator>ginganinja</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Good day to you who happens to be looking! First post, just to give an idea of what I intend to post on here. My current big things are iPhone, Silverlight, and .net development, but maybe I’ll put some other stuff on here so non-geeks don’t get bored. We shall see…<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ginganinja.wordpress.com&amp;blog=6837789&amp;post=1&amp;subd=ginganinja&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Good day to you who happens to be looking!  First post, just to give an idea of what I intend to post on here.  My current big things are iPhone, Silverlight, and .net development, but maybe I’ll put some other stuff on here so non-geeks don’t get bored.  We shall see…</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ginganinja.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ginganinja.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ginganinja.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ginganinja.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ginganinja.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ginganinja.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ginganinja.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ginganinja.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ginganinja.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ginganinja.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ginganinja.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ginganinja.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ginganinja.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ginganinja.wordpress.com/1/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ginganinja.wordpress.com&amp;blog=6837789&amp;post=1&amp;subd=ginganinja&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ginganinja.wordpress.com/2009/03/05/hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/970e3daa6f32f6a6fd7c5ce2fec9618d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ginganinja</media:title>
		</media:content>
	</item>
	</channel>
</rss>
