我正在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
谁能帮助您解决为什么关闭/未填写记录集的问题?
基于类似的问题:我在注释中建议执行存储过程时“关闭对象时不允许操作”:
我怀疑2个原因:1)您的sp不包含:
SET NOCOUNT ON;
2)您正在处理类型为table的变量。
最常见的原因Operation is not allowed when the object is closed
是该存储过程不包含SET NOCOUNT ON
命令,这可以防止多余的结果集干扰SELECT
语句。
有关更多信息,请参见:SET NOCOUNT(Transact-SQL)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句