存储过程执行后记录集关闭

Recordset Closed After Stored Procedure Execution

我正在 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

我在 If Not oRS.BOF... 行收到错误 Operation is not allowed when the object is closed,这表明存储过程没有返回结果。

但是,如果我在 SSMS 中执行存储过程,它会 returns 一行。 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 我在评论中推荐:

I suspect 2 reasons: 1) your sp does not contains: SET NOCOUNT ON; and 2) you're working on variable of type: table.

Operation is not allowed when the object is closed 最常见的原因是该存储过程不包含 SET NOCOUNT ON 命令,这可以防止额外的结果集干扰 SELECT 语句。

更多信息,请参阅:SET NOCOUNT (Transact-SQL)