返回值,表示通过ADO / VBA更新SQL Server存储过程的成功/失败

时间:2010-07-16 21:10:02

标签: vba stored-procedures ado return-value

我有一个SQL Server 2008存储过程来更新表中的值。我想让存储过程返回一个整数值,表明更新成功(返回0)或不返回(返回错误号)。通过ADO和VBA实现这一目标的最佳方法是什么?这里有一些简化形式的代码执行更新......我只是不确定如何取回存储过程的返回值

Public Function DoUpdate(employeeID as integer, firstName as string, lastname as string) as integer

  Dim cnn As ADODB.Connection
  Dim cmd As ADODB.Command
  Dim activeConnectionString As String

  activeConnectionString = GetActiveConnectionString()

  Set cnn = New ADODB.Connection
  cnn.ConnectionString = activeConnectionString
  cnn.CursorLocation = adUseClient
  cnn.Open

  Set cmd = New ADODB.Command
  cmd.ActiveConnection = cnn
  cmd.CommandType = adCmdStoredProc
  cmd.CommandText = "uspUpdateEmployeeName"
  cmd.NamedParameters = True

  cmd.Parameters("@EmployeeID").Value = employeeID 
  cmd.Parameters("@FirstName").Value = firstName
  cmd.Parameters("@LastName").Value = lastName

  cmd.Execute

  'Unsure of how to get back return value here
  'DoUpdate = returnValue

  Set cnn = Nothing

End Function

4 个答案:

答案 0 :(得分:4)

注意: return_value 必须是第一个参数!

订单很重要。

当我最后一次指定我的"had too many arguments"参数而不是第一次时,我收到错误,说明我的查询return_value

参数的排序是我错误的原因。

答案 1 :(得分:2)

如果您使用

Dim lngRecs As Long

cmd.Execute lngRecs

lngRecs应包含受影响的记录。

答案 2 :(得分:2)

我似乎记得您需要提供类型为“adParamReturnValue”的额外参数,如下所示:

Dim lRetVal as Long

Set cmd = New ADODB.Command
cmd.Parameters.Append .CreateParameter("returnvalue", adInteger, adParamReturnValue)

cmd.Execute

'Now check the return value of the procedure
lRetVal = cmd.Parameters("returnvalue")

If lRetVal > 0 then

答案 3 :(得分:1)

有几种方法可以使用VBA获取值:

  1. 记录
  2. 受影响的记录数(仅适用于插入/更新/删除,否则为-1)
  3. 输出参数
  4. 返回值
  5. 我的代码演示了所有四个。这是一个返回值的存储过程:

    Create PROCEDURE CheckExpedite
        @InputX  varchar(10),
        @InputY int,
        @HasExpedite int out
    AS
    BEGIN
        Select @HasExpedite = 9 from <Table>
        where Column2 = @InputX and Column3 = @InputY
    
        If @HasExpedite = 9
            Return 2
        Else
            Return 3
    End
    

    这是我在Excel VBA中使用的子。您需要参考Microsoft ActiveX Data Objects 2.8 Library。

    Sub CheckValue()
    
        Dim InputX As String: InputX = "6000"
        Dim InputY As Integer: InputY = 2014
    
        'open connnection
        Dim ACon As New Connection
        'ACon.Open ("Provider=SQLOLEDB;Data Source=<SqlServer>;" & _
        '    "Initial Catalog=<Table>;Integrated Security=SSPI")
    
        'set command
        Dim ACmd As New Command
        Set ACmd.ActiveConnection = ACon
        ACmd.CommandText = "CheckExpedite"
        ACmd.CommandType = adCmdStoredProc
    
        'Return value must be first parameter else you'll get error from too many parameters
        'Procedure or function "Name" has too many arguments specified.
        ACmd.Parameters.Append ACmd.CreateParameter("ReturnValue", adInteger, adParamReturnValue)
        ACmd.Parameters.Append ACmd.CreateParameter("InputX", adVarChar, adParamInput, 10, InputX)
        ACmd.Parameters.Append ACmd.CreateParameter("InputY", adInteger, adParamInput, 6, InputY)
        ACmd.Parameters.Append ACmd.CreateParameter("HasExpedite", adInteger, adParamOutput)
    
        Dim RS As Recordset
        Dim RecordsAffected As Long
    
        'execute query that returns value
        Call ACmd.Execute(RecordsAffected:=RecordsAffected, Options:=adExecuteNoRecords)
    
        'execute query that returns recordset
        'Set RS = ACmd.Execute(RecordsAffected:=RecordsAffected)
    
        'get records affected, return value and output parameter
        Debug.Print "Records affected: " & RecordsAffected
        Debug.Print "Return value: " & ACmd.Parameters("ReturnValue")
        Debug.Print "Output param: " & ACmd.Parameters("HasExpedite")
    
        'use record set here
        '...
    
        'close
        If Not RS Is Nothing Then RS.Close
        ACon.Close
    
    End Sub