sql distinct语句在powershel脚本中不起作用



我正试图在powershell脚本上运行以下sql命令:我将以下内容设置为变量:

$DBSUM =     select x.[example 1], y.example2, z.example3
from (select count (distinct valueA) AS "example 1"
from dbo.table1
) as x,
(select count (distinct ValueB) AS "example2"
from dbo.table2
) as y,
(select count (distinct ValueC) AS "example3"
from dbo.table3
) as z

在一些其他命令之后,我有以下命令:

$SqlConnectionSUM = New-Object System.Data.SqlClient.SqlConnection
$SqlConnectionSUM.ConnectionString = "Server = $SQLServer; Database = 
$SQLDB; Integrated Security = True"
$SqlCmdSUM = New-Object System.Data.SqlClient.SqlCommand
$SqlCmdSUM.CommandText = $DBSUM
$SqlCmdSUM.Connection = $SqlConnectionSUM
$SqlAdapterSUM = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapterSUM.SelectCommand = $SqlCmdSUM
$DataSetSUM = New-Object System.Data.DataSet
$null = $SqlAdapterSUM.Fill($DataSetSUM) 
$DataSetSUM.Tables[0] | select * -ExcludeProperty RowError, RowState, 
HasErrors, ItemArray,Table | ConvertTo-Html -Head $HtmlHead | Out-File 
"$PSScriptRootfilelocationexample.html"

上面的命令在SQL中非常有效,但当我在powershell上运行时,我会得到错误:

+ ... .example3 from (select count (distinct ValueA) AS "example1 ...
+                                                            ~~~~~~
Unexpected token 'example' in expression or statement.
+ CategoryInfo          : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken

有人能给我建议吗?

谢谢。

感谢Luuk和Astentx的回复。

我现在工作正常,问题是"example 1上的空格

我用_替换了空格,去掉了",t工作得非常好。

结果如下:

$DBSUM =     select x.example_1, y.example2, z.example3
from (select count (distinct valueA) AS example_1
from dbo.table1
) as x,
(select count (distinct ValueB) AS example2
from dbo.table2
) as y,
(select count (distinct ValueC) AS example3
from dbo.table3
) as z

谢谢大家。

最新更新