VBA:如何捕获存储过程的返回值

时间:2016-12-16 17:45:16

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

我的目标是从vba函数执行sql server存储过程,并检查存储过程是否返回任何记录。

在vba代码中我得到了这个:

Japser : 
1.900.1 , libjasper 1.900.1
x265 : 
x265 [info]: HEVC encoder version 2.0
x265 [info]: build info [Linux][GCC 4.4.7][64 bit] 8bit
x265 [info]: using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX AVX2 FMA3 LZCNT BMI2

存储过程:

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

如果sp能够返回任何值,那么如果我能获得任何帮助,那就太棒了。

谢谢。

2 个答案:

答案 0 :(得分:3)

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

$scope.updateSelect = function(){
        $scope.decimation = ($scope.decimation ? parseInt($scope.decimation) : 0 )+ parseInt($scope.b_mesu);
    }

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

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

答案 1 :(得分:0)

您要做的只是从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