I am executing a stored procedure using ADO in VBA. I am trying to populate a recordset with results from a stored procedure in SQL Server 2008. The VBA example below:
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
I get an Operation is not allowed when the object is closed error in the If Not oRS.BOF... , which tells me that the stored procedure does not return a result.
However, if I execute a stored procedure in SSMS, it returns a single row. SP goes line by line:
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
The result of the procedure in SSMS is:
id col1 __ ____ 1 1
Can anyone help on why the record set is being closed / not populated?
source share