gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 09:35:22 25 4
gpt4 key购买 nike

我已经创建了一个传递查询并试图从中调用一个存储过程。

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

ODBC 调用失败”

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

这是我的代码:

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

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

最佳答案

获取有关“ODBC 调用失败”原因的更多信息。 error 我们可以遍历 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 即时窗口中看到以下内容:

(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.

关于sql-server - ODBC 调用存储过程失败 - 通过查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29774178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com