我在VBA中使用ADO執行存儲過程。我試圖填充從存儲過程在SQL Server中的結果下面的VBA的2008例的記錄:記錄集在存儲過程執行後關閉
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中執行存儲過程,它將返回一行。該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
任何人都可以用,爲什麼記錄被關閉/未填寫幫助嗎?
,以確定有多少記錄已受SP,使用'Exceute'方法使用參數一起:https://msdn.microsoft.com/en-us/library /windows/desktop/ms681559%28v=vs.85%29.aspx而不是BOF和EOF – 2015-02-23 16:15:43
@MaciejLos感謝您的回覆。有多個行受到影響,因爲有幾個SP正在執行。您是否打算在嘗試使用記錄集之前檢查此內容?謝謝 – Gareth 2015-02-23 16:25:14
是的。在這種情況下,不需要檢查BOF和EOF方法返回的值。如果RecordsAffected param返回大於零的值,則記錄集將填充值;) – 2015-02-23 16:30:10