【问题标题】:Calling stored procedure using powershell使用powershell调用存储过程
【发布时间】:2017-10-02 09:36:57
【问题描述】:

我正在尝试通过运行以下命令从 powershell 调用存储过程。存储过程的名称是 sp_sample(驻留在 ssms 中)。但是我在 powershell 中看不到代码的任何输出,也没有触发该过程。有人可以纠正一下吗。

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=LABVM-
 40\RTC;Database=testDB;Integrated Security=True"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText ="sp_sample"
$SqlCmd.Connection = $SqlConnection
$sqlConnection.Open()
$Result = $SqlCmd.ExecuteNonQuery()
$sqlConnection.Close()

PS:我对powershell一无所知,我从互联网上选择了上面的代码。

【问题讨论】:

  • 没有输出并且没有错误?
  • 您期待什么样的输出?您知道ExecuteNonQuery() 仅返回受影响的行数。
  • 感谢大家的回复!上面的查询确实有效,只是没有意识到。再次感谢!!!

标签: sql-server powershell stored-procedures


【解决方案1】:
Invoke-Sqlcmd -ServerInstance "ServerName" -database "DatabaseName" -Query "EXEC dbo.sp_sample"

【讨论】:

  • IIRC 这取决于是否安装了 SQL 管理工具,而不仅仅是客户端。
  • 感谢大家的回复!上面的查询确实有效,只是没有意识到。再次感谢您的投入!!!
猜你喜欢
  • 2015-05-08
  • 1970-01-01
  • 1970-01-01
  • 2011-12-11
  • 2014-08-07
  • 2011-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多