I am using SQLCMD to deploy a set of scripted Agent jobs. I am running these with a SQLCMD script to calls each script using the :r
syntax.
It looks like this:
:ON ERROR EXIT
:r $(scriptpath)\Script1.sql
:r $(scriptpath)\Script2.sql
:r $(scriptpath)\Script3.sql
Is there anyway to disable variable substitution with these scripts (equivalent of the /x
switch for SQLCMD)?
I want to run Script1.sql, Script2.sql, etc… without variable substitution.
Best Answer
I think I understand the problem. Your SQL Agent scripts have
sqlcmd
variables in them which you do not want to set whilst deploying the scripts, only when the job runs. I imagine you have a job step like this for example:When you run the script you receive an error saying the variable is not defined, eg
There are a couple of workarounds for this.
declare the
sqlcmd
variable in your deployment script and set its value to how the variable would look in the script. So in my simple example I have a variable calledmyVar
so I set its value to"$(myVar)"
like this:Run the
sqlcmd
executable with variable substitution disabled from within SQL Server Management Studio. Placing two exclamation marks (!!) or (bang-bang) before commands insqlcmd
mode allows you to run that command as if from the command-line, egBasically you are just replacing the
:r
for a!!sqlcmd
and it should work.HTH