Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server Powershell不会运行嵌套SQL查询_Sql Server_Powershell_Nested - Fatal编程技术网

Sql server Powershell不会运行嵌套SQL查询

Sql server Powershell不会运行嵌套SQL查询,sql-server,powershell,nested,Sql Server,Powershell,Nested,SQL Server 2012 我有一个Powershell脚本,可以运行查询并输出到EXCEL 如果我执行以下命令 $SQL9 = "SELECT * FROM dbo.Computers WHERE Date_of_Record = CAST(GETDATE() AS DATE) AND dbo.Computers.COMPUTER_NAME LIKE '%s001' AND dbo.Computers.IP_Address LIKE '%.100' ORDER BY Computer_Na

SQL Server 2012

我有一个Powershell脚本,可以运行查询并输出到EXCEL

如果我执行以下命令

$SQL9 = "SELECT *
FROM dbo.Computers
WHERE Date_of_Record = CAST(GETDATE() AS DATE)
AND dbo.Computers.COMPUTER_NAME LIKE '%s001'
AND dbo.Computers.IP_Address LIKE '%.100'
ORDER BY Computer_Name"

$ws = $wb.Worksheets.Item(9)
$ws.name = "GUP"

$qt = $ws.QueryTables.Add("ODBC;DSN=$DSN3;UID=$username;PWD=$password", $ws.Range("A1"), $SQL9)

if ($qt.Refresh()){
    $ws.Activate()
    $ws.Select()
    $excel.Rows.Item(1).HorizontalAlignment = $xlCenter
    $excel.Rows.Item(1).VerticalAlignment = $xlTop
    $excel.Rows.Item("1:1").Font.Name = "Calibri" 
    $excel.Rows.Item("1:1").Font.Size = 11 
    $excel.Rows.Item("1:1").Font.Bold = $true 
 }
它起作用了

但是如果我使用另一个SQL语句,即

$SQL9 = "SELECT Date_of_Record, Computer_Name, IP_Address, Agent_Version
FROM dbo.Computers
WHERE Computer_Name in (SELECT Computer_Name
                        FROM dbo.Computers
                        GROUP BY Computer_Name
                        HAVING COUNT(DISTINCT Agent_Version) > 1)
AND dbo.Computers.COMPUTER_NAME LIKE '%s001'
AND dbo.Computers.IP_Address LIKE '%.100'
ORDER BY Computer_Name, Date_of_Record"
它不起作用。我在sqlmanagementstudio中运行了这个查询,它按照预期工作

我甚至使用了另一个SQL语句并运行了脚本,即

$SQL9 = "SELECT Computer_Name, IP_Address, COUNT(*) AS Number_of_Days_Checked_Past_Month
FROM dbo.Computers
WHERE Date_of_Record > GETDATE() - 30
AND dbo.Computers.COMPUTER_NAME LIKE '%s001'
AND dbo.Computers.IP_Address LIKE '%.100'
Group BY Computer_Name, IP_Address
ORDER BY Number_of_Days_Checked_Past_Month DESC"
它是有效的

为什么我尝试执行时powershell脚本挂起

$SQL9 = "SELECT Date_of_Record, Computer_Name, IP_Address, Agent_Version
FROM dbo.Computers
WHERE Computer_Name in (SELECT Computer_Name
                        FROM dbo.Computers
                        GROUP BY Computer_Name
                        HAVING COUNT(DISTINCT Agent_Version) > 1)
AND dbo.Computers.COMPUTER_NAME LIKE '%s001'
AND dbo.Computers.IP_Address LIKE '%.100'
ORDER BY Computer_Name, Date_of_Record"

是因为存在嵌套的SELECT吗?如何解决此问题?

即使您没有告诉我,我猜问题在于您正在使用ODBC从Excel连接到SQL Server

ODBC不支持嵌套查询。您需要使用不同的方法从Excel访问SQL Server。你能使用OLEDB驱动程序吗

您还可以修改查询,使其不使用子查询。我不知道如何执行。您还可以在服务器上创建视图,以便excel中的查询不使用子查询


请::您可以在联接中使用子查询,并对联接值进行筛选。

什么错误?你还没有贴出来。此外,检查语句是否正确的最快方法是打开SSMS,连接到数据库并运行查询。您不必购买SQL Server来使用管理工具,您可以免费下载工具的快速版本。您会遇到什么错误?我已经添加了一个答案,希望您会遇到ODBC限制。无错误,Powershell在使用嵌套选择时会挂起,我更新了OPI,甚至不知道OLE DB驱动程序。我会研究的。