From msdn I learn, that the Variable Precedence of :Setvar X Y is higher, than that of sqlcmd -v X=Y.

I want to write a script, that uses the variables supplied to sqlcmd with the -v option, but which uses some default values, when run in Management Studio using SQLcmd mode.

Best there would be some way to supply the values to use somehow to the Management Studio without having to provide them in the script.

2 Answers 2


The precedence of setvar is the lowest. Try it out and see if it works.


Declare the sqlcmd variables in a parent script, then call the child script using the :r command, eg

:setvar var1 test1
:setvar var2 test2

:r c:\temp\yourSQLCMDScript.sql

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.