I've read the documentation (sqlcmd Utility), but I can't get sqlcmd's -v parameter to work as expected.
Assuming this SQL script (echo.sql):
:setvar THE_PATH C:\Users\Craig\Desktop
PRINT 'THE_PATH: $(THE_PATH)'
:setvar THE_TOP 10
PRINT 'THE_TOP: $(THE_TOP)'
When run at the PowerShell prompt without the -v argument set:
PS> sqlcmd -E -S 'server' -d 'database' -i '.\echo.sql'
THE_PATH: C:\Users\Craig\Desktop
THE_TOP: 10
Setting the numeric variable (THE_TOP) are ignored:
PS> sqlcmd -E -S 'server' -d 'database' -i '.\echo.sql' -v THE_TOP=5
PS> sqlcmd -E -S 'server' -d 'database' -i '.\echo.sql'
THE_PATH: C:\Users\Craig\Desktop
THE_TOP: 10
If I eliminate the default value for THE_TOP in echo.sql, it reinforced the assertion that the parameter is being ignored:
:setvar THE_TOP
PRINT 'THE_TOP: $(THE_TOP)'
PS> sqlcmd -E -S 'server' -d 'database' -i '.\echo.sql' -v THE_TOP=5
THE_PATH: C:\Users\Craig\Desktop
THE_TOP: $(THE_TOP)
If I attempt to set the THE_PATH parameter, I get:
PS> sqlcmd -E -S 'server' -d 'database' -i '.\echo.sql' -v THE_PATH="C:\path"
Sqlcmd: ':\path': Invalid argument. Enter '-?' for help.
What is the correct -v syntax?
Invoke-Sqlcmdcmdlet. If you don't want to, pass an array to the-vparameter as described in the not accepted answer of this thread: stackoverflow.com/questions/12120106/…-v THE_PATH=`"C:\path`".