0
我的目標是從vba函數執行sql server存儲過程,並檢查存儲過程是否返回任何記錄。VBA:如何捕獲存儲過程返回值
在VBA代碼我這個地步得到:
Function TestStoredProcedure()
Dim strMsg As String
Dim ADOCon As ADODB.Connection
Dim ADOQD As ADODB.Command
Dim ADORS As ADODB.Recordset
Set ADOCon = New ADODB.Connection
ADOCon.ConnectionString = GetConnectionString("Dev")
ADOCon.CommandTimeout = 0
ADOCon.Open
Set ADOQD = New ADODB.Command
ADOQD.ActiveConnection = ADOCon
ADOQD.CommandTimeout = 0
ADOQD.CommandType = adCmdStoredProc
ADOQD.CommandText = "mn_CheckForInvalidEntries"
'Execute
Set ADORS = ADOQD.Execute
If ADORS.RecordCount > 0 Then
strMsg = "The SLI Search Feed was not successful."
MsgBox strMsg, vbExclamation, "foo"
Else
strMsg = "The SLI Search Feed successful."
MsgBox strMsg, vbExclamation, "foo"
End If
ADOCon.Close
Set ADOQD = Nothing
Set ADOCon = Nothing
strMsg = ""
End Function
,並且存儲過程:
ALTER PROCEDURE [dbo].[mn_CheckForInvalidEntries]
AS
BEGIN
SET NOCOUNT ON;
SELECT
[ProductID]
, [ForSale]
FROM [Product]
WHERE [ProductID] IN
(
SELECT
[SearchIndex].[ProductID]
FROM [dbo].[SearchIndex]
INNER JOIN [ProductData]
ON [dbo].[SearchIndex].[ProductID] = [ProductData].[ProductID]
WHERE [ForSale] = 1
AND [SearchIndex].[ProductID] NOT LIKE 'mn[d-g]%'
AND [Record] IS NULL
AND [SearchIndex].[ProductID] NOT LIKE 'mn[a-z]%'
END;
如果我能得到在得到檢查的部分如果SP返回的任何幫助價值觀,這將是偉大的。
謝謝。
該程序看起來像它返回行,所以你嘗試分配一個ADO記錄集'Execute'的返回值? 'Set rs = ADOQD.Execute()'然後檢查'rs.EOF'並相應地執行。 –
我有,而我得到的回報是-1。我正在更新我的vb代碼,以便您可以在等待某些評論,建議時看到我嘗試過的內容。 –
不要使用'Recordcount'(它只對特定的遊標類型有效)來確定是否有任何記錄:如果沒有返回任何記錄,'ADORS.EOF'將爲True –