<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Random Musings of Jeremy Jameson : SQL Server</title><link>http://blogs.msdn.com/jjameson/archive/tags/SQL+Server/default.aspx</link><description>Tags: SQL Server</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Bug Deploying SQL Server Database Using VSDBCMD.EXE</title><link>http://blogs.msdn.com/jjameson/archive/2009/12/09/bug-deploying-sql-server-database-using-vsdbcmd-exe.aspx</link><pubDate>Wed, 09 Dec 2009 14:32:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9934579</guid><dc:creator>Jeremy Jameson</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jjameson/comments/9934579.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jjameson/commentrss.aspx?PostID=9934579</wfw:commentRss><description>&lt;P&gt;Yesterday we encountered a bug while trying to deploy a new SQL Server database from a Visual Studio database project using the VSDBCMD.EXE utility, following the prescriptive guidance on MSDN:&lt;/P&gt;
&lt;DIV class=reference&gt;&lt;CITE&gt;How to: Prepare a Database for Deployment From a Command Prompt by Using VSDBCMD&lt;/CITE&gt; 
&lt;DIV class=referenceLink&gt;&lt;A href="http://msdn.microsoft.com/en-us/library/dd193258.aspx" mce_href="http://msdn.microsoft.com/en-us/library/dd193258.aspx"&gt;http://msdn.microsoft.com/en-us/library/dd193258.aspx&lt;/A&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;According to this MSDN article, all you need to do is copy some files to the server running Microsoft SQL Server, and then run the VSDBCMD.EXE utility (specifying your .dbschema or .deploymanifest file).&lt;/P&gt;
&lt;P&gt;Unfortunately, when we attempted to deploy our database to our Test environment, we encountered a &lt;CODE&gt;NullReferenceException&lt;/CODE&gt;: &lt;/P&gt;
&lt;BLOCKQUOTE class="directQuote errorMessage"&gt;Object reference not set to an instance of an object.&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Fortunately, it didn't take long to find &lt;A href="http://social.msdn.microsoft.com/Forums/en-US/vstsdb/thread/32725cf6-74c1-4b5a-9057-b909ae8a2517" mce_href="http://social.msdn.microsoft.com/Forums/en-US/vstsdb/thread/32725cf6-74c1-4b5a-9057-b909ae8a2517"&gt;Gert's recommendation&lt;/A&gt; to add the following registry key:&lt;/P&gt;
&lt;DIV class=consoleBlock&gt;&lt;SAMP&gt;reg add HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0&lt;/SAMP&gt;&lt;/DIV&gt;
&lt;P&gt;The error occurred since Visual Studio has never been installed on the SQL Server in TEST (unlike our local development VMs).&lt;/P&gt;
&lt;P&gt;It certainly would be nice if the above MSDN article were updated to note the bug and associated workaround.&lt;/P&gt;
&lt;BLOCKQUOTE class=note&gt;
&lt;DIV class=noteTitle&gt;Note&lt;/DIV&gt;
&lt;DIV&gt;As pointed out in a comment by Ramkumar Perumal, Microsoft.SqlServer.BatchParser.dll does not exist in any of the specified folders. This should also be reflected in the MSDN article.&lt;/DIV&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You will also find instructions to add the registry key -- as well as an explanation for why you more than likely already have Microsoft.SqlServer.BatchParser.dll installed on your SQL Server -- in the following blog post:&lt;/P&gt;
&lt;DIV class=reference&gt;&lt;CITE&gt;Deploying your Database Project without VSTSDB installed. 2009-02-21&lt;/CITE&gt; 
&lt;DIV class=referenceLink&gt;&lt;A href="http://blogs.msdn.com/bahill/archive/2009/02/21/deploying-your-database-project-without-vstsdb-installed.aspx" mce_href="http://blogs.msdn.com/bahill/archive/2009/02/21/deploying-your-database-project-without-vstsdb-installed.aspx"&gt;http://blogs.msdn.com/bahill/archive/2009/02/21/deploying-your-database-project-without-vstsdb-installed.aspx&lt;/A&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9934579" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jjameson/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/jjameson/archive/tags/Visual+Studio/default.aspx">Visual Studio</category></item><item><title>Error Installing KB 970892 When Reporting Services Configured with Domain Account</title><link>http://blogs.msdn.com/jjameson/archive/2009/12/07/error-installing-kb-970892-when-reporting-services-configured-with-domain-account.aspx</link><pubDate>Tue, 08 Dec 2009 05:11:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9933900</guid><dc:creator>Jeremy Jameson</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jjameson/comments/9933900.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jjameson/commentrss.aspx?PostID=9933900</wfw:commentRss><description>&lt;P&gt;For a little over a month, Windows Update was failing on one of the servers in the &lt;A href="http://blogs.msdn.com/jjameson/archive/2009/09/14/the-jameson-datacenter.aspx" mce_href="http://blogs.msdn.com/jjameson/archive/2009/09/14/the-jameson-datacenter.aspx"&gt;"Jameson Datacenter"&lt;/A&gt; (a.k.a. my home lab). Specifically, &lt;A href="http://support.microsoft.com/kb/970892" mce_href="http://support.microsoft.com/kb/970892"&gt;KB 970892&lt;/A&gt; simply would not install on JUBILEE -- my Systems Center Operations Manager (SCOM) 2007 VM, that I use to monitor a number of physical and virtual machines.&lt;/P&gt;
&lt;P&gt;Here's the first event I found regarding this error:&lt;/P&gt;
&lt;DIV class=logExcerpt&gt;&lt;SAMP&gt;Log Name: Application&lt;BR&gt;Source: MsiInstaller&lt;BR&gt;Date: 10/27/2009 3:03:49 AM&lt;BR&gt;Event ID: 10005&lt;BR&gt;Task Category: None&lt;BR&gt;Level: Error&lt;BR&gt;Keywords: Classic&lt;BR&gt;User: SYSTEM&lt;BR&gt;Computer: jubilee.corp.technologytoolbox.com&lt;BR&gt;Description:&lt;BR&gt;Product: Microsoft SQL Server 2005 Reporting Services (64-bit) -- Error 29528. The setup has encountered an unexpected error while Setting reporting service and share point exclusion path. The error is: Fatal error during installation.&lt;/SAMP&gt;&lt;/DIV&gt;
&lt;P&gt;This was quickly followed by another error in the event log:&lt;/P&gt;
&lt;DIV class=logExcerpt&gt;&lt;SAMP&gt;Log Name: Application&lt;BR&gt;Source: MsiInstaller&lt;BR&gt;Date: 10/27/2009 3:04:55 AM&lt;BR&gt;Event ID: 1023&lt;BR&gt;Task Category: None&lt;BR&gt;Level: Error&lt;BR&gt;Keywords: Classic&lt;BR&gt;User: SYSTEM&lt;BR&gt;Computer: jubilee.corp.technologytoolbox.com&lt;BR&gt;Description:&lt;BR&gt;Product: Microsoft SQL Server 2005 Reporting Services (64-bit) - Update 'GDR 4053 for SQL Server Reporting Services 2005 (64-bit) ENU (KB970892)' could not be installed. Error code 1603. Additional information is available in the log file C:\Program Files\Microsoft SQL Server\90\Setup Bootstrap\LOG\Hotfix\RS9_Hotfix_KB970892_sqlrun_rs.msp.log.&lt;/SAMP&gt;&lt;/DIV&gt;
&lt;P&gt;Shortly thereafter, I started seeing the following error once every minute:&lt;/P&gt;
&lt;DIV class=logExcerpt&gt;&lt;SAMP&gt;Log Name: Application&lt;BR&gt;Source: Report Server (MSSQLSERVER)&lt;BR&gt;Date: 10/27/2009 3:06:52 AM&lt;BR&gt;Event ID: 107&lt;BR&gt;Task Category: Management&lt;BR&gt;Level: Error&lt;BR&gt;Keywords: Classic&lt;BR&gt;User: N/A&lt;BR&gt;Computer: jubilee.corp.technologytoolbox.com&lt;BR&gt;Description:&lt;BR&gt;Report Server (MSSQLSERVER) cannot connect to the report server database.&lt;/SAMP&gt;&lt;/DIV&gt;
&lt;P&gt;Since I have Windows Update configured to automatically download and install updates every morning, the patch attempted to install each day -- but failed each and every time.&lt;/P&gt;
&lt;P&gt;I have to admit that I've spent a fair amount of time troubleshooting this error over the past month, but since it wasn't a blocking issue -- just a particularly irritating annoyance -- I kept putting it off. [Honestly, I rarely look at the SCOM reports and instead rely mostly on email notifications and the Operations Manager Console.]&lt;/P&gt;
&lt;P&gt;Fortunately, I finally managed to determine the root cause tonight and resolve the issue.&lt;/P&gt;
&lt;P&gt;After downloading and installing the standalone patch installation, I discovered the following in the installation log:&lt;/P&gt;
&lt;DIV class=logExcerpt&gt;&lt;SAMP&gt;MSI (s) (F0:54) [21:09:49:565]: Invoking remote custom action. DLL: C:\Windows\Installer\MSIAC31.tmp, Entrypoint: Do_RSSetSharePointExclusionPath&lt;BR&gt;&amp;lt;Func Name='LaunchFunction'&amp;gt;&lt;BR&gt;Function=Do_RSSetSharePointExclusionPath&lt;BR&gt;&amp;lt;Func Name='GetCAContext'&amp;gt;&lt;BR&gt;&amp;lt;EndFunc Name='GetCAContext' Return='T' GetLastError='203'&amp;gt;&lt;BR&gt;Doing Action: Do_RSSetSharePointExclusionPath&lt;BR&gt;PerfTime Start: Do_RSSetSharePointExclusionPath : Mon Dec 07 21:09:49 2009&lt;BR&gt;&amp;lt;Func Name='Do_RSSetSharePointExclusionPath'&amp;gt;&lt;BR&gt;The application pool /s already exists.&lt;BR&gt;Error Code: 0x80077374 (29556)&lt;BR&gt;Windows Error Text: Source File Name: sqlca\sqliisca.cpp&lt;BR&gt;Compiler Timestamp: Mon Nov 17 17:05:40 2008&lt;BR&gt;Function Name: Do_RSSetSharePointExclusionPath&lt;BR&gt;Source Line Number: 914&lt;BR&gt;&lt;/SAMP&gt;&lt;/DIV&gt;
&lt;P&gt;As noted in &lt;A href="http://support.microsoft.com/kb/917826" mce_href="http://support.microsoft.com/kb/917826"&gt;KB 917826&lt;/A&gt;, there appears to be a known issue when Reporting Services is configured to run using a domain account. For JUBILEE, the &lt;STRONG&gt;ReportServer &lt;/STRONG&gt;application pool was configured to run as &lt;STRONG&gt;TECHTOOLBOX\svc-mom-das &lt;/STRONG&gt;(the SCOM data access service account). After changing the app pool to run as &lt;STRONG&gt;NetworkService &lt;/STRONG&gt;instead, I ran the standalone install of KB 970892 and it completed successfully.&lt;/P&gt;
&lt;P&gt;I then changed the app pool identity back to TECHTOOLBOX\svc-mom-das (since that appears to be how SCOM 2007 wants it configured) and verified that a couple of reports run successfully. Woohoo!&lt;/P&gt;
&lt;P&gt;I'm crossing my fingers that tomorrow morning, Windows Update detects that KB 970892 is installed and no errors occur.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9933900" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jjameson/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/jjameson/archive/tags/Infrastructure/default.aspx">Infrastructure</category></item><item><title>Database Default Locations in SQL Server</title><link>http://blogs.msdn.com/jjameson/archive/2009/12/03/database-default-locations-in-sql-server.aspx</link><pubDate>Thu, 03 Dec 2009 13:30:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9931948</guid><dc:creator>Jeremy Jameson</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jjameson/comments/9931948.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jjameson/commentrss.aspx?PostID=9931948</wfw:commentRss><description>&lt;P&gt;I've mentioned before the importance of using multiple "spindles" when working with large SQL Server databases.&lt;/P&gt;
&lt;P&gt;Generally speaking, the recommendation is to use different RAID 1+0 arrays for data and log files -- and depending on the size and load of your database, you may also need to isolate data files on individual RAID 1+0 arrays.&lt;/P&gt;
&lt;P&gt;For really large OLTP databases that require lots of &lt;A href="http://en.wikipedia.org/wiki/IOPS" mce_href="http://en.wikipedia.org/wiki/IOPS"&gt;IOPS&lt;/A&gt;, additional arrays might also be required to split the log files (although, honestly, I haven't worked on any projects where this has been necessary).&lt;/P&gt;
&lt;P&gt;One of the configuration steps that I always recommend for any Production SQL Server environment is to configure the default database locations to ensure your "data I/O" is isolated from your "log I/O" (both of which should be isolated from other I/O -- specifically, the paging file).&lt;/P&gt;
&lt;P&gt;For example, suppose you have the following disk configuration on your SQL Server cluster:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;C: (Local Disk) - RAID 1 array for operating system files, program files, and paging file&lt;/LI&gt;
&lt;LI&gt;D: (DATA01) - RAID 1+0 array for data files&lt;/LI&gt;
&lt;LI&gt;L: (LOG01) - RAID 1+0 array for log files&lt;/LI&gt;
&lt;LI&gt;Q: (Quorum)&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;In this simplistic example, the D:, L:, and Q: drives are LUNs on the backend SAN, whereas the C: drive is DAS (Direct Attached Storage). Hopefully, the Storage team responsible for managing your SAN provides dedicated physical drives for D: and L: -- although, honestly, I've seen several enterprise organizations that don't dedicate these drives and subsequently experience performance issues later on.&lt;/P&gt;
&lt;P&gt;Given the above disk configuration, SQL Server Management Studio should be used to configure the default database locations similar to the following:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Server Properties 
&lt;UL&gt;
&lt;LI&gt;Database Settings 
&lt;UL&gt;
&lt;LI&gt;Default database locations 
&lt;UL&gt;
&lt;LI&gt;Data: D:\NotBackedUp\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA&lt;/LI&gt;
&lt;LI&gt;Log: L:\NotBackedUp\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;You certainly don't have to use the &lt;A href="http://blogs.msdn.com/jjameson/archive/2007/03/22/backedup-and-notbackedup.aspx" mce_href="http://blogs.msdn.com/jjameson/archive/2007/03/22/backedup-and-notbackedup.aspx"&gt;NotBackedUp&lt;/A&gt; folder if you don't want to (that's just the standard that I've been using for years).&lt;/P&gt;
&lt;P&gt;Once you've configured the default database locations, anytime you create a new database -- for example, when you create a new Web application or content database in Microsoft Office SharePoint Server (MOSS) 2007 -- the data and log files will be placed on the desired drives (i.e. D: and L:, respectively).&lt;/P&gt;
&lt;P&gt;Of course, when creating MOSS 2007 databases for a Production (or Test) environment, you still need to resize them appropriately in order to avoid having them simply auto-grow from a very small initial size (in order to avoid fragmentation and optimize performance).&lt;/P&gt;
&lt;P&gt;Suppose you need to determine the default database locations through SQL (perhaps because you are scripting the process to create your databases). If you fire up SQL Server Profiler and start a new trace (the default trace options are fine), you will find that SQL Server Management Studio executes the following SQL statements when viewing the &lt;STRONG&gt;Database Settings &lt;/STRONG&gt;page in the &lt;STRONG&gt;Server Properties &lt;/STRONG&gt;window:&lt;/P&gt;
&lt;DIV class=codeBlock&gt;&lt;PRE&gt;&lt;CODE&gt;&lt;SPAN style="COLOR: #0000ff"&gt;declare&lt;/SPAN&gt; @RegPathParams &lt;SPAN style="COLOR: #0000ff"&gt;sysname
declare&lt;/SPAN&gt; @Arg &lt;SPAN style="COLOR: #0000ff"&gt;sysname
declare&lt;/SPAN&gt; @Param &lt;SPAN style="COLOR: #0000ff"&gt;sysname
declare&lt;/SPAN&gt; @MasterPath &lt;SPAN style="COLOR: #0000ff"&gt;nvarchar&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;512&lt;SPAN style="COLOR: #808080"&gt;)
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;declare&lt;/SPAN&gt; @LogPath &lt;SPAN style="COLOR: #0000ff"&gt;nvarchar&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;512&lt;SPAN style="COLOR: #808080"&gt;)
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;declare&lt;/SPAN&gt; @ErrorLogPath &lt;SPAN style="COLOR: #0000ff"&gt;nvarchar&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;512&lt;SPAN style="COLOR: #808080"&gt;)
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;declare&lt;/SPAN&gt; @n &lt;SPAN style="COLOR: #0000ff"&gt;int

