Sql VBA:如何捕获存储过程返回值
我的目标是从vba函数执行sql server存储过程,并检查存储过程是否返回任何记录 在vba代码中,我做到了这一点:Sql VBA:如何捕获存储过程返回值,sql,vba,stored-procedures,sql-server-2012,vb6,Sql,Vba,Stored Procedures,Sql Server 2012,Vb6,我的目标是从vba函数执行sql server存储过程,并检查存储过程是否返回任何记录 在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.Conn
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是否返回了任何值,那就太好了
多谢各位 要存储结果,可以使用记录集
Dim adoRs As ADODB.Recordset
Set adoRs = ADOQD.Execute
然后可以询问记录集是否为空
isEmpty = (adoRs.BOF And adoRs.EOF)
要存储结果,可以使用记录集
Dim adoRs As ADODB.Recordset
Set adoRs = ADOQD.Execute
然后可以询问记录集是否为空
isEmpty = (adoRs.BOF And adoRs.EOF)
您只需要从Excel运行存储过程,对吗?这里有两个选项供您尝试
Option Explicit
Sub Working2()
Dim con As Connection
Dim rst As Recordset
Dim strConn As String
Set con = New Connection
strConn = "Provider=SQLOLEDB;"
strConn = strConn & "Data Source=LAPTOP\SQL_EXPRESS;"
strConn = strConn & "Initial Catalog=Northwind;"
strConn = strConn & "Integrated Security=SSPI;"
con.Open strConn
'Put a country name in Cell E1
Set rst = con.Execute("Exec dbo.TestNewProc '" & ActiveSheet.Range("E1").Text & "'")
'The total count of records is returned to Cell A5
ActiveSheet.Range("A5").CopyFromRecordset rst
rst.Close
con.Close
End Sub
Sub Working()
Dim con As Connection
Dim rst As Recordset
Set con = New Connection
con.Open "Provider=SQLOLEDB;Data Source=LAPTOP\SQL_EXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI;"
Set rst = con.Execute("Exec dbo.[Ten Most Expensive Products]")
'Results of SProc are returned to Cell A1
ActiveSheet.Range("A1").CopyFromRecordset rst
rst.Close
con.Close
End Sub
您只需要从Excel运行存储过程,对吗?这里有两个选项供您尝试
Option Explicit
Sub Working2()
Dim con As Connection
Dim rst As Recordset
Dim strConn As String
Set con = New Connection
strConn = "Provider=SQLOLEDB;"
strConn = strConn & "Data Source=LAPTOP\SQL_EXPRESS;"
strConn = strConn & "Initial Catalog=Northwind;"
strConn = strConn & "Integrated Security=SSPI;"
con.Open strConn
'Put a country name in Cell E1
Set rst = con.Execute("Exec dbo.TestNewProc '" & ActiveSheet.Range("E1").Text & "'")
'The total count of records is returned to Cell A5
ActiveSheet.Range("A5").CopyFromRecordset rst
rst.Close
con.Close
End Sub
Sub Working()
Dim con As Connection
Dim rst As Recordset
Set con = New Connection
con.Open "Provider=SQLOLEDB;Data Source=LAPTOP\SQL_EXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI;"
Set rst = con.Execute("Exec dbo.[Ten Most Expensive Products]")
'Results of SProc are returned to Cell A1
ActiveSheet.Range("A1").CopyFromRecordset rst
rst.Close
con.Close
End Sub
该过程看起来像是返回行,因此您是否尝试将ADO记录集分配给
Execute
的返回值Set rs=ADOQD.Execute()
然后检查rs.EOF
并相应地进行操作。我得到了,我得到的回报是-1。我正在更新我的vb代码,以便您可以看到我在等待一些评论和建议时尝试过的内容。不要使用Recordcount
(这只适用于特定的光标类型)来确定是否存在任何记录:ADORS.EOF
如果没有返回任何内容,则为真该过程看起来像是返回行,那么,您是否尝试过将ADO记录集分配给Execute
的返回值Set rs=ADOQD.Execute()
然后检查rs.EOF
并相应地进行操作。我得到了,我得到的回报是-1。我正在更新我的vb代码,以便您可以看到我在等待一些评论和建议时尝试的内容。不要使用Recordcount
(这只适用于特定的光标类型)来确定是否存在任何记录:ADORS.EOF
如果没有返回任何内容,则为真