【问题标题】:VBA/SQL Server: How to return a succeed/error message back from db to vba excelVBA/SQL Server:如何将成功/错误消息从 db 返回到 vba excel
【发布时间】:2021-08-27 10:56:23
【问题描述】:

我在我的 excel vba 代码中使用存储过程将数据传递到 sql server 并将返回消息发送回 vba 代码。好吧,我在这个网站和其他网站上阅读了很多其他问题主题,但我找不到这种方法的最佳解决方案。为了给大家更好的想象,我举个例子:

VBA 代码:

// defined vba function to return message from database to gui
Public Function fGetMessage() As String
    ' Declare variable and store the data id in the .Tag of userform
    Dim lngVarId As Long
    lngVarId = UserForm1.tbVar.Tag
    
    ' Instance of ADODB.Connection
    Dim oDatabase As clsDatabase
    Set oDatabase = New clsDatabase
    
    ' send sql statement with sproc to sql server and get a return message from database
    Dim oCmd As ADODB.Command
    Set oCmd = New ADODB.Command
    
    With oCmd
        .ActiveConnection = oDatabase.fGetDbConnection()
        .CommandType = adCmdStoredProc
        .CommandText = "dbo.spDeleteProtocolData"
        .Parameters(1) = lngVarId
        
        Dim oRs As ADODB.Recordset
        Set oRs = New ADODB.Recordset
        Set oRs = oCmd.Execute
    End With
    
    fGetDeleteMessage = oCmd.Fields("ReturnMessage") // here I want the string return message
End Function

SQL 服务器:

// defined stored procedure in sql server
CREATE PROCEDURE dbo.spDeleteProtocolData
   @VarId AS INT
AS
BEGIN
   SET NOCOUNT ON;

   BEGIN TRY
    BEGIN TRANSACTION
        UPDATE dbo.vProtocolData
        SET StatusId = 0
        WHERE VarId = @VarId
    COMMIT TRANSACTION
   END TRY
   BEGIN CATCH
    -- store error message in log table
    INSERT INTO dbo.Logs (ErrNumber, ErrLine, ErrState, ErrSeverity, ErrSProc, ErrMessage, CreatedBy, CreatedAt)
    SELECT ERROR_NUMBER(), ERROR_LINE(), ERROR_STATE(), ERROR_SEVERITY(), ERROR_PROCEDURE(), ERROR_MESSAGE(), CURRENT_USER, GETDATE()

    ROLLBACK TRANSACTION
   END CATCH
END
GO

有没有人有想法或更好的解决方案来处理 vba excel 和 sql server 数据库之间的返回消息?

【问题讨论】:

  • 你想在这里“返回”什么字符串?我没有看到 SQL SELECTing 有任何数据要返回到应用程序,那么字符串是从哪里来的?
  • @Larnu 我想到了获取 sql server 存储过程的“返回值”。例如,当您在 ssms 中执行存储过程时,您会看到一个名为“返回值”的结果值。也许我可以使用此列“返回值”来处理来自数据库的消息。这是一个好方法还是您有更好的解决方案?
  • “当你在 ssms 中执行存储过程时,你会看到一个名为“返回值”的结果值” 不,你没有。但是,您是在谈论表示程序成功的RETURN 值吗? 0 成功,还有什么失败?
  • @Larnu 是的,我愿意 :) 也许你有其他想法。
  • RETURN 不是字符串,而是int,因此在您的代码中将其作为“字符串”引用是非常令人困惑的。这回答了你的问题了吗? Assign a stored procedure return value to a VBA variable

标签: sql-server excel vba sql-server-2012


【解决方案1】:

您可以使用输出参数:

Dim B As Boolean
Dim C As ADODB.Command
Dim I As Long
Dim P As ADODB.Parameter
Dim V As Variant

   Set C = New ADODB.Command
   C.ActiveConnection = glob_CONNECT
   C.CommandType = adCmdStoredProc
   C.CommandText = X.SQL_Name
   
   ' bind Inputparameter
   If X.NInParams > 0 Then
      For I = 1 To X.NInParams Step 1
         Set P = C.CreateParameter(X.InParam(I).SQL_Name, _
                                   X.InParam(I).ADODB_Type, _
                                   adParamInput, _
                                   X.InParam(I).SQL_Size, _
                                   X.InParam(I).SQL_Value)
         C.Parameters.Append P
      Next
   End If
   
   ' bind Outputparameter
   If X.NOutParams > 0 Then
      For I = 1 To X.NOutParams Step 1
         Set P = C.CreateParameter(X.OutParam(I).SQL_Name, _
                                   X.OutParam(I).ADODB_Type, _
                                   adParamOutput, _
                                   X.OutParam(I).SQL_Size)
         C.Parameters.Append P
      Next
   End If
   
   ' Execute SP (Errors are possible!)
   On Error GoTo 0
   On Error Resume Next
   C.Execute
   
   ' Error?
   If Err.Number <> 0 Then
      SQL_EXEC = False
      Exit Function
   End If

   ' Fetch Output
   If X.NOutParams > 0 Then
      For I = 1 To X.NOutParams Step 1
         V = VNormExec(C.Parameters(X.OutParam(I).SQL_Name), X.OutParam(I).SQL_Type)
         X.OutParam(I).SQL_Value = V
      Next
   End If

【讨论】:

    猜你喜欢
    • 2015-09-13
    • 2015-03-16
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    • 2020-10-30
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多