<?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>Serge&#039;s Technology View &#187; sql script</title>
	<atom:link href="http://blog.dragonsoft.us/tag/sql-script/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.dragonsoft.us</link>
	<description>Talk about Technologies, Software Architecture and Management</description>
	<lastBuildDate>Tue, 31 Jan 2012 01:43:34 +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>SQL tasks automation: apply scripts from folder</title>
		<link>http://blog.dragonsoft.us/2009/12/02/sql-tasks-automation-apply-scripts-from-folder/</link>
		<comments>http://blog.dragonsoft.us/2009/12/02/sql-tasks-automation-apply-scripts-from-folder/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 03:18:13 +0000</pubDate>
		<dc:creator>Serguei Dosyukov</dc:creator>
				<category><![CDATA[Fun stuff with SQL Server]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[apply script]]></category>
		<category><![CDATA[apply sql script]]></category>
		<category><![CDATA[batch file]]></category>
		<category><![CDATA[osql]]></category>
		<category><![CDATA[sql automation]]></category>
		<category><![CDATA[sql script]]></category>
		<category><![CDATA[sql server]]></category>
		<guid isPermaLink="false">http://blog.dragonsoft.us/?p=1008</guid>
		<description><![CDATA[You will be amazed how common this question is: I have a folder with arbitrary set of SQL scripts and want to apply them all automatically. How do I do that? In other words: Having a folder &#8211; ex: C:\AutoSQLScripts &#8211; with some &#8230; <a href="http://blog.dragonsoft.us/2009/12/02/sql-tasks-automation-apply-scripts-from-folder/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>You will be amazed how common this question is:</p>
<blockquote><p><strong>I have a folder with arbitrary set of SQL scripts and want to apply them all automatically. How do I do that?</strong></p></blockquote>
<p>In other words:</p>
<ul>
<li>Having a folder &#8211; ex: C:\AutoSQLScripts &#8211; with some unknown set of SQL scripts</li>
<li>And a SQL Instance where scripts need to be applied</li>
<li>Run a scheduled task to perform the operation automatically.</li>
</ul>
<h3>Solution:</h3>
<p>First, lets figure out single operation - run SQL Script from command line in unattended mode using <strong><a href="http://msdn.microsoft.com/en-us/library/aa214012(SQL.80).aspx" target="_blank">osql</a></strong> utility:</p>
<pre class="brush: powershell; title: ; notranslate">::
osql -S MYSERVER -d MYDATABASE -E -i MySqlScript.sql  -o report.txt
::</pre>
<p>Where:</p>
<p><strong>-S</strong> indicates server name<br />
<strong>-d</strong> is an alternative to <em>USE db_name</em> inside the script<br />
<strong>-E</strong> indicates that trusted connection would be used<br />
<strong>-i</strong> identifies the file that contains a batch of SQL statements<br />
<strong>-o</strong> identifies the file that receives output from <strong>osql</strong></p>
<p>Simple so far&#8230;</p>
<p>Fun starts when we need to go through files in the folder. Using &#8220;magic&#8221; of Power Shell, it is also easy:</p>
<pre class="brush: powershell; title: ; notranslate">::
FOR /f &quot;TOKENS=*&quot; %%a IN ('dir /b &quot;%1*.sql&quot;') do ECHO %%a
::or
SET _PATH=%1
IF (NOT %_PATH%==&quot;&quot;) SET _PATH = '-p &quot;%_PATH%&quot;'
FORFILES %_PATH% -s -m *.* -c &quot;CMD /C ECHO @FILE&quot;
::</pre>
<p>Let&#8217;s ignore the end for now and concentrate on the syntax of the loop itself.</p>
<p><strong>FOR /f</strong> indicates that we need to perform a loop against a set of files specified in IN. To produce the set we are using <strong>&#8220;raw&#8221; dir </strong>of the folder, where %1 is, if specified, an input param of the batch file call.</p>
<p><strong>FORFILES</strong> is a little more complex and given here for comparison, yet produces the same result.</p>
<p><strong>Note: </strong><em>commands above work only with &#8220;local&#8221; (drive: based) locations. If you need apply scripts from Network folder, map it first as a local drive then apply scripts.</em></p>
<p>Now let&#8217;s combine SQL call with the batch processing and we would get the following <strong>ApplyScript.cmd</strong> which could be used to apply any scripts from current or specific folder to specified SQL Server/Database instance:</p>
<pre class="brush: powershell; title: ; notranslate">::
@ECHO OFF
SET _SERVER=MYSERVER
SET _DB=MYDATABASE
FOR /f &quot;TOKENS=*&quot; %%a IN ('dir /b %1*.sql') DO CALL osql -S %_SERVER% -d %_DB% -E -i %%a  -o report.txt
::</pre>
<p>Enjoy.</p>
<hr/><span style="font-size: 7pt">Copyright &copy; 2012 <strong><a href="http://blog.dragonsoft.us">Serge&#039;s Technology View</a></strong>. This Feed is for personal non-commercial use only.</span>]]></content:encoded>
			<wfw:commentRss>http://blog.dragonsoft.us/2009/12/02/sql-tasks-automation-apply-scripts-from-folder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

