Search code examples
sqlpowershellbatch-filesqlcmd

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.

e.g.

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

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

Thanks,

B


Solution

  • 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.