2

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

3
  • have you tried a %20 instead of the space? Commented Jun 18, 2009 at 20:34
  • 2
    If you're using SQL 2008, I highly recommend using Invoke-SqlCmd instead (along with the other cmdlets & providers). Commented Aug 29, 2009 at 16:05
  • related: stackoverflow.com/questions/9714054/… Commented Jan 31, 2013 at 12:34

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Tried for 4+ hours, even building commands that looked great in echoargs but still failed. This finally did it. PSH rocks <s>.
2

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.