我创建了一个VIEW以避免嵌套SELECT
语句,因为Powershell脚本在尝试执行时挂起
Powershell won't run nested SQL Query
现在,尝试使用VIEW执行语句时PowerShell脚本挂起
$SQL10 = "SELECT DISTINCT dbo.Computers.Computer_Name,
IP_Address, COUNT(Computer_ID) AS Num_Computer_ID
FROM dbo.Computers, [V_Duplicate_ID]
WHERE dbo.Computers.Computer_Name = [dbo].[V_Duplicate_ID].[Computer_Name]
AND dbo.Computers.COMPUTER_NAME LIKE '%s001'
AND dbo.Computers.IP_Address LIKE '%.100'
GROUP BY dbo.Computers.Computer_Name, IP_Address
ORDER BY Num_Computer_ID DESC"
...
$ws = $wb.Worksheets.Item(10)
$ws.name = "GUP Dup ID"
$qt = $ws.QueryTables.Add("ODBC;DSN=$DSN3;UID=$username;PWD=$password", $ws.Range("A1"), $SQL10)
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查询输出到EXCEL工作表?
更新
我创建了视图
CREATE VIEW V_DUPLICATE_ID AS
SELECT DISTINCT Computer_Name, IP_Address, COUNT(Computer_ID) AS Num_Computer_ID
FROM dbo.Computers
WHERE Computer_Name in (SELECT DISTINCT Computer_Name
FROM dbo.Computers
WHERE dbo.Computers.COMPUTER_NAME LIKE '%s001'
AND dbo.Computers.IP_Address LIKE '%.100'
GROUP BY Computer_Name,IP_Address
HAVING COUNT(DISTINCT Computer_ID) > 1)
AND dbo.Computers.COMPUTER_NAME LIKE '%s001'
AND dbo.Computers.IP_Address LIKE '%.100'
GROUP BY Computer_Name, IP_Address
并更新了脚本
$SQL10 = "SELECT * FROM [V_Duplicate_ID]
ORDER BY Num_Computer_ID DESC"
...
$ws = $wb.Worksheets.Item(10)
$ws.name = "GUP Duplicate ID"
$qt = $ws.QueryTables.Add("ODBC;DSN=$DSN3;UID=$username;PWD=$password", $ws.Range("A1"), $SQL10)
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
}
脚本仍在悬挂。