select&lt;/SPAN&gt; @n&lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt;0
&lt;SPAN style="COLOR: #0000ff"&gt;select&lt;/SPAN&gt; @RegPathParams&lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;N'Software\Microsoft\MSSQLServer\MSSQLServer'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;'\Parameters'
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;select&lt;/SPAN&gt; @Param&lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;'dummy'
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;while&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(not&lt;/SPAN&gt; @Param &lt;SPAN style="COLOR: #808080"&gt;is&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;null)
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;begin
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;select&lt;/SPAN&gt; @Param&lt;SPAN style="COLOR: #808080"&gt;=null
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;select&lt;/SPAN&gt; @Arg&lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;'SqlArg'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff00ff"&gt;convert&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;nvarchar&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt;@n&lt;SPAN style="COLOR: #808080"&gt;)

&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;exec&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;master&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;.&lt;/SPAN&gt;dbo&lt;SPAN style="COLOR: #808080"&gt;.&lt;/SPAN&gt;&lt;SPAN style="COLOR: #800000"&gt;xp_instance_regread
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;        &lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;N'HKEY_LOCAL_MACHINE'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;,
&lt;/SPAN&gt;        @RegPathParams&lt;SPAN style="COLOR: #808080"&gt;,
&lt;/SPAN&gt;        @Arg&lt;SPAN style="COLOR: #808080"&gt;,
&lt;/SPAN&gt;        @Param &lt;SPAN style="COLOR: #0000ff"&gt;OUTPUT
&lt;/SPAN&gt;        
    &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;@Param &lt;SPAN style="COLOR: #808080"&gt;like&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'-d%'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;)
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;begin
&lt;/SPAN&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;select&lt;/SPAN&gt; @Param&lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff00ff"&gt;substring&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;@Param&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; 3&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; 255&lt;SPAN style="COLOR: #808080"&gt;)
&lt;/SPAN&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;select&lt;/SPAN&gt; @MasterPath&lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff00ff"&gt;substring&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(
&lt;/SPAN&gt;            @Param&lt;SPAN style="COLOR: #808080"&gt;,
&lt;/SPAN&gt;            1&lt;SPAN style="COLOR: #808080"&gt;,
&lt;/SPAN&gt;            &lt;SPAN style="COLOR: #ff00ff"&gt;len&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;@Param&lt;SPAN style="COLOR: #808080"&gt;)&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;-&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff00ff"&gt;charindex&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;'\'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff00ff"&gt;reverse&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;@Param&lt;SPAN style="COLOR: #808080"&gt;)))&lt;/SPAN&gt;            
    &lt;SPAN style="COLOR: #0000ff"&gt;end
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;else&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;@Param &lt;SPAN style="COLOR: #808080"&gt;like&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'-l%'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;)
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;begin
&lt;/SPAN&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;select&lt;/SPAN&gt; @Param&lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff00ff"&gt;substring&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;@Param&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; 3&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; 255&lt;SPAN style="COLOR: #808080"&gt;)
&lt;/SPAN&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;select&lt;/SPAN&gt; @LogPath&lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff00ff"&gt;substring&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(
&lt;/SPAN&gt;            @Param&lt;SPAN style="COLOR: #808080"&gt;,
&lt;/SPAN&gt;            1&lt;SPAN style="COLOR: #808080"&gt;,
&lt;/SPAN&gt;            &lt;SPAN style="COLOR: #ff00ff"&gt;len&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;@Param&lt;SPAN style="COLOR: #808080"&gt;)&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;-&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff00ff"&gt;charindex&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;'\'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff00ff"&gt;reverse&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;@Param&lt;SPAN style="COLOR: #808080"&gt;)))
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;end
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;else&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;if&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;@Param &lt;SPAN style="COLOR: #808080"&gt;like&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'-e%'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;)
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;begin
&lt;/SPAN&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;select&lt;/SPAN&gt; @Param&lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff00ff"&gt;substring&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;@Param&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; 3&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; 255&lt;SPAN style="COLOR: #808080"&gt;)
&lt;/SPAN&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;select&lt;/SPAN&gt; @ErrorLogPath&lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff00ff"&gt;substring&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(
&lt;/SPAN&gt;            @Param&lt;SPAN style="COLOR: #808080"&gt;,
&lt;/SPAN&gt;            1&lt;SPAN style="COLOR: #808080"&gt;,
&lt;/SPAN&gt;            &lt;SPAN style="COLOR: #ff00ff"&gt;len&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;@Param&lt;SPAN style="COLOR: #808080"&gt;)&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;-&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff00ff"&gt;charindex&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;'\'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff00ff"&gt;reverse&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;@Param&lt;SPAN style="COLOR: #808080"&gt;)))
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;end
&lt;/SPAN&gt;    
    &lt;SPAN style="COLOR: #0000ff"&gt;select&lt;/SPAN&gt; @n&lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt;@n&lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt;1
