简体   繁体   中英

Powershell SQLCMD

We were experiencing problems with Powershell and SQLCMD, when there was sapces in the -v parameter variable powershell wouldn't run the command.

eg

sqlcmd ... -v VAR="Some space"

Has anyone experienced this before or know how to fix the problem?

Thanks,

B

The syntax above works for the PS commandline but fails within a script.

We struggled a long time with how to make this work. One of our very clever QA guys finally came up with the following:

$variableWithSpaces="one two three"
$mySqlCmd = "sqlcmd -E -S $dbServer -i $script  -v var=```"$variableWithSpaces```" "
Invoke-Expression $mySqlCmd 

Plug ugly but it works.

Powershell will actually pass the parameter to the program as "VAR=Some space" . Maybe sqlcmd stumbles over this. By using

VAR=`"Some space`"

instead it will get passed as VAR="Some space" . Maybe that resolves the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM