【问题标题】:Stored procedure from VBA Excel not runningVBA Excel 中的存储过程未运行
【发布时间】:2019-06-25 05:32:07
【问题描述】:

我从 VBA 调用来自 PC 的存储过程,它工作正常。在另一台 PC 和不同的用户中,它无法正常工作。虽然是一个查询,但它在两台 PC 上都有效。

我调用存储过程如下:

Dim rst As New ADODB.Recordset
Dim ConnectionString As String
Dim StrQuery As String

' Connection string for accessing MS SQL database
ConnectionString = <Connection details>

' Opens connection to the database
cnn.Open ConnectionString
' Timeout error in seconds for executing the entire query; The stored procedure normally runs for around 20 min
cnn.CommandTimeout = 2400

' Process execution
StrQuery = "exec [00_Main] @date = '01/31/2018' "
rst.Open StrQuery, cnn
rst.Close

我猜我在执行存储过程的时候有一个错误信息,但是我不知道如何捕获它。

我尝试了以下方法,但没有得到任何输出

' Process execution
StrQuery = "exec [00_Main] @date = '01/31/2018' "
rst.Open StrQuery, cnn
Debug.Print rst.Fields.Count
Debug.Print rst.RecordCount
Debug.Print rst
rst.Close

当我在 SQL Management Studio 中运行存储过程时,我只会得到输出消息,因为存储过程只是更新表。喜欢:

(29145907 rows affected)
(330527 rows affected)

我还尝试在链接here 之后添加错误信息,但该过程运行时没有给我任何错误。喜欢:

' Process execution
DateSelection = Sheets("STB Check").Range("F1")
'StrQuery = "exec [00_Main] @date = '" & DateSelection & "' "
StrQuery = "exec [00_Main] @date = '01/31/2018' "
rst.Open StrQuery, cnn

Done:
rst.Close
Exit Sub

AdoError:

  Dim errLoop As Error
  Dim strError As String

  i = 1

' Process
 StrTmp = StrTmp & vbCrLf & "VB Error # " & Str(Err.Number)
 StrTmp = StrTmp & vbCrLf & "   Generated by " & Err.Source
 StrTmp = StrTmp & vbCrLf & "   Description  " & Err.Description

' Enumerate Errors collection and display properties of
' each Error object.
 Set Errs1 = cnn.Errors
 For Each errLoop In Errs1
      With errLoop
        StrTmp = StrTmp & vbCrLf & "Error #" & i & ":"
        StrTmp = StrTmp & vbCrLf & "   ADO Error   #" & .Number
        StrTmp = StrTmp & vbCrLf & "   Description  " & .Description
        StrTmp = StrTmp & vbCrLf & "   Source       " & .Source
        i = i + 1
   End With
Next

  MsgBox StrTmp

  ' Clean up Gracefully

  On Error Resume Next
  GoTo Done

有什么想法吗?

【问题讨论】:

  • 从第二台 PC 运行时,“它不工作”是什么意思?另外,当用户交换 PC 时会发生什么?
  • 在失败后尝试检查Connection.Errors 集合 - 例如参见support.microsoft.com/en-us/help/167957/…
  • 嗯,您的连接被命名为cnn,因此检查Conn1 上的错误没有多大用处 - 没有任何方法可以运行没有错误。您的错误处理代码在哪里?看起来您已经进行了一些设置,但那里没有 On Error...。
  • 如果您的 SP 没有返回任何记录,则您无法对该记录集执行任何操作 - 您可以使用 connection.execute 方法。
  • 存储过程有权限,你检查过两个用户有相同的(执行)权限吗?

标签: sql-server excel vba stored-procedures


【解决方案1】:

使用适当的参数化,并将日期视为Date,而不是字符串。

不要直接运行ADODB.Recordset,而是使用ADODB.Command;将命令文本设置为存储过程的名称,并将ADODB.Parameter 添加到其Parameters 集合中,提供单元格值(在验证IsDate 为该单元格值返回True 之后) - like on docs.microsoft.com :

Dim theDate As Date
theDate = Sheets("STB Check").Range("F1").Value

Dim cmd As ADODB.Command
Set cmd = New ADODB.Command
Set cmd.ActiveConnection = cnn
cmd.CommandType = adCmdStoredProc
cmd.CommandText = "[00_Main]"

Dim dateParam As ADODB.Parameter
Set dateParam = cmd.CreateParameter("date", adDate, adParamInput)
dateParam.Value = theDate

cmd.Parameters.Append dateParam

Dim results As ADODB.Recordset
Set results = cmd.Execute

【讨论】:

  • 谢谢,像这样我收到一条错误消息“访问远程服务器被拒绝,因为不存在登录映射”。我认为这给了我一些可以深入研究的东西
  • @Selrac 是的,看起来像一个微不足道的权限问题 ;-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-10
  • 1970-01-01
  • 1970-01-01
  • 2020-02-24
相关资源
最近更新 更多