&lt;SPAN style="COLOR: #0000ff"&gt;end
&lt;/SPAN&gt;            
&lt;SPAN style="COLOR: #0000ff"&gt;print&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'LogPath = '&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; @LogPath
&lt;SPAN style="COLOR: #0000ff"&gt;print&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'MasterPath = '&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; @MasterPath&lt;/CODE&gt;&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Note that I truncated the actual SQL batch and added the &lt;CODE&gt;&lt;SPAN style="COLOR: #0000ff"&gt;print&lt;/SPAN&gt; &lt;/CODE&gt;statements.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9931948" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jjameson/archive/tags/MOSS+2007/default.aspx">MOSS 2007</category><category domain="http://blogs.msdn.com/jjameson/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Environment Naming Conventions</title><link>http://blogs.msdn.com/jjameson/archive/2009/06/09/environment-naming-conventions.aspx</link><pubDate>Wed, 10 Jun 2009 02:42:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9718725</guid><dc:creator>Jeremy Jameson</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jjameson/comments/9718725.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jjameson/commentrss.aspx?PostID=9718725</wfw:commentRss><description>&lt;P&gt;One of the challenges I see in organizations that I work with is the lack of naming conventions for various environments -- or sometimes naming conventions that provide little or no value.&lt;/P&gt;
&lt;P&gt;For about the last ten years, I've been a strong proponent of a simple "-suffix" naming convention that is not only very easy to learn (and remember!) but also makes things incredibly obvious to all team members and stakeholders on a project.&lt;/P&gt;
&lt;P&gt;Most enterprise IT organizations typically have a naming convention for servers that includes things like the Active Directory domain the server is a member of, the role of the server, and usually some unique identifier to distinguish multiple servers in a farm. Other "ingredients" such as the datacenter the server is physically located in may also be included.&lt;/P&gt;
&lt;P&gt;For example, my favorite fictitious manufacturing company, Fabrikam Technologies, has Production servers named:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;FAB-DC-01 (the first domain controller for the FABRIKAM domain),&lt;/LI&gt;
&lt;LI&gt;FAB-DC-02,&lt;/LI&gt;
&lt;LI&gt;FAB-WEB-01 (the first Web server in a farm),&lt;/LI&gt;
&lt;LI&gt;FAB-WEB-02,&lt;/LI&gt;
&lt;LI&gt;FAB-SQL-01A (the first node in a SQL Server cluster, for which the cluster is named FAB-SQL-01),&lt;/LI&gt;
&lt;LI&gt;FAB-SQL-01B,&lt;/LI&gt;
&lt;LI&gt;etc.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;This is a great start. As you can see from these examples, it's pretty easy to tell what the role of each server is.&lt;/P&gt;
&lt;P&gt;However, in any organization of reasonable size, we certainly need more than just the Production environment (PROD). At a minimum, I recommend using a Test environment (TEST) and a Development environment (DEV). Note that this is in addition to individual developer environments -- which I typically refer to as "local" evironments (LOCAL). Depending on how much parallel development is planned -- and the corresponding release schedule -- you might also need a Maintenance environment (MAINT). However, many organizations can function effectively with just the DEV-TEST-PROD triad.&lt;/P&gt;
&lt;P&gt;So this begs the question, what names should we use for DEV and TEST?&lt;/P&gt;
&lt;P&gt;Here is what I recommend:&lt;/P&gt;
&lt;P&gt;Monikers in the Development environment utilize a “-dev” suffix. For example, the Web server in DEV corresponding to the FAB-WEB-01 server in PROD is named FAB-WEB-01-DEV. Likewise, the SQL Server in the Development environment is named FAB-SQL-01A-DEV. Note that typically Development environments do not have a cluster, but I still recommend following the same naming convention and even adding a DNS entry for the DEV "cluster" name (e.g. FAB-SQL-01-DEV). Test environments often do have a SQL Server cluster configuration (which is a great place to validate your failover configuration and load testing).&lt;/P&gt;
&lt;P&gt;Host headers in the Development environment also follow the “-dev” naming convention. For example, if the internal name of the Fabrikam Web site in Production is &lt;A href="http://fabrikam/" mce_href="http://fabrikam"&gt;http://fabrikam&lt;/A&gt;, then &lt;A href="http://fabrikam-dev/" mce_href="http://fabrikam-dev"&gt;http://fabrikam-dev&lt;/A&gt; is the corresponding URL used in the Development environment.&lt;/P&gt;
&lt;P&gt;In order to distinguish sites on the local development VMs, the “-local” suffix is used. For example, &lt;A href="http://fabrikam-local/" mce_href="http://fabrikam-local"&gt;http://fabrikam-local&lt;/A&gt; is the URL on an individual developer’s VM corresponding to &lt;A href="http://fabrikam/" mce_href="http://fabrikam"&gt;http://fabrikam&lt;/A&gt; in Production. The %WINDIR%\System32\Drivers\etc\hosts file is then used to associate these URLs with the loopback address (127.0.0.1). [Be sure you are aware of the workaround in &lt;A href="http://support.microsoft.com/kb/896861" mce_href="http://support.microsoft.com/kb/896861"&gt;KB 896861&lt;/A&gt; when using the loopback address.]&lt;/P&gt;
&lt;P&gt;Similarly, monikers and host headers in the Test environment utilize a “-test” suffix (e.g. FAB-WEB-01-TEST, FAB-SQL-01A-TEST, and &lt;A href="http://fabrikam-test/" mce_href="http://fabrikam-test"&gt;http://fabrikam-test&lt;/A&gt;).&lt;/P&gt;
&lt;P&gt;Also note that organizations typically create separate service accounts within Active Directory for the Development, Test, and Production environments (and if you are not currently doing this, I strongly recommend it). For example, suppose &lt;STRONG&gt;FABRIKAM\svc-web&lt;/STRONG&gt; is the service account for the application pool running &lt;A href="http://fabrikam/" mce_href="http://fabrikam"&gt;http://fabrikam&lt;/A&gt; in Production. Then we would expect the Web site in DEV to be running as &lt;STRONG&gt;FABRIKAM\svc-web-dev&lt;/STRONG&gt; and the site in TEST to be running as &lt;STRONG&gt;FABRIKAM\svc-web-test&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;This ensures that only the minimal number of people need to know the passwords for a particular environment. For example, either the whole Development team -- or just a small subset of team leads -- needs to know the password for &lt;STRONG&gt;FABRIKAM\svc-web-dev&lt;/STRONG&gt;, but they certainly don't know the password for &lt;STRONG&gt;FABRIKAM\svc-web-test&lt;/STRONG&gt; (since that environment is managed by the Test team). Likewise, the Test team had better not know the password for the service account in Production (since -- at least hopefully -- that environment is owned by a separate Release Management team).&lt;/P&gt;
&lt;P&gt;It is also important to point out how this naming convention is applied to fully qualified domain names (FQDNs). For example, suppose the external address of &lt;A href="http://fabrikam/" mce_href="http://fabrikam"&gt;http://fabrikam&lt;/A&gt; is &lt;A href="http://www.fabrikam.com/" mce_href="http://www.fabrikam.com"&gt;http://www.fabrikam.com&lt;/A&gt;. What then should we use for the corresponding FQDN in DEV, or should we even configure an FQDN for DEV? The answer to that latter question is a resounding "Yes!" and the answer to the former is &lt;A href="http://www-dev.fabrikam.com/" mce_href="http://www-dev.fabrikam.com"&gt;http://www-dev.fabrikam.com&lt;/A&gt;. Note that we almost certainly won't to expose DEV on the Internet -- although we may choose to expose TEST (&lt;A href="http://www-test.fabrikam.com/" mce_href="http://www-test.fabrikam.com"&gt; http://www-test.fabrikam.com&lt;/A&gt;) &lt;/P&gt;
&lt;P&gt;The primary reason I recommend using these FQDNs (aside from the fact that we are following the simple "-suffix" naming convention) is that browsers behave differently between intranet names (e.g. &lt;A href="http://fabrikam-dev/" mce_href="http://fabrikam-dev"&gt;http://fabrikam-dev&lt;/A&gt;) and FQDNs (e.g. &lt;A href="http://www-dev.fabrikam.com/" mce_href="http://www-dev.fabrikam.com"&gt;http://www-dev.fabrikam.com&lt;/A&gt;). In other words, when you are trying to simulate a problem on the external site, you should ensure that you are simulating the same security "zone" in DEV, or even in your local environment (e.g. &lt;A href="http://www-local.fabrikam.com/" mce_href="http://www-local.fabrikam.com"&gt;http://www-local.fabrikam.com&lt;/A&gt;).&lt;/P&gt;
&lt;P&gt;Lastly, I want to mention documentation. Whenever I sit down to write an Installation Guide -- or similar documentation -- I typically only specify the values for the Production environment, even though I fully expect the document to also be used to install and configure the Test and Development environments. I simply include a brief section at the beginning of the document describing the naming convention and that it is the responsibility of the developer or tester to modify the server names, host headers, and service accounts appropriately as he or she is following along when configuring DEV or TEST.&lt;/P&gt;
&lt;P&gt;Since we typically have "infrastructure models" (i.e. Visio diagrams showing the physical architecture of DEV, TEST, and PROD), identifying servers and host headers in various environments is really straightforward.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9718725" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jjameson/archive/tags/My+System/default.aspx">My System</category><category domain="http://blogs.msdn.com/jjameson/archive/tags/Simplify/default.aspx">Simplify</category><category domain="http://blogs.msdn.com/jjameson/archive/tags/MOSS+2007/default.aspx">MOSS 2007</category><category domain="http://blogs.msdn.com/jjameson/archive/tags/Core+Development/default.aspx">Core Development</category><category domain="http://blogs.msdn.com/jjameson/archive/tags/WSS+v3/default.aspx">WSS v3</category><category domain="http://blogs.msdn.com/jjameson/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/jjameson/archive/tags/Infrastructure/default.aspx">Infrastructure</category></item><item><title>Add sysadmin in SQL Server 2008 Using Local Administrator</title><link>http://blogs.msdn.com/jjameson/archive/2009/05/29/add-sysadmin-in-sql-server-2008-using-local-administrator.aspx</link><pubDate>Fri, 29 May 2009 15:44:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9652881</guid><dc:creator>Jeremy Jameson</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jjameson/comments/9652881.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jjameson/commentrss.aspx?PostID=9652881</wfw:commentRss><description>&lt;P&gt;A couple of months ago, I had to SysPrep a copy of one of my VMs in order to remove dependencies on my home domain (I had to work out of the Microsoft office for a couple of days because my DSL router cratered).&lt;/P&gt;
&lt;P&gt;However, while the SysPrep process was quick and painless, after the reboot I found that I could no longer use my domain account to administer SQL Server. Sure, I had local Administrator access, but starting with SQL Server 2005, being a local Admin no longer grants you seamless and unlimited access to SQL Server.&lt;/P&gt;
&lt;P&gt;So the question that I had was: How do I add a user to the &lt;STRONG&gt;sysadmin&lt;/STRONG&gt; role in SQL Server 2008 if all I have is local Administrator access?&lt;/P&gt;
&lt;P&gt;Based on my experience with SQL Server 2005, I remembered this was done as part of the installation using the Surface Area Configuration wizard. However, I couldn't find the equivalent in SQL Server 2008. The SQL Server Books Online clearly stated that the Surface Area Configuration tool was discontinued, but the "Replacement" information didn't help me in the slightest.&lt;/P&gt;
&lt;P&gt;I tried an Internet search (using both Windows Live Search and Google) for:&lt;/P&gt;
&lt;BLOCKQUOTE class=directQuote&gt;add sysadmin SQL Server 2008&lt;/BLOCKQUOTE&gt;
&lt;P&gt;However, neither search returned anything useful (at least not within the first two pages of results -- which is typically my threshold before I move on to a different approach to solving a problem).&lt;/P&gt;
&lt;P&gt;Fortunately for me, I didn't have to wait long for a response to my inquiry to one of the internal discussion lists. David Browne, a Technology Architect here at Microsoft, pointed me to the following:&lt;/P&gt;
&lt;DIV class=reference&gt;&lt;CITE&gt;Troubleshooting: Connecting to SQL Server When System Administrators Are Locked Out&lt;/CITE&gt; 
&lt;DIV class=referenceLink&gt;&lt;A href="http://msdn.microsoft.com/en-us/library/dd207004.aspx" mce_href="http://msdn.microsoft.com/en-us/library/dd207004.aspx"&gt;http://msdn.microsoft.com/en-us/library/dd207004.aspx&lt;/A&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;I wish that would have come up in my search!&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9652881" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jjameson/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Backing Up User Databases in SQL Server (and SQL Server Express)</title><link>http://blogs.msdn.com/jjameson/archive/2008/05/30/backing-up-user-databases-in-sql-server-and-sql-server-express.aspx</link><pubDate>Fri, 30 May 2008 17:22:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8563270</guid><dc:creator>Jeremy Jameson</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jjameson/comments/8563270.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jjameson/commentrss.aspx?PostID=8563270</wfw:commentRss><description>&lt;P&gt;Since I appear to be on a roll with my blog this morning, I figured that I should write one more post about SQL Server before I get back to my "day job."&lt;/P&gt;
&lt;P&gt;I typically use SQL Server Management Studio to configure and schedule database backups, because the&lt;STRONG&gt; Maintenance Plan Wizard&lt;/STRONG&gt; makes&lt;STRONG&gt; &lt;/STRONG&gt;it very quick and easy to click through a few screens and select the appropriate options. However, on some of the servers running in the &lt;A href="http://blogs.msdn.com/jjameson/archive/2009/09/14/the-jameson-datacenter.aspx" mce_href="http://blogs.msdn.com/jjameson/archive/2009/09/14/the-jameson-datacenter.aspx"&gt;"Jameson Datacenter"&lt;/A&gt; (a.k.a. my basement), I only have SQL Server Express installed, not the full SQL Server product. For example, on my server that runs Windows Server Update Services (WSUS), I use SQL Server Express as the "backend" storage solution, because for this particular scenario, I want to keep the database local instead of relying on a separate server. However, I still want to ensure that I have periodic backups of the databases.&lt;/P&gt;
&lt;P&gt;In order to make this as painless as possible, I wrote the following script:&lt;/P&gt;
&lt;DIV class=codeBlock&gt;&lt;PRE&gt;&lt;CODE&gt;&lt;SPAN style="COLOR: #0000ff"&gt;USE&lt;/SPAN&gt; [Tools]
GO
&lt;SPAN style="COLOR: #008000"&gt;/****** Object:  StoredProcedure [dbo].[BackupUserDatabases]    Script Date: 03/15/2007 07:55:44 ******/
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;SET&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;ANSI_NULLS&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;ON
&lt;/SPAN&gt;GO
&lt;SPAN style="COLOR: #0000ff"&gt;SET&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;QUOTED_IDENTIFIER&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;ON
&lt;/SPAN&gt;GO
&lt;SPAN style="COLOR: #0000ff"&gt;CREATE&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;PROC&lt;/SPAN&gt; [dbo]&lt;SPAN style="COLOR: #808080"&gt;.&lt;/SPAN&gt;[BackupUserDatabases]
&lt;SPAN style="COLOR: #808080"&gt;(
&lt;/SPAN&gt;    @backupType &lt;SPAN style="COLOR: #0000ff"&gt;VARCHAR&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;15&lt;SPAN style="COLOR: #808080"&gt;)
)
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;AS

DECLARE&lt;/SPAN&gt; @backupFolder &lt;SPAN style="COLOR: #0000ff"&gt;VARCHAR&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;255&lt;SPAN style="COLOR: #808080"&gt;)

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;SET&lt;/SPAN&gt; @backupFolder &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'C:\BackedUp\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\'&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; @backupType

