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

<channel>
	<title>SQLCMD</title>
	<atom:link href="http://www.sqlcmd.org/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sqlcmd.org</link>
	<description>Everything you need to know about SQLCMD utility</description>
	<lastBuildDate>Thu, 05 Jan 2012 22:32:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Create Excel File Using OPENROWSET and XP_CMDSHELL</title>
		<link>http://www.sqlcmd.org/create-excel-file-using-openrowset-and-xp_cmdshell/</link>
		<comments>http://www.sqlcmd.org/create-excel-file-using-openrowset-and-xp_cmdshell/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 11:26:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[OPENROWSET]]></category>
		<category><![CDATA[OPENROWSET  excel file]]></category>
		<category><![CDATA[XP_CMDSHELL]]></category>
		<category><![CDATA[XP_CMDSHELL excel file]]></category>

		<guid isPermaLink="false">http://www.sqlcmd.org/?p=113</guid>
		<description><![CDATA[Create Excel File Using OPENROWSET and XP_CMDSHELL Create PROCEDURE MCA_SP_CREATE_EXCEL @File_Name as varchar(50) = &#8221; AS BEGIN SET NOCOUNT ON declare @File_Name as varchar(50) DECLARE @Cmd varchar(1000) DECLARE @fn varchar(500) DECLARE @provider varchar(100) DECLARE @ExcelString varchar(100) &#8211; New File Name &#8230; <a href="http://www.sqlcmd.org/create-excel-file-using-openrowset-and-xp_cmdshell/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Create Excel File Using OPENROWSET and XP_CMDSHELL</p>
<p><span style="color: rgb(0, 0, 255);" mce_style="color: #0000ff;">Create PROCEDURE MCA_SP_CREATE_EXCEL @File_Name as varchar(50) = &#8221;<br />
AS<br />
BEGIN<br />
SET NOCOUNT ON<br />
declare @File_Name as varchar(50)<br />
DECLARE @Cmd varchar(1000)<br />
DECLARE @fn varchar(500)<br />
DECLARE @provider varchar(100)<br />
DECLARE @ExcelString varchar(100)</span></p>
<p><span mce_style="color: #0000ff;" id="" style="color: rgb(0, 0, 255);">&#8211; New File Name to be created<br />
IF @File_Name = &#8221;<br />
Select @fn = &#8216;C:Test.xls&#8217;<br />
ELSE<br />
Select @fn = &#8216;C:&#8217; + @File_Name + &#8216;.xls&#8217;</span></p>
<p><span mce_style="color: #0000ff;" id="" style="color: rgb(0, 0, 255);">&#8211; FileCopy command string formation<br />
SELECT @Cmd = &#8216;Copy C:Template.xls &#8216; + @fn</span></p>
<p><span mce_style="color: #0000ff;" id="" style="color: rgb(0, 0, 255);">&#8211; FielCopy command execution through Shell Command<br />
EXEC MASTER..XP_CMDSHELL @cmd, NO_OUTPUT</span></p>
<p><span mce_style="color: #0000ff;" id="" style="color: rgb(0, 0, 255);">&#8211; Mentioning the OLEDB Rpovider and excel destination filename<br />
set @provider = &#8216;Microsoft.Jet.OLEDB.4.0&#8242;<br />
set @ExcelString = &#8216;Excel 8.0;Database=&#8217; + @fn</span></p>
<p><span mce_style="color: #0000ff;" id="" style="color: rgb(0, 0, 255);">&#8211; Executing the OPENROWSET Command for copying the select contents to Excel sheet.<br />
exec(&#8216;insert into OPENrowset(&#8221;&#8217; + @provider + &#8221;&#8217;,&#8221;&#8217; + @ExcelString + &#8221;&#8217;,&#8221;SELECT FirstName,LastName,Phone,Address,City,State,Zip FROM [Sheet1$]&#8221;)<br />
select au_fname as FirstName,au_lname as LastName,phone,address,city,State,Zip from authors&#8217;)</span></p>
<p><span mce_style="color: #0000ff;" id="" style="color: rgb(0, 0, 255);">exec(&#8216;insert into OPENrowset(&#8221;&#8217; + @provider + &#8221;&#8217;,&#8221;&#8217; + @ExcelString + &#8221;&#8217;,&#8221;SELECT StoreId, OrderNo, OrderDate, Quantity FROM [Sheet2$]&#8221;)<br />
select stor_id as StoreId,Ord_Num as OrderNo,Ord_Date as OrderDate,qty as Quantity from sales&#8217;)</span></p>
<p><span mce_style="color: #0000ff;" id="" style="color: rgb(0, 0, 255);">SET NOCOUNT OFF<br />
END</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sqlcmd.org/create-excel-file-using-openrowset-and-xp_cmdshell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using XP_CMDSHELL on SQL Server 2005</title>
		<link>http://www.sqlcmd.org/using-xp_cmdshell-on-sql-server-2005/</link>
		<comments>http://www.sqlcmd.org/using-xp_cmdshell-on-sql-server-2005/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 17:50:37 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[XP_CMDSHELL]]></category>
		<category><![CDATA[XP_CMDSHELL SQL 2005]]></category>

		<guid isPermaLink="false">http://www.sqlcmd.org/?p=110</guid>
		<description><![CDATA[So in order to use xp_cmdshell whether you are a sysadmin or a regular user you need to first enable the use of xp_cmdshell.  Doing this is similar to what was done when enabling the dedicated administrator connection.  This can &#8230; <a href="http://www.sqlcmd.org/using-xp_cmdshell-on-sql-server-2005/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>So in order to use xp_cmdshell whether you are a sysadmin or a regular user you need to first enable the use of xp_cmdshell.  Doing this is similar to what was done when enabling the dedicated administrator connection.  This can be done by using sp_configure:</p>
<p><span style="color: #0000ff;">sp_configure &#8216;xp_cmdshell&#8217;, &#8217;1&#8242; </span></p>
<p><strong>Next Steps</strong></p>
<p>* Check whether or not you are using xp_cmdshell for previous versions of SQL Server and enable it if it is still needed when you make the upgrade to SQL 2005<br />
* With a new installation of SQL Server 2005 check to see if you really need this option enabled.  From a security perspective it is best to minimize any risks, so keep this option disabled makes the most sense unless there is a real need.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sqlcmd.org/using-xp_cmdshell-on-sql-server-2005/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is XP_CMDSHELL</title>
		<link>http://www.sqlcmd.org/what-is-xp_cmdshell/</link>
		<comments>http://www.sqlcmd.org/what-is-xp_cmdshell/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 17:41:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[XP_CMDSHELL]]></category>
		<category><![CDATA[XP_CMDSHELL SQL 2000]]></category>
		<category><![CDATA[XP_CMDSHELL SQL 2005]]></category>

		<guid isPermaLink="false">http://www.sqlcmd.org/?p=104</guid>
		<description><![CDATA[XP_CMDSHELL &#8211; extended stored procedure. With this extended stored procedure you have the ability to run any command line process within your SQL script, so you can embed this within your stored procedures, jobs or batch processing. In SQL Server &#8230; <a href="http://www.sqlcmd.org/what-is-xp_cmdshell/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>XP_CMDSHELL &#8211;  extended stored procedure.</p>
<p>With this extended stored procedure you have the ability to <strong>run any command line process within your SQL script</strong>, so you can embed this within your stored procedures, jobs or batch processing.</p>
<p>In<span style="color: #ff0000;"><strong> SQL Server 2000</strong></span> this option <span style="color: #0000ff;"><strong>is enabled by default</strong></span>, but to limit access to using xp_cmdshell only members of the sysadmin server role have default rights.</p>
<p>To extend rights to other users <span style="color: #0000ff;">you can use the GRANT statement to allow execute rights</span>.</p>
<p>With<span style="color: #ff0000;"><strong> SQL Server 2005</strong></span> the use of <span style="color: #0000ff;"><strong>xp_cmdshell is turned off by default</strong></span>. As SQL Server continues to mature Microsoft has put many safeguards in place to minimize the security risks of unwanted code being executed within SQL Server or outside of SQL Server.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sqlcmd.org/what-is-xp_cmdshell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Examples of XP_SMDSHELL Commands</title>
		<link>http://www.sqlcmd.org/examples-of-xp_smdshell-commands/</link>
		<comments>http://www.sqlcmd.org/examples-of-xp_smdshell-commands/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 17:35:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[OSQL]]></category>
		<category><![CDATA[SQLCMD]]></category>
		<category><![CDATA[XP_SMDSHELL]]></category>

		<guid isPermaLink="false">http://www.sqlcmd.org/?p=102</guid>
		<description><![CDATA[1) XP_CMDSHELL Directory Search Let&#8217;s start out with a simple directory search example. Say you do not have access to the physical SQL Server machine, but you want to see all the files in the &#8220;C:\temp&#8221; directory that have a &#8230; <a href="http://www.sqlcmd.org/examples-of-xp_smdshell-commands/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><strong>1) XP_CMDSHELL Directory Search</strong><br />
</span></span></p>
<p><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;">Let&#8217;s start out with a simple directory search example.  Say you do not have access to the physical SQL Server machine, but you want to <span style="color: #ff0000;">see all the files in the &#8220;C:\temp&#8221; directory that have a &#8220;.sql&#8221; extension</span>.  You can do this by issuing the following command: </span></span></p>
<p><span style="color: #0000ff;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;">exec master.dbo.xp_cmdshell &#8216;dir c:\temp\*.sql&#8217;<br />
</span></span></span></p>
<p><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><strong>2) Create a new directory using XP_CMDSHELL</strong><br />
</span></span></p>
<p><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;">Now that wasn&#8217;t too exciting, so let&#8217;s come up with a more useful example.  Imagine you are in the process of building a new <strong>SQL Agent job</strong> using Enterprise Manager.  <span style="color: #0000ff;">To organize the output from all the steps in this new job, you want the output of each step to go into a common directory on the server, where each step has its own output file.</span> The only problem is the new directory does not exist.  Assume you do not have access to the server, or do not want to walk down the hall to the computer room to create this new directory.  No problem you can just enter the following command in Query Analyzer from the privacy of your office to accomplish creating the new directory: </span></span></p>
<p><span style="color: #0000ff;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;">exec master.dbo.xp_cmdshell &#8216;mkdir &#8220;c:\temp\SQL Agent Output\new_job\&#8221;&#8216;<br />
</span></span></span></p>
<p><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><strong>3) Run DTS package via stored procedure using XP_CMDSHELL</strong><br />
</span></span></p>
<p><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;">As you can see, <span style="color: #ff0000;">it is very easy to execute DOS commands using xp_cmdshell</span>.  So can xp_cmdshell also run executables?  The answer is YES!  Suppose <span style="color: #ff0000;"><strong>you have a DTS Package that you want to run via a stored procedure</strong></span>.  There is no &#8220;sp_executeDTS&#8221; stored procedure, but you can use the following xp_cmdshell command to run your DTS package:</span></span></p>
<p><span style="color: #0000ff;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;">exec master.dbo.xp_cmdshell &#8216;dtsrun -E -Sserver1 -N&#8221;Export Invoices&#8221;&#8216;</span></span></span></p>
<p><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;">This command executed the <span style="color: #ff0000;"><strong>&#8220;dtsrun&#8221;</strong></span> executable with the necessary parameters to run the &#8220;Export Invoices&#8221; DTS package.  Are you starting to see how the xp_cmdshell extented store procedure can provide you with some alternatives and also expands the functionality you can build into a T-SQL script.</span></span></p>
<p><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><strong>4) Execute SQL script via SQLCMD (OSQL) using XP_CMDSHELL</strong><br />
</span></span></p>
<p><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;">For my last example, suppose you have a <span style="color: #ff0000;"><strong>T-SQL script stored on a hard drive that you would like to execute via a number of different stored procedures</strong></span>.  This T-SQL script does a number of T-SQL commands and you want those same commands to be executed in many different stored procedures.  In addition, the <span style="color: #ff0000;">T-SQL script is generated by some other organization on a daily basis</span>, so you are unable to include the lines of code in each stored procedure, without changing the stored procedures daily.  Therefore, you want to use xp_cmdshell to execute the T-SQL script.  To perform this execution you perform the following command in each of your stored procedures: </span></span></p>
<p><span style="color: #0000ff;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;">exec master.dbo.xp_cmdshell &#8216;osql -E -Sserver1 -i c:\temp\nightly.sql&#8217;<br />
</span></span></span></p>
<p><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;">Here I have used xp_cmdshell to execute the SQL Server osql utility to process the T-SQL commands contained in the script file &#8220;c:\temp\nightly.sql&#8221;.   See how easy it is to execute a T-SQL batch script via a stored procedure using the xp_cmdshell extended stored procedure! </span></span></p>
<p><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><strong>5) Execute SQL script via XP_CMDSHELL only if the file exists</strong><br />
</span></span></p>
<p><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;">If you use xp_cmdshell in a script, you need to <span style="color: #0000ff;"><strong>be aware that if the <span style="color: #ff0000;">operating system command fails</span>, your <span style="color: #ff0000;">script <span style="text-decoration: underline;">will not</span> detect that the error occurred</span></strong></span>. If you want to perform something differently if the operating system command fails then you will need to code your xp_cmdshell execution so you can capture the error.  Let me demonstrate what I am talking about.</span></span></p>
<p><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;">Say you want to copy an operating system file before you perform some work, but if the operating system file you want to copy does not exist to copy you do not want to perform the additional work. If you execute xp_cmdshell like so: </span></span></p>
<p><span style="color: #0000ff;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;">exec master.dbo.xp_cmdshell &#8216;copy c:\temp\doesnotexist.txt c:\temp\workfile.txt&#8217;Print &#8216;Copy worked now we can do some more stuff.&#8217;&#8230;<br />
</span></span></span></p>
<p><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;">The &#8220;Print&#8221; statement above will be executed regardless of whether the file to be copied exists or not.  Therefore, if you need to control your logic based on whether or not the &#8220;copy&#8221; command actually worked, then you should code your xp_command shell execution like this: </span></span></p>
<p><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="color: #0000ff;">DECLARE @rc intEXEC @rc = master.dbo.xp_cmdshell &#8216;copy c:\temp\doesnotexist.txt c:\temp\workfile.txt&#8217;print @rcIF @rc &lt;&gt; 0BEGIN  PRINT &#8216;Copy Failure Skip work&#8217;ENDELSEBEGIN  Print &#8216;Copy worked now we can do some more stuff&#8217;  &#8230;.END<br />
</span></p>
<p></span></span></p>
<p><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;">Here I captured the return code of the execution of xp_cmdshell in a variable @rc.  This allows me to then check the value held by variable @rc and then I programmatically determine whether or not some additional code should be run or not.</span></span></p>
<p><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;">When you use the extended stored procedure xp_cmdshell it runs commands in the background.  Because of this, xp_cmdshell &#8220;MUST NOT&#8221; be used to run programs that require user input.  If you try to execute a program that requires user input, the xp_cmdshell process will hang.  The process hangs because the program is waiting for user input, but xp_cmdshell will never display the user interface to allow the user to enter data. </span></span></p>
<p><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;">If you write lots of T-SQL scripts to automate your Database Administration task then you probably already know about the extended stored procedure xp_cmdshell.  This extended stored procedure allows you additional flexibility to get at operation system resources via T-SQL, which is extremely valuable for managing your SQL Server environment.   If you have not yet experimented with the power of xp_cmdshell, then take a few minutes to determine how it allows you to enter command level statements via T-SQL.</span></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sqlcmd.org/examples-of-xp_smdshell-commands/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XP_CMDSHELL &#8211; Extended Stored Procedure</title>
		<link>http://www.sqlcmd.org/xp_cmdshell-extended-stored-procedure/</link>
		<comments>http://www.sqlcmd.org/xp_cmdshell-extended-stored-procedure/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 17:21:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[XP_CMDSHELL]]></category>

		<guid isPermaLink="false">http://www.sqlcmd.org/?p=100</guid>
		<description><![CDATA[&#8220;xp_cmdshell&#8221; is an extended stored procedure provided by Microsoft and stored in the master database. This procedure allows you to issue operating system commands directly to the Windows command shell via T-SQL code. If needed the output of these commands &#8230; <a href="http://www.sqlcmd.org/xp_cmdshell-extended-stored-procedure/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;">&#8220;xp_cmdshell&#8221; is an extended stored procedure provided by Microsoft and stored in the master database.  This procedure allows you to issue operating system commands directly to the Windows command shell via T-SQL code.  If needed the output of these commands will be returned to the calling routine.</span></span></p>
<p><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;">Now not just anyone can run this extended stored procedure. If you want to execute this extended stored procedure, you will either <span style="color: #ff0000;"><strong>need to be a member of the sysadmin role</strong></span>, or have the <span style="color: #ff0000;"><strong>xp_sqlagent_proxy_account</strong></span> set up on your SQL Server.  If a login executing this extended stored procedure is a member of the sysadmin role then the submitted command will run under the security context associated with the SQL Server Service account in which it runs. If the login executing this procedure is not a member of the sysadmin role, then the command uses the xp_sqlagent_proxy_account login security context for determining whether operating system commands can and cannot be run. If there is no xp_sqlagent_proxy_account then using this procedure will fail for all users not in the sysadmin role. </span></span></p>
<p><span style="font-family: Verdana,Arial,Helvetica,sans-serif;"><span style="font-family: Verdana,Arial,Helvetica,sans-serif;">On the surface being able to run this extended stored procedure does not seem like much, but if the MSSQLSERVER service account has <span style="color: #ff0000;">local administration rights</span> then you can <span style="color: #0000ff;">use this extended stored procedure to perform any windows operating system command</span>.  Therefore, under this circumstance the <span style="color: #ff0000;"><strong>xp_cmdshell can create quite a security hole</strong></span>.  Saying that, you need to be careful how you set up your environment to make sure someone does not corrupt your system by using the almighty powerful xp_cmdshell extended stored procedure.   So let&#8217;s look at different examples of ways to use xp_cmdshell. </span></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sqlcmd.org/xp_cmdshell-extended-stored-procedure/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQLCMD Startup Script</title>
		<link>http://www.sqlcmd.org/sqlcmd-startup-script/</link>
		<comments>http://www.sqlcmd.org/sqlcmd-startup-script/#comments</comments>
		<pubDate>Sun, 16 Aug 2009 22:40:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[SQLCMD Script]]></category>
		<category><![CDATA[SQLCMD Startup Script]]></category>
		<category><![CDATA[Startup Script]]></category>

		<guid isPermaLink="false">http://www.sqlcmd.org/?p=97</guid>
		<description><![CDATA[SQLCMD can run a startup script defined by the environment variable SQLCMDINI. Suppose you want to always execute the following query when you are connected to the server: SELECT @@SERVERNAME, @@VERSION Place this command inside the init.sql file and define &#8230; <a href="http://www.sqlcmd.org/sqlcmd-startup-script/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>SQLCMD can run a startup script defined by the environment variable SQLCMDINI.</p>
<p>Suppose you want to always execute the following query when you are connected to the server:</p>
<p><span style="color: #0000ff;">SELECT @@SERVERNAME, @@VERSION</span></p>
<p>Place this command inside the init.sql file and define the following environment variable:</p>
<p><span style="color: #0000ff;">C:\SET SQLCMDINI=c:\sqlscripts\init.sql</span></p>
<p>Then whenever you start a SQLCMD session, the script automatically executes and displays query output listing the server name and full version string.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sqlcmd.org/sqlcmd-startup-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQLCMD Scripting Variables</title>
		<link>http://www.sqlcmd.org/sqlcmd-scripting-variables/</link>
		<comments>http://www.sqlcmd.org/sqlcmd-scripting-variables/#comments</comments>
		<pubDate>Sun, 16 Aug 2009 22:38:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[SQLCMD Scripting Variables]]></category>
		<category><![CDATA[SQLCMD Variables]]></category>

		<guid isPermaLink="false">http://www.sqlcmd.org/?p=95</guid>
		<description><![CDATA[It&#8217;s a good idea to discuss scripting variables at this point before discussing different SQLCMD commands, primarily because certain commands reveal their true power only via leveraging scripting variables. SQLCMD variables are different from T-SQL variables in many aspects. The &#8230; <a href="http://www.sqlcmd.org/sqlcmd-scripting-variables/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s a good idea to discuss scripting variables at this point before discussing different SQLCMD commands, primarily because certain commands reveal their true power only via leveraging scripting variables.</p>
<p>SQLCMD variables are different from T-SQL variables in many aspects. The easiest way of thinking about SQLCMD variables is that they are similar to macros that you can define in many text editors and development environments. Wherever such a variable is referenced, the query editor substitutes it with the text associated with the variable, which makes using them much more convenient then using just T-SQL variables.</p>
<p>Another major benefit of such variables is that <span style="color: #0000ff;"><strong>they are carried forward between multiple batches</strong></span>. If you define a SQLCMD variable on the first batch, you can use it in all subsequent batches (unless it was undefined). Moreover, as you&#8217;ll see in the next sections, these commands can be used to parameterize queries stored in different files.</p>
<p>Scripting variables have the following format: $(&lt;variable name&gt;). The variable name is not case sensitive. Scripting variables can be defined in one of the following ways:</p>
<p>*      via command line option v;<br />
*      through the :SETVAR command described later in this chapter;<br />
*      by defining an environment variable prior to running SQLCMD.</p>
<p>If an environment variable name conflicts with a -v or :setvar definition or command line option, the command line option takes precedence. If a variable is referenced in a script whose value is not defined, an error message is returned and the SQLCMD session terminates. Let&#8217;s consider a very simple but illustrative example of scripting variable usage.</p>
<p>Suppose you want to create a generic script that backs up a database to a specific location on the disk. The following line is going to be placed in the backup.sql script file:</p>
<p><span style="color: #0000ff;">BACKUP DATABASE $(db) TO DISK = &#8220;$(path)\$(db).bak&#8221;</span></p>
<p>Now to use it effectively you can pass in values of two variables on the SQLCMD command line as follows:</p>
<p><span style="color: #0000ff;">C:&gt;SQLCMD ic:\backup.sql -vdb=&#8221;pubs&#8221; path=&#8221;c:\data&#8221;</span></p>
<p>You can see that to back up a different database or back up to a different location, you can execute this script again and again by modifying scripting variables and not touching the script itself.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sqlcmd.org/sqlcmd-scripting-variables/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>:XML [ON/OFF] SQLCMD Command</title>
		<link>http://www.sqlcmd.org/xml-onoff-sqlcmd-command/</link>
		<comments>http://www.sqlcmd.org/xml-onoff-sqlcmd-command/#comments</comments>
		<pubDate>Sun, 16 Aug 2009 22:35:45 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[:XML]]></category>
		<category><![CDATA[:XML [ON/OFF] SQLCMD Command]]></category>
		<category><![CDATA[:XML SQLCMD Command]]></category>

		<guid isPermaLink="false">http://www.sqlcmd.org/?p=93</guid>
		<description><![CDATA[:XML [ON/OFF] When you expect XML data to be coming back from the server, for example because of a FOR XML clause in your query, you should set XML mode to ON via this command, to properly format the output. &#8230; <a href="http://www.sqlcmd.org/xml-onoff-sqlcmd-command/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><strong>:XML [ON/OFF]</strong></p>
<p>When you expect XML data to be coming back from the server, for example because of a FOR XML clause in your query, you should set XML mode to ON via this command, to properly format the output. By default it is set to OFF and thus displays unformatted data in a continuous stream.</p>
<p>One side effect of setting the XML mode to ON is that error messages also appear in XML format. This option does not affect formatting of any other non-XML columns present in the returned data set.</p>
<p>Tips and Tricks</p>
<p>Setting XML mode ON and OFF needs to be done only upon the start of a new batch; otherwise, results are indeterminate. Consequently, XML and regular data results cannot be mixed in the same batch or data of one of these two types becomes unreadable.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sqlcmd.org/xml-onoff-sqlcmd-command/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>:ON ERROR [exit/ignore] SQLCMD Command</title>
		<link>http://www.sqlcmd.org/on-error-exitignore-sqlcmd-command/</link>
		<comments>http://www.sqlcmd.org/on-error-exitignore-sqlcmd-command/#comments</comments>
		<pubDate>Sun, 16 Aug 2009 22:34:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[:ON ERROR]]></category>
		<category><![CDATA[:ON ERROR SQLCMD]]></category>
		<category><![CDATA[SQLCMD command]]></category>

		<guid isPermaLink="false">http://www.sqlcmd.org/?p=91</guid>
		<description><![CDATA[:ON ERROR [exit/ignore] This command instructs SQLCMD on how to handle errors during script execution. The following options are available: *      exit Exits the program with the appropriate error value *      ignore Ignores the error and continues execution. An error &#8230; <a href="http://www.sqlcmd.org/on-error-exitignore-sqlcmd-command/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><strong>:ON ERROR [exit/ignore]</strong></p>
<p>This command instructs SQLCMD on how to handle errors during script execution. The following options are available:</p>
<p>*      exit Exits the program with the appropriate error value<br />
*      ignore Ignores the error and continues execution. An error message is printed.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sqlcmd.org/on-error-exitignore-sqlcmd-command/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>:PERFTRACE /STDERR/STDOUT SQLCMD Command</title>
		<link>http://www.sqlcmd.org/perftrace-stderrstdout-sqlcmd-command/</link>
		<comments>http://www.sqlcmd.org/perftrace-stderrstdout-sqlcmd-command/#comments</comments>
		<pubDate>Sun, 16 Aug 2009 22:34:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[:PERFTRACE SQLCMD]]></category>
		<category><![CDATA[:PERFTRACE SQLCMD Command]]></category>
		<category><![CDATA[SQLCMD command]]></category>

		<guid isPermaLink="false">http://www.sqlcmd.org/?p=89</guid>
		<description><![CDATA[ERFTRACE &#60;filename&#62;/STDERR/STDOUT The ERFTRACE command redirects all performance information (see the discussion of the -p option) that by default goes to standard output.]]></description>
			<content:encoded><![CDATA[<p><strong> <img src='http://www.sqlcmd.org/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> ERFTRACE &lt;filename&gt;/STDERR/STDOUT</strong></p>
<p>The <img src='http://www.sqlcmd.org/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> ERFTRACE command redirects all performance information (see the discussion of the -p option) that by default goes to standard output.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.sqlcmd.org/perftrace-stderrstdout-sqlcmd-command/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

