我正在VBA中使用ADO执行存储过程。我正在尝试使用SQL Server 2008中存储过程的结果填充记录集。以下VBA的示例:
Public Function DoSomething() As Variant()
Dim oDB As ADODB.Connection: Set oDB = New ADODB.Connection
Dim oCM As ADODB.Command: Set oCM = New ADODB.Command
Dim oRS As ADODB.Recordset
oDB.Open gcConn
With oCM
.ActiveConnection = oDB
.CommandType = adCmdStoredProc
.CommandText = "spTestSomething"
.NamedParameters = True
.Parameters.Append .CreateParameter("@Param1", adInteger, adParamInput, , 1)
Set oRS = .Execute
End With
If Not oRS.BOF And Not oRS.EOF Then 'Error thrown here'
DoSomething = oRS.GetRows()
Else
Erase DoSomething
End If
oRS.Close
Set oRS = Nothing
oDB.Close
Set oDB = Nothing
End Function
我在
Operation is not allowed when the object is closed
行上收到错误If Not oRS.BOF...
,它向我指示存储过程未返回结果。但是,如果我在SSMS中执行存储过程,它将返回一行。 SP遵循以下原则:
CREATE PROC spTestSomething
@Param1 int
AS
BEGIN
DECLARE @TempStore table(id int, col1 int);
INSERT INTO table1
(param1)
OUTPUT inserted.id, inserted.col1
INTO @TempStore
VALUES
(@Param1);
EXEC spOtherSP;
SELECT
id,
col1
FROM
@TempStore;
END
GO
在SSMS中执行该过程的结果是:
id col1
__ ____
1 1
谁能帮助您解决为什么关闭/未填写记录集的问题?
最佳答案
基于类似的问题:“Operation is not allowed when the object is closed” when executing stored procedure我在评论中建议:
Operation is not allowed when the object is closed
的最常见原因是该存储过程不包含SET NOCOUNT ON
命令,这可以防止额外的结果集干扰SELECT
语句。
有关更多信息,请参见:SET NOCOUNT (Transact-SQL)
关于sql-server - 存储过程执行后,记录集关闭,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28677262/