&lt;SPAN style="COLOR: #0000ff"&gt;DECLARE&lt;/SPAN&gt; @timestamp &lt;SPAN style="COLOR: #0000ff"&gt;DATETIME
DECLARE&lt;/SPAN&gt; @dateString &lt;SPAN style="COLOR: #0000ff"&gt;VARCHAR&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;8&lt;SPAN style="COLOR: #808080"&gt;)
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;DECLARE&lt;/SPAN&gt; @timeString &lt;SPAN style="COLOR: #0000ff"&gt;VARCHAR&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;12&lt;SPAN style="COLOR: #808080"&gt;)

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;SELECT&lt;/SPAN&gt; @timestamp &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff00ff"&gt;GETDATE&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;()

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;SELECT&lt;/SPAN&gt; @dateString &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff00ff"&gt;CONVERT&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;VARCHAR&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;8&lt;SPAN style="COLOR: #808080"&gt;),&lt;/SPAN&gt; @timestamp&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; 112&lt;SPAN style="COLOR: #808080"&gt;)

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;SELECT&lt;/SPAN&gt; @timeString &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff00ff"&gt;CONVERT&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;VARCHAR&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;12&lt;SPAN style="COLOR: #808080"&gt;),&lt;/SPAN&gt; @timestamp&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; 14&lt;SPAN style="COLOR: #808080"&gt;)

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #008000"&gt;-- Remove seconds from timestamp
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;SET&lt;/SPAN&gt; @timeString &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;LEFT(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff00ff"&gt;REPLACE&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;@timeString&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;':'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;''&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;),&lt;/SPAN&gt; 4&lt;SPAN style="COLOR: #808080"&gt;)

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;DECLARE&lt;/SPAN&gt; @databases &lt;SPAN style="COLOR: #0000ff"&gt;TABLE
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(
&lt;/SPAN&gt;    ID &lt;SPAN style="COLOR: #0000ff"&gt;INT&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;IDENTITY&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt; 1&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; 1 &lt;SPAN style="COLOR: #808080"&gt;)
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; DatabaseName &lt;SPAN style="COLOR: #0000ff"&gt;SYSNAME
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;)

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;INSERT&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;INTO
&lt;/SPAN&gt;    @databases 
    &lt;SPAN style="COLOR: #0000ff"&gt;SELECT&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;name
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;FROM&lt;/SPAN&gt; master&lt;SPAN style="COLOR: #808080"&gt;.&lt;/SPAN&gt;dbo&lt;SPAN style="COLOR: #808080"&gt;.&lt;/SPAN&gt;sysdatabases 
    &lt;SPAN style="COLOR: #0000ff"&gt;WHERE&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;name&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;NOT&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;IN&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'master'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'model'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'msdb'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'tempdb'&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;)

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;DECLARE&lt;/SPAN&gt; @id &lt;SPAN style="COLOR: #0000ff"&gt;TINYINT
SELECT&lt;/SPAN&gt; @id &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff00ff"&gt;MIN&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt; ID &lt;SPAN style="COLOR: #808080"&gt;)&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;FROM&lt;/SPAN&gt; @databases

