Excel VBA Recordset不返回数据,尽管正在运行的存储过程返回数据。

huangapple go评论87阅读模式
英文:

Excel VBA Recordset doesn't return data even though the stored procedure it's running does

问题

I am working on some VBA code in Excel that is connecting to a SQL Server database. One of the modules is running three commands - one that uses a CommandText and two that are running stored procedures (neither of which has parameters).

This was working correctly until today. I updated one of the stored procedures because it wasn't returning the correct data and now, when I run the following code, the ADORecordSet has a state of closed after the call to .Execute so the call to adoRs.EOF throws an error

> 3704 - Operation is not allowed when the object is closed

With adoCm
    Set .ActiveConnection = adoConnect
    .CommandType = adCmdStoredProc
    
    .CommandText = "sp_XOL_DETAIL"
    Set adoRs = .Execute
        
    If Not adoRs.EOF Then
      Worksheets("Additional Detail (XOL)").Activate
      Range("A2").CopyFromRecordset adoRs
    End If
    adoRs.Close
  End With

All three calls to the database use the same ADOConnection (adoConnect), ADOCommand (adoCm), and ADORecordSet (adoRS). The ADORecordset is closed after loading its data into the spreadsheet and before the ADOCommand is configured for the next query.

However, when I run the stored procedure in SSMS, it returns almost 1,000 rows of data.

  • I have verified that the connection is open.
  • I have tried creating a new ADORecordSet instead of using the one that the two previous queries have run in, but I get the same error.
  • I tried changing the .CommandType to adCmdText and the .CommandText to exec sp_XOL_DETAIL, but I get the same error.

What might be causing this? Or what else should I look at to try to diagnose the issue?

Thanks!

英文:

I am working on some VBA code in Excel that is connecting to a SQL Server database. One of the modules is running three commands - one that uses a CommandText and two that are running stored procedures (neither of which has parameters).

This was working correctly until today. I updated one of the stored procedures because it wasn't returning the correct data and now, when I run the following code, the ADORecordSet has a state of closed after the call to .Execute so the call to adoRs.EOF throws an error

> 3704 - Operation is not allowed when the object is closed

With adoCm
    Set .ActiveConnection = adoConnect
    .CommandType = adCmdStoredProc
    
    .CommandText = "sp_XOL_DETAIL"
    Set adoRs = .Execute
        
    If Not adoRs.EOF Then
      Worksheets("Additional Detail (XOL)").Activate
      Range("A2").CopyFromRecordset adoRs
    End If
    adoRs.Close
  End With

All three calls to the database use the same ADOConnection (adoConnect), ADOCommand (adoCm), and ADORecordSet (adoRS). The ADORecordset is closed after loading its data into the spreadsheet and before the ADOCommand is configured for the next query.

However, when I run the stored procedure in SSMS, it returns almost 1,000 rows of data.

  • I have verified that the connection is open.
  • I have tried creating a new ADORecordSet instead of using the one that the two previous queries have run in, but I get the same error.
  • I tried changing the .CommandType to adCmdText and the .CommandText to exec sp_XOL_DETAIL, but I get the same error.

What might be causing this? Or what else should I look at to try to diagnose the issue?

Thanks!

答案1

得分: 0

我找出了问题 - 我需要在存储过程代码的顶部添加 "SET NOCOUNT ON;"。添加后重新编译,一切都正常运作。

英文:

I figured out the issue - I needed to have "SET NOCOUNT ON;" at the top of the SP code. After adding that and recompiling, everything is working correctly.

huangapple
  • 本文由 发表于 2023年6月30日 05:24:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76584684.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定