【问题标题】:VBA: How to capture a stored procedure return valueVBA:如何捕获存储过程的返回值
【发布时间】:2017-05-02 13:37:25
【问题描述】:

我的目标是从 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.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 代码,以便您在等待一些 cmets 和建议时看到我尝试过的内容。
  • 不要使用Recordcount(它只适用于特定的游标类型)来确定是否有任何记录:如果没有返回任何记录,ADORS.EOF 将为 True

标签: sql vba stored-procedures sql-server-2012 vb6


【解决方案1】:

要存储结果,您可以使用记录集。

Dim adoRs As ADODB.Recordset
Set adoRs = ADOQD.Execute

然后你可以询问记录集是否为空。

isEmpty = (adoRs.BOF And adoRs.EOF) 

【讨论】:

    【解决方案2】:

    您只想从 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-19
      • 2010-12-12
      • 2023-04-04
      • 2014-06-27
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多