&lt;SPAN style="COLOR: #0000ff"&gt;WHILE&lt;/SPAN&gt; @id &lt;SPAN style="COLOR: #808080"&gt;IS&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;NOT&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;NULL&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;BEGIN
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;DECLARE&lt;/SPAN&gt; @databaseName &lt;SPAN style="COLOR: #0000ff"&gt;SYSNAME
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;SELECT&lt;/SPAN&gt; @databaseName &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; DatabaseName &lt;SPAN style="COLOR: #0000ff"&gt;FROM&lt;/SPAN&gt; @databases &lt;SPAN style="COLOR: #0000ff"&gt;WHERE&lt;/SPAN&gt; ID &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; @id

    &lt;SPAN style="COLOR: #0000ff"&gt;DECLARE&lt;/SPAN&gt; @backupFileName &lt;SPAN style="COLOR: #0000ff"&gt;VARCHAR&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;512&lt;SPAN style="COLOR: #808080"&gt;)
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;SELECT&lt;/SPAN&gt; @backupFileName &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; @databaseName &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'_backup_'&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt;@dateString &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; @timeString &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'.bak'

&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;IF&lt;/SPAN&gt; @backupType &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'Full'
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;BEGIN
&lt;/SPAN&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;EXEC&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;'BACKUP DATABASE ['&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; @databaseName &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'] TO DISK ='''&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; @backupFolder &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'\'&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; @BackupFileName &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;''''&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;)
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;END
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;ELSE&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;IF&lt;/SPAN&gt; @backupType &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'Differential'
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;BEGIN
&lt;/SPAN&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;EXEC&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;'BACKUP DATABASE ['&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; @databaseName &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'] TO DISK ='''&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; @backupFolder &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'\'&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; @BackupFileName &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;''' WITH DIFFERENTIAL'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;)
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;END
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;ELSE&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;IF&lt;/SPAN&gt; @backupType &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'Transaction Log'
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;BEGIN
&lt;/SPAN&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;DECLARE&lt;/SPAN&gt; @recoveryModel &lt;SPAN style="COLOR: #0000ff"&gt;SQL_VARIANT

