ODBC 调用存储过程失败 - 通过查询

ODBC Call Failed with stored procedure - Pass through query

我创建了一个直通查询并试图从中调用一个存储过程。

我能够成功地在 sql 服务器数据库上执行查询,但是当涉及到存储过程时,我收到一个错误:

"ODBC call Failed"

问题仅出在存储过程上。查询执行良好。

这是我的代码:

Dim qdf As DAO.QueryDef, rst As ADODB.Recordset
Dim DatabaseName As String
Dim Server As String
ServerName = "XXXX"
DatabaseName = "XXX"
Set qdf = CurrentDb.CreateQueryDef("")
 strConnectionString = "ODBC;DRIVER={sql server};" & _
        "DATABASE=" & DatabaseName & ";" & _
        "SERVER=" & ServerName & ";" & _
        "Trusted_Connection=YES;"
qdf.Connect = strConnectionString
qdf.SQL = " EXEC [dbo].[SAMPLE_TEST]"
qdf.ReturnsRecords = True
Set rst = qdf.OpenRecordset
Debug.Print rst!RecordCount
rst.Close
Set rst = Nothing

如果我遗漏了什么,请告诉我?

刚刚又看了一遍你的代码。你不是运行 qry

你不需要qry.execute吗?

要获得有关 "ODBC--call failed." 错误原因的更多信息,我们可以遍历 DBEngine.Errors 集合,看看是否有其他消息可能更具描述性。例如,使用代码

    qdf.Connect = strConnectionString
    qdf.SQL = " EXEC [dbo].[SAMPLE_TEST]"
    qdf.ReturnsRecords = True
    On Error GoTo oops
    Set rst = qdf.OpenRecordset
    Debug.Print rst!RecordCount
    rst.Close
    Set rst = Nothing
    Exit Sub
oops:
    Dim dbeError As Error
    For Each dbeError In DBEngine.Errors
        Debug.Print "(" & dbeError.Number & "): " & dbeError.Description
    Next
End Sub

我们可能会在 VBA 立即 window 中看到以下内容:

(229): [Microsoft][ODBC SQL Server Driver][SQL Server]The EXECUTE permission was denied on the object 'SAMPLE_TEST', database 'myDb', schema 'dbo'.
(3146): ODBC--call failed.

当然

The EXECUTE permission was denied on the object 'SAMPLE_TEST', database 'myDb', schema 'dbo'.

更有帮助

ODBC--call failed.