Invoke-Sqlcmd:SQL PowerShell 中不允许重复的列名
Invoke-Sqlcmd : Duplicate column names are not permitted in SQL PowerShell
我在 14 个查询中吐出 Glenn Berry's Performance Query,但其中一个(第 9 个)无法正常工作。
这是我的 PowerShell 代码:
#Provide SQLServerName
$SQLServer ="localhost"
#Provide Database Name
$DatabaseName ="master"
#Prompt for user credentials
$credential = Get-Credential
$Query1= "-- 09 Index Sizes
-- Note: THIS IS SLOW as it reads index blocks. SAMPLED is not that high, but watch for prod I/O impact if using 'DETAILED'
SELECT DB_NAME() AS DatabaseName,
Object_name(i.object_id) AS TableName
,i.index_id, name AS IndexName
,i.type_desc
,ips.page_count, ips.compressed_page_count
,CAST(ips.avg_fragmentation_in_percent as DECIMAL(5,1)) [fragmentation_pct]
,CAST(ips.avg_page_space_used_in_percent as DECIMAL(5,1)) [page_space_used_pct]
,ips.index_depth, ips.page_count ,ips.forwarded_record_count, ips.record_count
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, 'SAMPLED') AS ips -- or SAMPLED
INNER JOIN sys.indexes AS i ON ips.object_id = i.object_id AND ips.index_id = i.index_id
where ips.page_count > 1
ORDER BY ips.record_count desc;"
Invoke-Sqlcmd -Database $DatabaseName -ServerInstance $Server -Credential $credential -Query $Query1 | Format-Table
返回的错误说:
Invoke-Sqlcmd : Duplicate column names are not permitted in SQL PowerShell. To repeat a column, use a column alias for the duplicate
column in the format Column_Name AS New_Name.
At C:\Users\FrancescoM\Desktop\CSV\Test.ps1:23 char:1
+ Invoke-Sqlcmd -Database $DatabaseName -ServerInstance $Server -Crede ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SyntaxError: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException
+ FullyQualifiedErrorId : DuplicateColumnNameErrorMessage,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
不确定什么是重复列,因为如果我 运行 在 SSMS 上调用 PowerShell 脚本的查询,我看不到任何重复列:
回到家后,我以适当的方式格式化了查询,感谢 Notepad++,在单击每一列后我发现 ips.page_count
被调用了两次。
所以确实有一个列被调用了两次
为 Invoke-Sqlcmd 命令使用以下参数。
-OutputSqlErrors $False
它将抑制错误。
文档:
https://docs.microsoft.com/en-us/powershell/module/sqlserver/invoke-sqlcmd?view=sqlserver-ps
当查询的结果集中有重复的列名时会出现此错误。
invoke-sqlcmd : SQL PowerShell
中不允许重复列名
假设您使用下面的查询来查看 invoke-sqlcmd 查询中的 result-set-
select 测试,* 来自测试表
现在测试列将在结果中重复,调用 sqlcmd 执行将失败。
要像这样解决这个问题-
select 测试为 testColumn,* 来自 testtable
我在 14 个查询中吐出 Glenn Berry's Performance Query,但其中一个(第 9 个)无法正常工作。
这是我的 PowerShell 代码:
#Provide SQLServerName
$SQLServer ="localhost"
#Provide Database Name
$DatabaseName ="master"
#Prompt for user credentials
$credential = Get-Credential
$Query1= "-- 09 Index Sizes
-- Note: THIS IS SLOW as it reads index blocks. SAMPLED is not that high, but watch for prod I/O impact if using 'DETAILED'
SELECT DB_NAME() AS DatabaseName,
Object_name(i.object_id) AS TableName
,i.index_id, name AS IndexName
,i.type_desc
,ips.page_count, ips.compressed_page_count
,CAST(ips.avg_fragmentation_in_percent as DECIMAL(5,1)) [fragmentation_pct]
,CAST(ips.avg_page_space_used_in_percent as DECIMAL(5,1)) [page_space_used_pct]
,ips.index_depth, ips.page_count ,ips.forwarded_record_count, ips.record_count
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, 'SAMPLED') AS ips -- or SAMPLED
INNER JOIN sys.indexes AS i ON ips.object_id = i.object_id AND ips.index_id = i.index_id
where ips.page_count > 1
ORDER BY ips.record_count desc;"
Invoke-Sqlcmd -Database $DatabaseName -ServerInstance $Server -Credential $credential -Query $Query1 | Format-Table
返回的错误说:
Invoke-Sqlcmd : Duplicate column names are not permitted in SQL PowerShell. To repeat a column, use a column alias for the duplicate
column in the format Column_Name AS New_Name.
At C:\Users\FrancescoM\Desktop\CSV\Test.ps1:23 char:1
+ Invoke-Sqlcmd -Database $DatabaseName -ServerInstance $Server -Crede ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SyntaxError: (:) [Invoke-Sqlcmd], SqlPowerShellSqlExecutionException
+ FullyQualifiedErrorId : DuplicateColumnNameErrorMessage,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
不确定什么是重复列,因为如果我 运行 在 SSMS 上调用 PowerShell 脚本的查询,我看不到任何重复列:
回到家后,我以适当的方式格式化了查询,感谢 Notepad++,在单击每一列后我发现 ips.page_count
被调用了两次。
所以确实有一个列被调用了两次
为 Invoke-Sqlcmd 命令使用以下参数。
-OutputSqlErrors $False
它将抑制错误。
文档: https://docs.microsoft.com/en-us/powershell/module/sqlserver/invoke-sqlcmd?view=sqlserver-ps
当查询的结果集中有重复的列名时会出现此错误。 invoke-sqlcmd : SQL PowerShell
中不允许重复列名假设您使用下面的查询来查看 invoke-sqlcmd 查询中的 result-set-
select 测试,* 来自测试表
现在测试列将在结果中重复,调用 sqlcmd 执行将失败。
要像这样解决这个问题- select 测试为 testColumn,* 来自 testtable