&lt;/SPAN&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;SELECT&lt;/SPAN&gt; @recoveryModel &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff00ff"&gt;DATABASEPROPERTYEX&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;@databaseName&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'Recovery'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;)

&lt;/SPAN&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;IF&lt;/SPAN&gt; @recoveryModel &lt;SPAN style="COLOR: #808080"&gt;&amp;lt;&amp;gt;&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'SIMPLE'
&lt;/SPAN&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;BEGIN
&lt;/SPAN&gt;            &lt;SPAN style="COLOR: #0000ff"&gt;EXEC&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;'BACKUP LOG ['&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; @databaseName &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'] TO DISK ='''&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; @backupFolder &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'\'&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; @BackupFileName &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;''''&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;)
&lt;/SPAN&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;END
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;END
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;ELSE
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;BEGIN
&lt;/SPAN&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;RAISERROR&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;&lt;SPAN style="COLOR: #ff0000"&gt;'Invalid backup type'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; 16&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; 1&lt;SPAN style="COLOR: #808080"&gt;)
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;END

&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;DELETE&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;FROM&lt;/SPAN&gt; @databases &lt;SPAN style="COLOR: #0000ff"&gt;WHERE&lt;/SPAN&gt; ID &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; @id

    &lt;SPAN style="COLOR: #0000ff"&gt;SELECT&lt;/SPAN&gt; @id &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff00ff"&gt;MIN&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt; ID &lt;SPAN style="COLOR: #808080"&gt;)&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;FROM&lt;/SPAN&gt; @databases
&lt;SPAN style="COLOR: #0000ff"&gt;END&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Well, technically, it's a stored procedure, but nevertheless I still keep the script to generate the sproc in my toolbox: &lt;STRONG&gt;BackupUserDatabases.sql&lt;/STRONG&gt;. Notice that I create the sproc in a separate database (I arbitrarily chose the name &lt;STRONG&gt;Tools&lt;/STRONG&gt;).&lt;/P&gt;
&lt;P&gt;I can then schedule full, differential, and transaction log backups using scheduled tasks, as shown below.&lt;/P&gt;
&lt;DIV class=image&gt;&lt;IMG title="" alt="" src="http://blogs.msdn.com/photos/jjameson/images/8563260/500x99.aspx" width=500 height=99 mce_src="http://blogs.msdn.com/photos/jjameson/images/8563260/500x99.aspx"&gt; 
&lt;DIV class=caption&gt;Figure 1: Scheduled tasks for backing up databases&lt;/DIV&gt;
&lt;DIV class=imageLink&gt;&lt;A href="http://blogs.msdn.com/photos/jjameson/images/8563260/original.aspx" target=_blank mce_href="http://blogs.msdn.com/photos/jjameson/images/8563260/original.aspx"&gt;See full-sized image.&lt;/A&gt; &lt;/DIV&gt;&lt;/DIV&gt;
&lt;P&gt;Here is the command behind one of the scheduled tasks (you can easily deduce the others):&lt;/P&gt;
&lt;DIV class=consoleBlock&gt;&lt;SAMP&gt;"C:\Program Files\Microsoft SQL Server\90\Tools\Binn\SQLCMD.EXE" -S .\SQLExpress -d Tools -Q&lt;BR&gt;&amp;nbsp;&amp;nbsp; "EXEC BackupUserDatabases @backupType='Full'"&lt;/SAMP&gt; &lt;/DIV&gt;
&lt;P&gt;Lastly, note that I have a separate server periodically ROBOCOPY the backup files off of this server to another location -- just in case the WSUS server happens to catch on fire or some other act of God completely wipes out the local database backups ;-)&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8563270" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jjameson/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://blogs.msdn.com/jjameson/archive/tags/WSUS/default.aspx">WSUS</category></item><item><title>Counting Rows in All Database Tables in SQL Server</title><link>http://blogs.msdn.com/jjameson/archive/2008/05/30/counting-rows-in-all-database-tables.aspx</link><pubDate>Fri, 30 May 2008 16:56:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8563190</guid><dc:creator>Jeremy Jameson</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/jjameson/comments/8563190.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jjameson/commentrss.aspx?PostID=8563190</wfw:commentRss><description>&lt;P&gt;Here is yet another of the SQL scripts that I like to keep handy in my toolbox: &lt;STRONG&gt;Count Records in All Tables.sql&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Sometimes when I get "dropped into" a consulting situation with a new customer, I need to quickly get acquainted with one or more of their SQL Server databases. One of the first things I usually like to know is: "What are the largest tables in the database in terms of the number of rows?"&lt;/P&gt;
&lt;P&gt;While you could certainly craft some SQL to &lt;CODE&gt;SELECT COUNT(*)&lt;/CODE&gt; from each user table, this is very inefficient. A much better way is to simply query the system tables as shown below:&lt;/P&gt;
&lt;DIV class=codeBlock&gt;&lt;PRE&gt;&lt;CODE&gt;&lt;SPAN style="COLOR: #0000ff"&gt;SELECT
&lt;/SPAN&gt;    sysobjects&lt;SPAN style="COLOR: #808080"&gt;.&lt;/SPAN&gt;Name
    &lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; sysindexes&lt;SPAN style="COLOR: #808080"&gt;.&lt;/SPAN&gt;Rows
&lt;SPAN style="COLOR: #0000ff"&gt;FROM
&lt;/SPAN&gt;    sysobjects
    &lt;SPAN style="COLOR: #808080"&gt;INNER&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;JOIN&lt;/SPAN&gt; sysindexes
    &lt;SPAN style="COLOR: #0000ff"&gt;ON&lt;/SPAN&gt; sysobjects&lt;SPAN style="COLOR: #808080"&gt;.&lt;/SPAN&gt;id &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; sysindexes&lt;SPAN style="COLOR: #808080"&gt;.&lt;/SPAN&gt;id
&lt;SPAN style="COLOR: #0000ff"&gt;WHERE
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;type&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'U'
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #808080"&gt;AND&lt;/SPAN&gt; sysindexes&lt;SPAN style="COLOR: #808080"&gt;.&lt;/SPAN&gt;IndId &lt;SPAN style="COLOR: #808080"&gt;&amp;lt;&lt;/SPAN&gt; 2
&lt;SPAN style="COLOR: #0000ff"&gt;ORDER&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;BY
&lt;/SPAN&gt;    sysobjects&lt;SPAN style="COLOR: #808080"&gt;.&lt;/SPAN&gt;Namecode&lt;/CODE&gt;&lt;/PRE&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8563190" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jjameson/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Shrinking All Database Files in SQL Server</title><link>http://blogs.msdn.com/jjameson/archive/2008/05/30/shrinking-all-database-files.aspx</link><pubDate>Fri, 30 May 2008 16:50:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8563139</guid><dc:creator>Jeremy Jameson</dc:creator><slash:comments>6</slash:comments><comments>http://blogs.msdn.com/jjameson/comments/8563139.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jjameson/commentrss.aspx?PostID=8563139</wfw:commentRss><description>&lt;P&gt;Here is another SQL script that I keep handy in my toolbox: &lt;STRONG&gt;Shrink All Database Files.sql&lt;/STRONG&gt;. Unlike the script that I shared in my &lt;A class="" href="http://blogs.msdn.com/jjameson/archive/2008/05/30/truncating-all-transaction-logs.aspx" mce_href="http://blogs.msdn.com/jjameson/archive/2008/05/30/truncating-all-transaction-logs.aspx"&gt;previous post&lt;/A&gt; that simply truncated all transaction logs to free up disk space, this script is suitable for running in a Production environment (PROD), as well as in non-production environments, such as a shared Development environment (DEV).&lt;/P&gt;
&lt;P&gt;Here is the script:&lt;/P&gt;
&lt;DIV class=codeBlock&gt;&lt;PRE&gt;&lt;CODE&gt;&lt;SPAN style="COLOR: #0000ff"&gt;DROP&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;TABLE&lt;/SPAN&gt; #CommandQueue

&lt;SPAN style="COLOR: #0000ff"&gt;CREATE&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;TABLE&lt;/SPAN&gt; #CommandQueue
&lt;SPAN style="COLOR: #808080"&gt;(
&lt;/SPAN&gt;    ID &lt;SPAN style="COLOR: #0000ff"&gt;INT&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;IDENTITY&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt; 1&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; 1 &lt;SPAN style="COLOR: #808080"&gt;)
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; SqlStatement &lt;SPAN style="COLOR: #0000ff"&gt;VARCHAR&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;1000&lt;SPAN style="COLOR: #808080"&gt;)
)

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;INSERT&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;INTO&lt;/SPAN&gt;    #CommandQueue
&lt;SPAN style="COLOR: #808080"&gt;(
&lt;/SPAN&gt;    SqlStatement
&lt;SPAN style="COLOR: #808080"&gt;)
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;SELECT
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #ff0000"&gt;'USE ['&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; A&lt;SPAN style="COLOR: #808080"&gt;.&lt;/SPAN&gt;name &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'] DBCC SHRINKFILE (N'''&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; B&lt;SPAN style="COLOR: #808080"&gt;.&lt;/SPAN&gt;name &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;''' , 1)'
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;FROM
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #008000"&gt;sys.databases&lt;/SPAN&gt; A
    &lt;SPAN style="COLOR: #808080"&gt;INNER&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;JOIN&lt;/SPAN&gt; &lt;SPAN style="COLOR: #008000"&gt;sys.master_files&lt;/SPAN&gt; B
    &lt;SPAN style="COLOR: #0000ff"&gt;ON&lt;/SPAN&gt; A&lt;SPAN style="COLOR: #808080"&gt;.&lt;/SPAN&gt;database_id &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; B&lt;SPAN style="COLOR: #808080"&gt;.&lt;/SPAN&gt;database_id
&lt;SPAN style="COLOR: #0000ff"&gt;WHERE
&lt;/SPAN&gt;    A&lt;SPAN style="COLOR: #808080"&gt;.&lt;/SPAN&gt;name &lt;SPAN style="COLOR: #808080"&gt;NOT&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;IN&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'master'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'model'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'msdb'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'tempdb'&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;)

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;DECLARE&lt;/SPAN&gt; @id &lt;SPAN style="COLOR: #0000ff"&gt;INT

SELECT&lt;/SPAN&gt; @id &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff00ff"&gt;MIN&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;ID&lt;SPAN style="COLOR: #808080"&gt;)
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;FROM&lt;/SPAN&gt; #CommandQueue

&lt;SPAN style="COLOR: #0000ff"&gt;WHILE&lt;/SPAN&gt; @id &lt;SPAN style="COLOR: #808080"&gt;IS&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;NOT&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;NULL
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;BEGIN
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;DECLARE&lt;/SPAN&gt; @sqlStatement &lt;SPAN style="COLOR: #0000ff"&gt;VARCHAR&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;1000&lt;SPAN style="COLOR: #808080"&gt;)
&lt;/SPAN&gt;    
    &lt;SPAN style="COLOR: #0000ff"&gt;SELECT
&lt;/SPAN&gt;        @sqlStatement &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; SqlStatement
    &lt;SPAN style="COLOR: #0000ff"&gt;FROM
&lt;/SPAN&gt;        #CommandQueue
    &lt;SPAN style="COLOR: #0000ff"&gt;WHERE
&lt;/SPAN&gt;        ID &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; @id

    &lt;SPAN style="COLOR: #0000ff"&gt;PRINT&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'Executing '''&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; @sqlStatement &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'''...'

&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;EXEC&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;@sqlStatement&lt;SPAN style="COLOR: #808080"&gt;)

&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;DELETE&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;FROM&lt;/SPAN&gt; #CommandQueue
    &lt;SPAN style="COLOR: #0000ff"&gt;WHERE&lt;/SPAN&gt; ID &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; @id

    &lt;SPAN style="COLOR: #0000ff"&gt;SELECT&lt;/SPAN&gt; @id &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff00ff"&gt;MIN&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;ID&lt;SPAN style="COLOR: #808080"&gt;)
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;FROM&lt;/SPAN&gt; #CommandQueue
&lt;SPAN style="COLOR: #0000ff"&gt;END&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;As you can see, this script follows the same pattern that I described in my previous post.&lt;/P&gt;
&lt;P&gt;I have found this script to be especially useful when working with Microsoft Office SharePoint Server (MOSS) 2007, because I sometimes migrate large amounts of content when working on certain features (particularly Search) but later decide to remove the content and need to recover the disk space on my VM.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8563139" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jjameson/archive/tags/MOSS+2007/default.aspx">MOSS 2007</category><category domain="http://blogs.msdn.com/jjameson/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Truncating All Transaction Logs in SQL Server</title><link>http://blogs.msdn.com/jjameson/archive/2008/05/30/truncating-all-transaction-logs.aspx</link><pubDate>Fri, 30 May 2008 16:32:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8563102</guid><dc:creator>Jeremy Jameson</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/jjameson/comments/8563102.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jjameson/commentrss.aspx?PostID=8563102</wfw:commentRss><description>&lt;P&gt;One of the SQL scripts that I keep handy in my toolbox is &lt;STRONG&gt;Truncate All Transaction Logs.sql&lt;/STRONG&gt;. While I would never recommend running this script in a Production environment (PROD), I find it to be very helpful for periodically freeing up disk space in shared Development environments (DEV) and especially on my local VMs. True, I could instead choose to schedule periodic backups, but then I'd still have to periodically delete the backup files (or, I suppose, I could schedule that as well), but, honestly, I really don't care to put that much effort into managing these environments -- especially since I tend to periodically "nuke" them from time to time to start fresh.&lt;/P&gt;
&lt;P&gt;Here is the script:&lt;/P&gt;
&lt;DIV class=codeBlock&gt;&lt;PRE&gt;&lt;CODE&gt;&lt;SPAN style="COLOR: #0000ff"&gt;DROP&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;TABLE&lt;/SPAN&gt; #CommandQueue

&lt;SPAN style="COLOR: #0000ff"&gt;CREATE&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;TABLE&lt;/SPAN&gt; #CommandQueue
&lt;SPAN style="COLOR: #808080"&gt;(
&lt;/SPAN&gt;    ID &lt;SPAN style="COLOR: #0000ff"&gt;INT&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;IDENTITY&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt; 1&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; 1 &lt;SPAN style="COLOR: #808080"&gt;)
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; SqlStatement &lt;SPAN style="COLOR: #0000ff"&gt;VARCHAR&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;1000&lt;SPAN style="COLOR: #808080"&gt;)
)

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;INSERT&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;INTO&lt;/SPAN&gt; #CommandQueue
&lt;SPAN style="COLOR: #808080"&gt;(
&lt;/SPAN&gt;    SqlStatement
&lt;SPAN style="COLOR: #808080"&gt;)
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;SELECT
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #ff0000"&gt;'BACKUP LOG ['&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;name&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'] WITH TRUNCATE_ONLY'
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;FROM
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #008000"&gt;sys.databases
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;WHERE
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;name&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;NOT&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;IN&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'master'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'model'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'msdb'&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'tempdb'&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;)

&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;DECLARE&lt;/SPAN&gt; @id &lt;SPAN style="COLOR: #0000ff"&gt;INT

SELECT&lt;/SPAN&gt; @id &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff00ff"&gt;MIN&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;ID&lt;SPAN style="COLOR: #808080"&gt;)
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;FROM&lt;/SPAN&gt; #CommandQueue

&lt;SPAN style="COLOR: #0000ff"&gt;WHILE&lt;/SPAN&gt; @id &lt;SPAN style="COLOR: #808080"&gt;IS&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;NOT&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;NULL
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;BEGIN
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;DECLARE&lt;/SPAN&gt; @sqlStatement &lt;SPAN style="COLOR: #0000ff"&gt;VARCHAR&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;1000&lt;SPAN style="COLOR: #808080"&gt;)
&lt;/SPAN&gt;    
    &lt;SPAN style="COLOR: #0000ff"&gt;SELECT
&lt;/SPAN&gt;        @sqlStatement &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; SqlStatement
    &lt;SPAN style="COLOR: #0000ff"&gt;FROM
&lt;/SPAN&gt;        #CommandQueue
    &lt;SPAN style="COLOR: #0000ff"&gt;WHERE
&lt;/SPAN&gt;        ID &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; @id

    &lt;SPAN style="COLOR: #0000ff"&gt;PRINT&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'Executing '''&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; @sqlStatement &lt;SPAN style="COLOR: #808080"&gt;+&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff0000"&gt;'''...'

