sql-server - invoke-sqlcmd 失败显示某些类型的查询没有结果

标签 sql-server powershell

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/

相关文章:

powershell - 如何使用PowerShell从应用程序中过滤字符流?

arrays - 扩展哈希表中的属性

c++ - 如何在 ODBC native 客户端中获取 SQL Server 日期时间字段

.net - Azure:用户登录失败

email - 尝试将邮件发送到 Office 365 时,Send-MailMessage 中没有端口参数?

rest - LetsEncrypt - ACMESharp 客户端挑战处于待处理状态超过一个小时

xml - 无法在 PowerShell 中完全解析 XML

c# - 如何在安装时创建 SQL Server 数据库

sql - 如何在SQL中为每个组选择一个最大行

sql-server - 聚集列存储索引会影响 SSAS 查询最终用户性能吗