sqlcmd / Invoke-SqlCmd中的转义变量

时间:2016-02-02 15:12:29

标签: sql-server windows powershell sqlcmd invoke-sqlcmd

我正在使用powershell并使用Invoke-SqlCmd。我能够将变量传递给SQL:

$variables = @( "MyVariable='hello'" )

Invoke-SqlCmd `
    -ServerInstance 'localhost' `
    -Database 'master' `
    -Username 'matthew' `
    -Password 'qwerty' `
    -Query 'SELECT $(MyVariable) AS foo' `
    -Variable $variables

这让我按预期返回hello。但是,如果我的变量的值包含等于(=):

$variables = @("MyVariable='aGVsbG8NCg=='") # base64 encoded 'hello'

它给了我以下错误:

  

用于为Invoke-Sqlcmd cmdlet定义新变量的格式无效。请使用'var = value'格式来定义新变量。

我无法找到关于如何正确转义值的sqlcmdInvoke-SqlCmd的任何文档。

如何转发发送到sqlcmd / Invoke-SqlCmd的变量?

4 个答案:

答案 0 :(得分:8)

在调查后使用了一些反思

  

C:\ Program Files(x86)\ Microsoft SQL Server \ 120 \ Tools \ PowerShell \ Modules \ SQLPS \ Microsoft.SqlServer.Management.PSSnapins.dll

查看Invoke-SqlCmd的构造函数,以下几行显示如果变量定义中有多个等号,它将失败的问题。

我对其他任何尝试使用generateLayout() { return {{x:0,y:0,w:6,h:12},{x:6,y:0,w:6,h:12}}; } 的人的建议是节省您的时间和理智,而只是使用开源替代Invoke-SqlCmd2

微软,请修复。

答案 1 :(得分:1)

我还发现自己需要传递base64编码的信息,其中包含讨厌的'='。对我有用的是将替换标记添加到传递给查询的变量数组中,然后在查询中使用SQL的REPLACE函数将标记替换为'='。

因此您可以将代码更新为如下形式:

$equalsSignReplacement = '[EQUALSIGN]'
$myVariable = 'aGVsbG8NCg=='
$variables = 
    "EqualsSignReplacement=$($equalsSignReplacement)",
    "MyVariable=$($myVariable.Replace('=',$equalsSignReplacement))"

Invoke-SqlCmd `
    -ServerInstance 'localhost' `
    -Database 'master' `
    -Username 'matthew' `
    -Password 'qwerty' `
    -Query 'SELECT REPLACE('$(MyVariable)','$(EqualsSignReplacement)','=') AS foo' `
    -Variable $variables

该解决方案的缺点是您需要主动使用它的应用程序。您需要提前知道哪些变量中可能带有“等号”,然后不仅更新您的powershell代码,还更新您的SQL脚本,以确保替换正确进行。

请确保使用变量唯一的替换标记,以免在查询中意外地用'='替换有效文本。

答案 2 :(得分:1)

使用child: Container( width: MediaQuery.of(context).size.width - 40.0, decoration: BoxDecoration( borderRadius: BorderRadius.circular(12.0), color: Color(0xff5a348b), gradient: LinearGradient( colors: [Color(0xffebac38), Color(0xffde4d2a)], begin: Alignment.centerRight, end: Alignment(-1.0, -2.0)), //Gradient ), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Padding( padding: const EdgeInsets.only(top: 8.0), child: Row( children: <Widget>[ DataTable( columns: <DataColumn>[ DataColumn( label: Text( 'Storage', style: TextStyle( color: Colors.white, fontSize: 16.0, ), ), ), DataColumn( label: Text( '1TB', style: TextStyle( fontWeight: FontWeight.bold, color: Colors.white, fontSize: 16.0, ), ), ), ], rows: <DataRow>[ DataRow(cells: <DataCell>[ DataCell( Text( 'Smart synchronization', style: TextStyle( color: Colors.white, fontSize: 16.0, ), ), ), DataCell( Icon( Icons.add, color: Colors.white54, ), ), ]), DataRow(cells: <DataCell>[ DataCell( Text( 'Full text search', style: TextStyle( color: Colors.white, fontSize: 16.0, ), ), ), DataCell( Icon( Icons.edit, color: Colors.white54, ), ), ]), ], ), ], ), ), ], ), ), 替换等号。

CHAR(61)

答案 3 :(得分:0)

I encounted this issue recently and had success using the answer from TheMadTechnician at:

Replace Single Quotes in PowerShell Or Excel CSV

上的未知道具`params`
  

答案是在字符串中创建子表达式以替换'   用''。

     

Invoke-Sqlcmd -ServerInstance "$SQLServer" -Database "$SqlDB" -query "INSERT INTO dbo.ecca_sw_standard_2 (name, version, product_id) VALUES (N'$($CSVAppName -replace "'", "''")', N'$($CSVVersion -replace "'", "''")' , N'$($CSVAppID -replace "'", "''")')"

     

这种方式的时候   字符串被展开,它将被适当地转义以进行SQL插入。