&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;EXEC&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;@sqlStatement&lt;SPAN style="COLOR: #808080"&gt;)

&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;DELETE&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;FROM&lt;/SPAN&gt; #CommandQueue
    &lt;SPAN style="COLOR: #0000ff"&gt;WHERE&lt;/SPAN&gt; ID &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; @id

    &lt;SPAN style="COLOR: #0000ff"&gt;SELECT&lt;/SPAN&gt; @id &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; &lt;SPAN style="COLOR: #ff00ff"&gt;MIN&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;(&lt;/SPAN&gt;ID&lt;SPAN style="COLOR: #808080"&gt;)
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #0000ff"&gt;FROM&lt;/SPAN&gt; #CommandQueue
&lt;SPAN style="COLOR: #0000ff"&gt;END&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;As you can see, there really isn't much to this script. However, what I really wanted to cover in this post is how I implemented the script, and why I think this is a good pattern for scripts that perform some operation on an arbitrary number of objects in a SQL Server database.&lt;/P&gt;
&lt;P&gt;Notice that the first thing that I do is create a temporary table that I call the "command queue." Okay, actually the first thing I do is drop the temporary table if it exists, just in case I need to run the script multiple times in a single session (not necessarily all that helpful when truncating transaction logs, but remember this is more about the pattern than this particular script).&lt;/P&gt;
&lt;P&gt;While I could certainly choose to bypass the "command queue" and simply execute the SQL statements directly, I find this approach to be much more robust in terms of error handling. If something unexpected happens during the execution of the script, I can easily resume processing after recovering from the error (without having to completely start over again).&lt;/P&gt;
&lt;P&gt;The rest of the script really requires no explanation. You can see that once I've "queued" up all of the commands to be run, I simply process them one at a time in a FIFO (First-In-First-Out) manner -- nothing special there.&lt;/P&gt;
&lt;P&gt;In my &lt;A class="" href="http://blogs.msdn.com/jjameson/archive/2008/05/30/shrinking-all-database-files.aspx" mce_href="http://blogs.msdn.com/jjameson/archive/2008/05/30/shrinking-all-database-files.aspx"&gt;next post&lt;/A&gt;, I share another useful script that follows the same pattern and potentially frees up even more disk space than simply truncating the transaction logs.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8563102" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jjameson/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Copying a SQL Server Database to Another Environment</title><link>http://blogs.msdn.com/jjameson/archive/2007/10/29/copying-a-sql-server-database-to-another-environment.aspx</link><pubDate>Mon, 29 Oct 2007 16:03:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5764128</guid><dc:creator>Jeremy Jameson</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jjameson/comments/5764128.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jjameson/commentrss.aspx?PostID=5764128</wfw:commentRss><description>&lt;P&gt;A couple of weeks ago I was troubleshooting a performance problem with the variations feature in Microsoft Office SharePoint Server (MOSS) 2007 and I needed to copy the content database to another environment for further analysis and testing. An easy (an unobtrusive) way to "snapshot" a database and copy it to another environment is to create a backup with the &lt;STRONG&gt;COPY_ONLY&lt;/STRONG&gt; option:&lt;/P&gt;
&lt;DIV class=codeBlock&gt;&lt;CODE&gt;&lt;PRE&gt;&lt;SPAN style="COLOR: #0000ff"&gt;BACKUP&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;DATABASE&lt;/SPAN&gt; [WSS_Content]
&lt;SPAN style="COLOR: #0000ff"&gt;TO&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;DISK&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; N&lt;SPAN style="COLOR: #ff0000"&gt;'H:\WSS_Content.bak'
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;WITH&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;NOFORMAT&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;NOINIT
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;NAME&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; N&lt;SPAN style="COLOR: #ff0000"&gt;'WSS_Content-Full Database Backup'
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;SKIP&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;NOREWIND&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;NOUNLOAD&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;STATS&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; 10
    &lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;COPY_ONLY&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/CODE&gt;&lt;/DIV&gt;
