简体   繁体   English

Powershell SQLCMD

[英]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. 当-v参数变量powershell中有空格时,我们遇到了Powershell和SQLCMD的问题,powershell无法运行该命令。

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. 上面的语法适用于PS命令行,但在脚本内失败。

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" . Powershell实际上会将参数作为"VAR=Some space"传递给程序。 Maybe sqlcmd stumbles over this. 也许sqlcmd偶然发现了这一点。 By using 通过使用

VAR=`"Some space`"

instead it will get passed as VAR="Some space" . 相反,它将作为VAR="Some space"传递。 Maybe that resolves the problem. 也许可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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