使用IIF查询中的Powershell ODBC错误

时间:2015-01-06 08:27:50

标签: sql powershell odbc

我正在尝试使用现有的Access数据库查询并将其放入Powershell。

$SQLServer = "SERVERNAME" #use Server\Instance for named SQL instances!
$SQLDBName = "DBname"
[string] $SqlQuery = $("SELECT TOP 5 Item1 as COMPUTER, Item2 as Location FROM dbo.mytable WHERE (((IIf([dbo_mytable].[swMonday]=1,"Mon")) Is Not Null) AND ((dbo_mytable.swGrpResp)="SpecificGroup") AND ((dbo_mytable.swRootObjectType)="Server"))")

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True"

$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection

$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd

$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)

$SqlConnection.Close()

$DataSet.Tables[0] | ft -AutoSize

Powershell会出现以下错误:

Exception calling "Fill" with "1" argument(s): "ERROR [42000] [Microsoft][ODBC SQL Server 
Driver][SQL Server]Incorrect syntax near ')'.
ERROR [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'IIf'."
$null = $da.fill <<<< ($dt)
CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
FullyQualifiedErrorId : DotNetMethodException

2 个答案:

答案 0 :(得分:1)

查询字符串的语法错误。首先,摆脱所有不必要的东西。您不需要在子表达式中放置字符串($()),也不需要将其转换为字符串,因为它已经是字符串。

您获得的错误的实际原因是您不能在双引号字符串中使用未转义的双引号。要么转义嵌套的双引号(`是PowerShell中的转义字符):

$SqlQuery = "SELECT TOP 5 Item1 as COMPUTER, Item2 as Location FROM dbo.mytable WHERE (((IIf([dbo_mytable].[swMonday]=1,`"Mon`")) Is Not Null) AND ((dbo_mytable.swGrpResp)=`"SpecificGroup`") AND ((dbo_mytable.swRootObjectType)=`"Server`"))"

或将其设为单引号字符串(因为您不在其中使用PowerShell变量):

$SqlQuery = 'SELECT TOP 5 Item1 as COMPUTER, Item2 as Location FROM dbo.mytable WHERE (((IIf([dbo_mytable].[swMonday]=1,"Mon")) Is Not Null) AND ((dbo_mytable.swGrpResp)="SpecificGroup") AND ((dbo_mytable.swRootObjectType)="Server"))'

此外,根据documentationIIF()函数正好需要3个参数。你只用两个参数调用它:

IIf([dbo_mytable].[swMonday]=1, "Mon")

所以你需要添加缺少的第三个:

IIf([dbo_mytable].[swMonday]=1, "Mon", "???")

更一般地说:我在您的查询中删除了所有剩余的括号,因为它们不会提高可读性。也许可以将其定义为多行字符串。

$Query = @'
SELECT TOP 5
  Item1 as COMPUTER,
  Item2 as Location
FROM dbo.mytable
WHERE IIf([dbo_mytable].[swMonday]=1, "Mon") Is Not Null AND
  dbo_mytable.swGrpResp = "SpecificGroup" AND
  dbo_mytable.swRootObjectType = "Server"
'@

更具可读性
$SqlQuery = 'SELECT TOP 5 Item1 as COMPUTER, Item2 as Location FROM dbo.mytable WHERE (((IIf([dbo_mytable].[swMonday]=1,"Mon")) Is Not Null) AND ((dbo_mytable.swGrpResp)="SpecificGroup") AND ((dbo_mytable.swRootObjectType)="Server"))'

甚至

$Query = @'
SELECT TOP 5
  Item1 as COMPUTER,
  Item2 as Location
FROM dbo.mytable
WHERE (((IIf([dbo_mytable].[swMonday]=1,"Mon")) Is Not Null) AND
  ((dbo_mytable.swGrpResp)="SpecificGroup") AND
  ((dbo_mytable.swRootObjectType)="Server"))
'@

答案 1 :(得分:0)

经过深入研究,我发现在SQL查询中使用Powershell中的CASE WHEN似乎是一个很好的例子。在这里发布这个应该有人从中受益。

CASE cmptlevel  
WHEN 60 THEN '60 (SQL Server 6.0)'  
WHEN 65 THEN '65 (SQL Server 6.5)'  
WHEN 70 THEN '70 (SQL Server 7.0)'  
WHEN 80 THEN '80 (SQL Server 2000)'  
WHEN 90 THEN '90 (SQL Server 2005)'  
WHEN 100 THEN '100 (SQL Server 2008)' 

取自https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Export-SQL-Data-67bcb690