{"id":697,"date":"2004-09-02T06:37:19","date_gmt":"2004-09-02T06:37:19","guid":{"rendered":"\/blogs\/kimberly\/post\/SQL-Server-2005-Management-Studio-Scripting-Option-and-SQLCMD-(loving-it!).aspx"},"modified":"2013-01-02T07:07:08","modified_gmt":"2013-01-02T15:07:08","slug":"sql-server-2005-management-studio-scripting-option-and-sqlcmd-loving-it","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/sql-server-2005-management-studio-scripting-option-and-sqlcmd-loving-it\/","title":{"rendered":"SQL Server 2005 &#8211; Management Studio Scripting Option and SQLCMD (loving it!)"},"content":{"rendered":"<p><P class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><FONT color=#000000>One of my favorite features of Management Studio is the ability to &#8220;script&#8221; a step rather than execute it. Within SQL Management Studio, each dialog has 4 different options from which to choose (these are including the typical OK\/Cancel) but also add a couple more at the top of the dialog:<?xml:namespace prefix = o ns = \"urn:schemas-microsoft-com:office:office\" \/><o:p><\/o:p><\/FONT><\/SPAN><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><o:p><FONT face=Arial color=#000000>&nbsp;<\/FONT><\/o:p><\/SPAN><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><FONT face=Arial><FONT color=#000000>At the top of the dialog<o:p><\/o:p><\/FONT><\/FONT><\/SPAN><\/P><br \/>\n<UL style=\"MARGIN-TOP: 0in\" type=disc><br \/>\n<LI class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none; mso-list: l0 level1 lfo1; tab-stops: list .5in\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><FONT face=Arial><FONT color=#000000>Schedule &#8211; which will create an Agent job.<o:p><\/o:p><\/FONT><\/FONT><\/SPAN><br \/>\n<LI class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none; mso-list: l0 level1 lfo1; tab-stops: list .5in\"><FONT face=Arial><FONT color=#000000><B style=\"mso-bidi-font-weight: normal\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">Script <\/SPAN><\/B><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">&#8211; which will send the command(s) to a new Query Window in Management Studio<o:p><\/o:p><\/SPAN><\/FONT><\/FONT><\/LI><\/UL><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><FONT face=Arial><FONT color=#000000>At the bottom of the dialog (the norm):<o:p><\/o:p><\/FONT><\/FONT><\/SPAN><\/P><br \/>\n<UL style=\"MARGIN-TOP: 0in\" type=disc><br \/>\n<LI class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none; mso-list: l0 level1 lfo1; tab-stops: list .5in\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><FONT face=Arial><FONT color=#000000>Run it right now &#8211; with the OK button<o:p><\/o:p><\/FONT><\/FONT><\/SPAN><br \/>\n<LI class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none; mso-list: l0 level1 lfo1; tab-stops: list .5in\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><FONT face=Arial><FONT color=#000000>Cancel it &#8211; fairly obvious, eh?<o:p><\/o:p><\/FONT><\/FONT><\/SPAN><\/LI><\/UL><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><o:p><FONT face=Arial color=#000000><\/FONT><\/o:p><\/SPAN><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><FONT face=Arial><FONT color=#000000>The &#8220;Script&#8221; option has to be one of my favorites. You can learn from this dialog, you can cut\/paste and help to build more complex scripts, you can just see what the heck the UI was going to do, etc&#8230; and well, that&#8217;s what led to SQLCMD. Something I wanted to do (from just a practical perspective) was create a simple\/fast way to connect to my existing SQL Server 2000 machine, backup a database and then restore it to my SQL Server 2005 machine. One of the benefits of backup\/restore is that SQL Server will upgrade the database on restore (no special settings required &#8211; this is default behavior if you restore a SQL Server 2000 backup to SQL Server 2005). Although finding the correct syntax for each step is easy through the UI, I wanted to take the overall process one-step further and create a SQLCMD-specific script to automate it (<EM>yes, I kind of like the idea of a command line tools for batch\/automation, etc.. and yes,&nbsp;I could have also done this with DTS or the SQL Agent&#8230; good idea for another blog entry! Hey&nbsp;guys (and you know who you are!) you should do the comparable DTS\/Agent jobs?! I&#8217;ll be waiting. <\/EM><SPAN style=\"FONT-SIZE: 10pt; FONT-FAMILY: Wingdings; mso-bidi-font-family: Arial; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-char-type: symbol; mso-symbol-font-family: Wingdings\"><SPAN style=\"mso-char-type: symbol; mso-symbol-font-family: Wingdings\">J <\/FONT><\/FONT><SPAN style=\"FONT-SIZE: 10pt; FONT-FAMILY: Wingdings; mso-bidi-font-family: Arial; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-char-type: symbol; mso-symbol-font-family: Wingdings\"><SPAN style=\"mso-char-type: symbol; mso-symbol-font-family: Wingdings\"><FONT color=#000000>J <SPAN style=\"FONT-SIZE: 10pt; FONT-FAMILY: Wingdings; mso-bidi-font-family: Arial; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-char-type: symbol; mso-symbol-font-family: Wingdings\"><SPAN style=\"mso-char-type: symbol; mso-symbol-font-family: Wingdings\">J<\/SPAN><\/SPAN><\/FONT><\/SPAN><\/SPAN><\/SPAN><\/SPAN><FONT face=Arial><FONT color=#000000>). <\/FONT><\/FONT><\/SPAN><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><FONT face=Arial><FONT color=#000000><\/FONT><\/FONT><\/SPAN>&nbsp;<\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><FONT face=Arial><FONT color=#000000>Anyway, the primary goal is to leverage some of the new capabilities of SQLCMD (which I have to admit &#8211; if you&#8217;re a command line\/automation person &#8211; this is going to <B style=\"mso-bidi-font-weight: normal\">quickly<\/B> become your favorite new features). <SPAN style=\"mso-spacerun: yes\">&nbsp;<\/SPAN>Here&#8217;s a very quick summary of SQLCMD:<o:p><\/o:p><\/FONT><\/FONT><\/SPAN><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><o:p><FONT face=Arial color=#000000>&nbsp;<\/FONT><\/o:p><\/SPAN><\/P><br \/>\n<UL style=\"MARGIN-TOP: 0in\" type=disc><br \/>\n<LI class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none; mso-list: l1 level1 lfo2; tab-stops: list .5in\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><FONT face=Arial><FONT color=#000000>Replaces OSQL (and then some!)<o:p><\/o:p><\/FONT><\/FONT><\/SPAN><br \/>\n<LI class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none; mso-list: l1 level1 lfo2; tab-stops: list .5in\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><FONT face=Arial><FONT color=#000000>Can set a variety of environment variables<o:p><\/o:p><\/FONT><\/FONT><\/SPAN><br \/>\n<LI class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none; mso-list: l1 level1 lfo2; tab-stops: list .5in\"><FONT color=#000000><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><FONT face=Arial>Offers a -A parameter for the dedicated admin connection (only one window available for this at a time and the usage here is when you can&#8217;t otherwise connect to your server &#8211; for whatever reason. To ensure that they can always use this they have kept resources available so that this should always work. I use &#8220;should&#8221; because well, I rarely use always (<EM>or never<\/EM>)&#8230; <\/FONT><\/SPAN><SPAN style=\"FONT-SIZE: 10pt; FONT-FAMILY: Wingdings; mso-bidi-font-family: Arial; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-char-type: symbol; mso-symbol-font-family: Wingdings\"><SPAN style=\"mso-char-type: symbol; mso-symbol-font-family: Wingdings\">J<\/SPAN><\/SPAN><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><o:p><\/o:p><\/SPAN><\/FONT><br \/>\n<LI class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none; mso-list: l1 level1 lfo2; tab-stops: list .5in\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><FONT color=#000000><FONT face=Arial>Adds &#8211;v for variable input from the command line but also supports variables set as environment variables (via SET) or variables set within the SQLCMD script (SETVAR).<o:p><\/o:p><\/FONT><\/FONT><\/SPAN><br \/>\n<LI class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none; mso-list: l1 level1 lfo2; tab-stops: list .5in\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><FONT color=#000000><FONT face=Arial>Allows connections to be specified within the script via:<BR>&nbsp;&nbsp;&nbsp;&nbsp; <\/FONT><\/FONT><\/SPAN><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><FONT color=#000000><FONT face=Arial>:CONNECT servername<BR>NOTE: the semi-colon is required <\/FONT><\/FONT><\/SPAN><br \/>\n<LI class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none; mso-list: l1 level1 lfo2; tab-stops: list .5in\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><\/SPAN><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><FONT color=#000000><FONT face=Arial>Has a few new error handling options such as <BR>&nbsp;&nbsp;&nbsp;&nbsp; <\/FONT><\/FONT><\/SPAN><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><FONT color=#000000><FONT face=Arial>:ON ERROR EXIT<BR>NICE: this uses RAISERROR and when a severity of&nbsp;11 (I think)&nbsp;or higher is raised you can define what happens (EXIT, IGNORE or RETRY <EM>n<\/EM>) to control script flow and retry behavior (yeah!)<\/FONT><\/FONT><\/SPAN><br \/>\n<LI class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none; mso-list: l2 level1 lfo3; tab-stops: list .5in\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><FONT color=#000000><FONT face=Arial>And with the exception of a few new (yet VERY cool things like this) SQLCMD is really just TSQL plus some very nice automation stuff. (<EM>I can hear TSQL geeks cheering&#8230;<\/EM>)<\/FONT><\/FONT><\/SPAN><\/LI><\/UL><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><o:p><FONT face=Arial color=#000000><\/FONT><\/o:p><\/SPAN><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><FONT color=#000000><FONT face=Arial>So &#8211; I created a script that does this and here&#8217;s the play by play of it:<\/FONT><\/FONT><\/SPAN><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><FONT face=Arial color=#000000><\/FONT><\/SPAN>&nbsp;<\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><FONT face=Arial color=#000000>I don&#8217;t want to support system databases for backup\/restore. You could certainly do a backup and then restore to a new name&#8230; but that will have to be in version 2.<\/FONT><\/SPAN><\/P><br \/>\n<BLOCKQUOTE dir=ltr style=\"MARGIN-RIGHT: 0px\"><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">:ON ERROR EXIT<BR>go<\/SPAN><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><\/SPAN>&nbsp;<\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">IF &#8216;$(DB)&#8217; IN (&#8216;master&#8217;, &#8216;model&#8217;, &#8216;msdb&#8217;, &#8216;ReportServer&#8217;, &#8216;distribution&#8217;, &#8216;tempdb&#8217;)<BR>BEGIN<BR>&nbsp;RAISERROR (&#8216;System Databases are NOT supported. Script Terminated.&#8217;, 16, -1)<BR>END<BR>go<\/SPAN><\/P><\/BLOCKQUOTE><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><FONT color=#000000><FONT face=Arial>Next, I wanted to create a connection to my 2000 server (this could have easily been a parameter!)<\/FONT><\/FONT><\/SPAN><\/P><br \/>\n<BLOCKQUOTE dir=ltr style=\"MARGIN-RIGHT: 0px\"><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">:CONNECT servername<\/SPAN><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><\/SPAN>&nbsp;<\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">BACKUP DATABASE $(DB)<BR>&nbsp;TO DISK = &#8216;\\\\servername\\sharename\\path\\$(db).bak&#8217; &#8212; NOTICE YOU CAN USE VARIABLES WITHIN STRINGS!<BR>&nbsp;WITH INIT<BR>PRINT &#8216; &#8216;<BR>go<\/SPAN><\/P><\/BLOCKQUOTE><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><\/SPAN><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">NOTE: Be sure to set a GO after each batch that includes changes in connections, etc. The idea is that much of the SQLCMD code (per batch) is evaluated and executed first. Without the go I would have ended up in the next connection &#8211; executing the backup. Here&#8217;s a quick example of what I mean:<\/SPAN><\/P><br \/>\n<BLOCKQUOTE dir=ltr style=\"MARGIN-RIGHT: 0px\"><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">&#8212; Example 1<\/SPAN><\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">:CONNECT SERVER1<BR>EXEC PROC1<\/SPAN><\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><BR>: CONNECT SERVER2<BR>EXEC PROC2<\/SPAN><\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">&#8212; In Example 1, both procedures execute on SERVER2<\/SPAN><\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><\/SPAN>&nbsp;<\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">:CONNECT SERVER1<BR>EXEC PROC1<BR><STRONG>go<\/STRONG><\/SPAN><\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><\/SPAN>&nbsp;<\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">&#8212; Example 2<\/SPAN><\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">: CONNECT SERVER2<BR>EXEC PROC2<BR><STRONG>go<\/STRONG><\/SPAN><\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">&#8212; In Example 2, PROC1 executes on SERVER1 then PROC2 executes on SERVER2 &#8211; as desired.<\/SPAN><\/P><\/BLOCKQUOTE><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">After the database is backed up I connect to the SQL 2005 server and drop the existing database&#8230; you may or may not want to do this so blindly.<\/SPAN><\/P><br \/>\n<BLOCKQUOTE dir=ltr style=\"MARGIN-RIGHT: 0px\"><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">:CONNECT servername<\/SPAN><\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">SET NOCOUNT ON<\/SPAN><\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><\/SPAN>&nbsp;<\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">IF DATABASEPROPERTYEX(&#8216;$(DB)&#8217;, &#8216;COLLATION&#8217;) IS NOT NULL<BR>&nbsp;DROP DATABASE [$(DB)]<\/SPAN><\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><\/SPAN>&nbsp;<\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">WAITFOR DELAY &#8216;$(DELAY)&#8217; &#8212; this was just for testing&#8230;<\/SPAN><\/P><\/BLOCKQUOTE><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">Next, I need to determine the SQL 2005&#8217;s Data Path as I didn&#8217;t want to assume C:\\Program Files\\&#8230;<\/SPAN><\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">To do this I&#8217;m using an undocumented xp (cause I wanted to see if it still works? it does!)<\/SPAN><\/P><br \/>\n<BLOCKQUOTE dir=ltr style=\"MARGIN-RIGHT: 0px\"><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">DECLARE @SQLPath&nbsp;nvarchar(512)<BR>EXECUTE master.dbo.xp_regread &#8216;HKEY_LOCAL_MACHINE&#8217;, <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8216;SOFTWARE\\Microsoft\\MSSQLServer\\Setup&#8217;, <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8216;SQLPath&#8217;, @SQLPath&nbsp;OUTPUT<\/SPAN><\/P><\/BLOCKQUOTE><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">Next, I do some cool stuff to create the correct locations for the new data and log file names. This was the hardest part really&#8230; What if the database has multiple files &#8211; and the pathes are NOT the same as the server from which the database was backed up. Well, now I need to do a restore with MOVE. Have you seen the syntax for RESTORE with MOVE&#8230; it&#8217;s not overly pretty. Anyway, I first create a table into which I will store the output of LOAD FILELISTONLY (so I can interrogate the backup device and see the filelist of the database which was backed up):<\/SPAN><\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><\/SPAN>&nbsp;<\/P><br \/>\n<BLOCKQUOTE dir=ltr style=\"MARGIN-RIGHT: 0px\"><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">CREATE TABLE #BackupFileList<BR>( LogicalName&nbsp;sysname&nbsp;NULL<BR>&nbsp;, PhysicalName&nbsp;sysname&nbsp;NULL<BR>&nbsp;, [Type]&nbsp;char(1)<BR>&nbsp;, FileGroupName&nbsp;sysname NULL<BR>&nbsp;, Size&nbsp;bigint<BR>&nbsp;, MaxSize&nbsp;bigint<BR>&nbsp;, FileId&nbsp;smallint<BR>&nbsp;, CreateLSN&nbsp;numeric(25,0)<BR>&nbsp;, DropLSN&nbsp;numeric(25,0)<BR>&nbsp;, UniqueId uniqueidentifier<BR>&nbsp;, ReadOnlyLSN&nbsp;numeric(25,0)<BR>&nbsp;, ReadWriteLSN&nbsp;numeric(25,0)<BR>&nbsp;, BackupSizeInBytes&nbsp;bigint<BR>&nbsp;, SourceBlockSize&nbsp;bigint<BR>&nbsp;, FileGroupId&nbsp;&nbsp;smallint<BR>&nbsp;, LogGroupGUID&nbsp;uniqueidentifier<BR>&nbsp;, DifferentialBaseLSN&nbsp;numeric(25,0)<BR>&nbsp;, DifferentialBaseGUID&nbsp;uniqueidentifier<BR>&nbsp;, IsReadOnly&nbsp;bit<BR>&nbsp;, IsPresent&nbsp;bit<BR>)<\/SPAN><\/P><\/BLOCKQUOTE><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">Next, I get the info from the backup device (again, notice the parameters being used even in Dynamic String Execution!)<\/SPAN><\/P><br \/>\n<BLOCKQUOTE dir=ltr style=\"MARGIN-RIGHT: 0px\"><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">INSERT #BackupFileList<BR>&nbsp;EXEC(&#8216;LOAD FILELISTONLY FROM DISK = &#8221;D:\\SQLDemo\\$(db).bak&#8221;&#8217;)<\/SPAN><\/P><\/BLOCKQUOTE><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">Now I reverse the physical name to find only the &#8220;file&#8220; part of the name&#8230; reversing it to find the first string up to a backslash (from the end). Then I extract that string and reverse it back&#8230; and&nbsp;&#8211; at the beginning of the string, I add the local server&#8217;s data path.<\/SPAN><\/P><br \/>\n<BLOCKQUOTE dir=ltr style=\"MARGIN-RIGHT: 0px\"><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">UPDATE #BackupFileList<BR>&nbsp;SET PhysicalName = @SQLPath + N&#8217;\\Data\\&#8217; + REVERSE(SUBSTRING(REVERSE(PhysicalName), 1, PATINDEX(&#8216;%\\%&#8217;, REVERSE(PhysicalName)) -1))<\/SPAN><\/P><\/BLOCKQUOTE><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">Now I&#8217;m ready to walk the filelist and build the RESTORE with MOVE database string. This completes the script!<\/SPAN><\/P><br \/>\n<BLOCKQUOTE dir=ltr style=\"MARGIN-RIGHT: 0px\"><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">DECLARE @LogicalName&nbsp;sysname<BR>&nbsp;, @PhysicalName&nbsp;sysname<BR>&nbsp;, @ExecStr&nbsp;&nbsp;&nbsp;nvarchar(max)<\/SPAN><\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><\/SPAN>&nbsp;<\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">DECLARE FileListCursor CURSOR FAST_FORWARD FOR <BR>&nbsp;SELECT LogicalName, PhysicalName<BR>&nbsp;FROM #BackupFileList<\/SPAN><\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><\/SPAN>&nbsp;<\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">OPEN FileListCursor<\/SPAN><\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><\/SPAN>&nbsp;<\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">FETCH NEXT FROM FileListCursor INTO @LogicalName, @PhysicalName<\/SPAN><\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><\/SPAN>&nbsp;<\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">SELECT @ExecStr = N&#8217;RESTORE DATABASE $(DB)&#8217; +<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; N&#8217; FROM DISK = &#8221;D:\\SQLDemo\\$(db).bak&#8221;&#8217; +<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; N&#8217; WITH MOVE &#8221;&#8217; + @LogicalName + N&#8221;&#8217; TO &#8221;&#8217; + @PhysicalName + N&#8221;&#8221;<BR>&nbsp;<BR>FETCH NEXT FROM FileListCursor INTO @LogicalName, @PhysicalName<\/SPAN><\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">WHILE @@FETCH_STATUS &lt;&gt; -1<BR>BEGIN<BR>&nbsp;SELECT @ExecStr = @ExecStr + N&#8217;, MOVE &#8221;&#8217; + @LogicalName + &#8221;&#8217; TO &#8221;&#8217; + @PhysicalName + &#8221;&#8221;<BR>&nbsp;FETCH NEXT FROM FileListCursor INTO @LogicalName, @PhysicalName<BR>END<\/SPAN><\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><\/SPAN>&nbsp;<\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">&#8212; SELECT @ExecStr<\/SPAN><\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">EXEC (@ExecStr)<\/SPAN><\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><\/SPAN>&nbsp;<\/P><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">DEALLOCATE FileListCursor<BR>go<\/SPAN><\/P><\/BLOCKQUOTE><br \/>\n<P class=MsoNormal dir=ltr style=\"MARGIN: 0in 0in 0pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><\/SPAN><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\">I set my parameters on the call to the script.<\/P><br \/>\n<P dir=ltr style=\"MARGIN-RIGHT: 0px\"><\/SPAN><SPAN style=\"FONT-SIZE: 10pt; mso-bidi-font-family: Arial\"><FONT face=Arial><FONT color=#000000>If you&#8217;re interested in reading more about somewhat stressful demos for the BI Team <a href=\"https:\/\/www.sqlskills.com\/blogs\/kimberly\/permalink.aspx?guid=d68a9ffa-733b-4400-9e31-3479240082e5\">read here<\/a>. <\/FONT><\/FONT><\/SPAN><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of my favorite features of Management Studio is the ability to &#8220;script&#8221; a step rather than execute it. Within SQL Management Studio, each dialog has 4 different options from which to choose (these are including the typical OK\/Cancel) but also add a couple more at the top of the dialog: &nbsp; At the top [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,65,78],"tags":[],"class_list":["post-697","post","type-post","status-publish","format-standard","hentry","category-events","category-sql-server-2005","category-tips"],"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts\/697","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/comments?post=697"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/posts\/697\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/media?parent=697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/categories?post=697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/kimberly\/wp-json\/wp\/v2\/tags?post=697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}