&lt;P&gt;From SQL Server 2005 Books Online:&lt;/P&gt;
&lt;BLOCKQUOTE class=directQuote&gt;
&lt;P&gt;Taking a backup normally changes the database, in turn affecting other backups and how they are restored. Sometimes, however, a backup must be taken for a special purpose that should not affect the overall backup and restore procedures for the database.&lt;/P&gt;
&lt;P&gt;A data backup is normally a base backup for one or more differential backups taken after it. Microsoft SQL Server 2005 introduces support for creating copy-only backups, which do not affect the normal sequence of backups. Therefore, unlike other backups, a copy-only backup does not impact the overall backup and restore procedures for the database.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In other words, by using the &lt;STRONG&gt;COPY_ONLY&lt;/STRONG&gt; option I avoided screwing up the scheduled differential backups on the database.&lt;/P&gt;
&lt;P&gt;However, there are a couple of issues with this approach:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;You cannot specify the &lt;STRONG&gt;COPY_ONLY &lt;/STRONG&gt;option through the UI in SQL Server Management Studio, but this is no big deal -- you can start by configuring most of the backup options using the UI, script the action to generate the corresponding SQL, and then add the &lt;STRONG&gt;COPY_ONLY &lt;/STRONG&gt;option as shown above&lt;/LI&gt;
&lt;LI&gt;You cannot restore a backup created using the &lt;STRONG&gt;COPY_ONLY &lt;/STRONG&gt;option through the UI in SQL Server Management Studio; in the &lt;STRONG&gt;Restore Database &lt;/STRONG&gt;dialog, when you select the &lt;STRONG&gt;From device&lt;/STRONG&gt; option and then specify the backup file previously created with the &lt;STRONG&gt;COPY_ONLY&lt;/STRONG&gt; option, no backup sets are displayed&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;The second problem was puzzling to me. After specifying my backup file, when I attempted to change to the &lt;STRONG&gt;Options &lt;/STRONG&gt;page, I encountered the following error:&lt;/P&gt;
&lt;BLOCKQUOTE class="directQuote errorMessage"&gt;You must select a restore source. &lt;/BLOCKQUOTE&gt;
&lt;P&gt;When I first encountered this problem, I thought I had a corrupt backup file. However, by once again reverting to SQL instead of the UI, I was able to verify the backup was, in fact, valid:&lt;/P&gt;
&lt;DIV class=codeBlock&gt;&lt;CODE&gt;&lt;PRE&gt;&lt;SPAN style="COLOR: #0000ff"&gt;RESTORE&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;FILELISTONLY
FROM&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;DISK&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; N&lt;SPAN style="COLOR: #ff0000"&gt;'E:\NotBackedUp\Temp\WSS_Content.bak'&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/CODE&gt;&lt;/DIV&gt;
&lt;P mce_keep="true"&gt;To restore from a &lt;STRONG&gt;COPY_ONLY &lt;/STRONG&gt;backup, use a command similar to the following:&lt;/P&gt;
&lt;DIV class=codeBlock&gt;&lt;CODE&gt;&lt;PRE&gt;&lt;SPAN style="COLOR: #0000ff"&gt;RESTORE&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;DATABASE&lt;/SPAN&gt; [WSS_Content_TEST]
&lt;SPAN style="COLOR: #0000ff"&gt;FROM&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;DISK&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; N&lt;SPAN style="COLOR: #ff0000"&gt;'E:\NotBackedUp\Temp\WSS_Content.bak'
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #0000ff"&gt;WITH&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;FILE&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; 1
    &lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;MOVE&lt;/SPAN&gt; N&lt;SPAN style="COLOR: #ff0000"&gt;'WSS_Content'
&lt;/SPAN&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;TO&lt;/SPAN&gt; N&lt;SPAN style="COLOR: #ff0000"&gt;'E:\Microsoft SQL Server\MSSQL.1\MSSQL\Data\WSS_Content_TEST.MDF'
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;MOVE&lt;/SPAN&gt; N&lt;SPAN style="COLOR: #ff0000"&gt;'WSS_Content_Log'
&lt;/SPAN&gt;        &lt;SPAN style="COLOR: #0000ff"&gt;TO&lt;/SPAN&gt; N&lt;SPAN style="COLOR: #ff0000"&gt;'L:\Microsoft SQL Server\MSSQL.1\MSSQL\Data\WSS_Content_TEST_Log.LDF'
&lt;/SPAN&gt;    &lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;NOUNLOAD&lt;/SPAN&gt;&lt;SPAN style="COLOR: #808080"&gt;,&lt;/SPAN&gt; &lt;SPAN style="COLOR: #0000ff"&gt;STATS&lt;/SPAN&gt; &lt;SPAN style="COLOR: #808080"&gt;=&lt;/SPAN&gt; 10&lt;/PRE&gt;&lt;/CODE&gt;&lt;/DIV&gt;
&lt;P&gt;Note that when copying a database from one environment to another, you often need to use the &lt;STRONG&gt;MOVE&lt;/STRONG&gt; option to specify the new location for the data and log files (to account for different disk configurations and available disk space).&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=5764128" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jjameson/archive/tags/MOSS+2007/default.aspx">MOSS 2007</category><category domain="http://blogs.msdn.com/jjameson/archive/tags/SQL+Server/default.aspx">SQL Server</category></item></channel></rss>