Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Script Command Line Arguments

Is there a way to get a script file that will be executing via SQLCMD to accept the parms passed in the SQLCMD call..

ie..

SQLCMD (assume the connection is OK..) -V "1/25/2012" -i "ScriptFile.sql"

What would the structure of ScriptFile.sql Look like? Would it start with the same as any other procedure..?

Basically I'm trying to call a text file script with parameters as an SQL command. So far I haven't been able to find any MSDN articles on this one.

like image 960
Mike D Avatar asked Mar 24 '26 22:03

Mike D


1 Answers

OK.. If anyone cares, it Goes Like This...

The SQL Script file which is using the variable uses the variable like so:

Update TableName Set ColumnValue=$(Param) 

--Notice there's no declare, and the format of the parameter uses
$(Param) instead of @Param as a variable...

The Command line looks almost the same. For some reason, the passed value needs to be in single quotes within double quotes, like so:

SQLCMD -S ServerName -d DatabaseName -v Param="'Param'" -i"ScriptFile.sql" 

Use just double quotes around the ScriptFile name, Single within Double for the Parameters.

like image 82
Mike D Avatar answered Mar 26 '26 12:03

Mike D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!