英文:
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
toadCmdText
and the.CommandText
toexec 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
toadCmdText
and the.CommandText
toexec 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论