T-SQL from the commandline - SQLCMD

As part of our ongoing efforts towards one-touch deployment we had the need to run SQL queries from the command-line. Or more specifically to be able to run SQL commands from NAnt.

Back in the day there was a tool called OSQL - nowadays there's SQLCMD. You can download it directly from Microsoft, although you'll also need the SQL Server Native Client (plus Windows Installer 4.5 if you want the 2008 version).

Switches and usage are here.

What this means for us is that we can now update our database with the last build label (our app checks that it is running the latest version). These are the lines from our post-buld NAnt file:


<target name="UpdateSchemaVersion">
<xmlpeek file="D:\Build\MyProject\fullversionlabel.xml" xpath="/fullversionlabel" property="schemaversion"/>

<exec program="C:\Program Files\Microsoft SQL Server\90\Tools\Binn\SQLCMD.exe">
<arg line=" -SMySQLServer -E -dMyDatabase -Q&quot;EXECUTE MyStoredProcedure '${schemaversion}'&quot;"/>
</exec>
</target>

Comments