Invoke-Sqlcmd -ServerInstance '.' -Database 'MyDB'
-Query 'EXEC SprocA @param1= "value";EXEC SprocB @param1= "value";'
基本上,我的 Invoke-SqlCmd
正在运行一个调用两个存储过程的查询。这两个存储过程都输出一堆行。
但是,如果 sprocA 不输出任何结果(空选择结果或没有行),则调用命令似乎不会打印第二个 sprocB 的输出,甚至如果有数据的话。
如果我更改 Invoke-SqlCmd
命令查询参数中存储过程的顺序,那么它可以正常工作并返回第一个存储过程的输出。
如果我有三个存储过程调用,其中第一个返回数据,第二个不返回数据,第三个返回数据,则它会打印第一个结果和第三个结果的输出。
基本上,仅当第一个存储过程没有输出时,它才不会打印任何输出。看起来很奇怪。
我能做些什么来解决这个 SQL 问题吗?可能是 PowerShell 的事情?
我还可以使用两个 Select
语句重现这一点,其中一个返回数据,另一个不返回数据。
最佳答案
这是 invoke-sqlcmd 的记录行为
When this cmdlet is run, the first result set that the script returns is displayed as a formatted table. If subsequent result sets contain different column lists than the first, those result sets are not displayed. If subsequent result sets after the first set have the same column list, their rows are appended to the formatted table that contains the rows that were returned by the first result set.
看起来两个结果集都确实返回了,但默认不输出。
EG
PS C:\> Invoke-SqlCmd "select 1 a; select 2 b, 3 c;" | % { $_ | Out-Default }
输出
a
-
1
b c
- -
2 3
关于sql-server - invoke-sqlcmd 失败显示某些类型的查询没有结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52913891/