如何将SQL Server存储过程结果返回到Excel

jus*_*eve 3 sql-server excel vba

我可以验证我的VBA代码是否正在服务器上执行存储过程但我无法将记录集恢复到Excel.

Set con = New ADODB.Connection
Set cmd = New ADODB.Command
Set rs = New ADODB.Recordset

con.Open "Provider=SQLOLEDB;Data Source=" & ServerName & ";...."//works 
set rs = cmd.Execute(, SP_Param, adCmdStoredProc) // executes
If rs.EOF = False Then WSP1.Cells(4, 1).CopyFromRecordset rs
//tosses Operation is not allowed when the object is closed.
Run Code Online (Sandbox Code Playgroud)

我已经尝试使用SQLOLEDB提供程序但无法使用它来访问存储过程.

相关参考文献:

  • ActiveX数据对象Recordset 6.0 Lib
  • ActiveX DataObjects 6.1
  • ActiveX远程数据服务6.0

RBa*_*ung 5

添加SET NOCOUNT ON到SQL存储过程的开头.