Question

Based on this link http://msdn.microsoft.com/en-us/library/ms170572.aspx, I successfully execute the SQL that I coded in the SQL file via the following code:

sqlcmd -S myServer\instanceName -i C:\myScript.sql

Now instead of executing a SELECT operation, I would like to have UPDATE operation. I'm wondering how to modify the above code so that it accept parameter and this parameter will be used in myScript.sql?

My sample UPDATE operation will be expected to be something like below:

UPDATE someColumn FROM someTable where tableID = @tableID;

So from the command, I will provide a tableID

Was it helpful?

Solution

There are few methods. Some of them are described here:

http://technet.microsoft.com/en-us/library/ms188714.aspx

sqlcmd -S myServer\instanceName -v tableID ="your_val" -i C:\myScript.sql


UPDATE someColumn FROM someTable where tableID = $(tableID);

OTHER TIPS

With -v parameter

  -v @tableID = 1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top