gpt4 book ai didi

sql-server - 在 VBA 中执行 SQL 时出现运行时错误

转载 作者:行者123 更新时间:2023-12-04 21:10:03 26 4
gpt4 key购买 nike

我收到 Run-time error '-2147217900 (80040e14) Incorrect syntax new '11999999'当试图将数据从 SQL 服务器数据库拉入 VBA 中的记录集时,这是我第一次使用 VBA,所以请原谅缺乏完美。

我的代码如下;

Set connection = New ADODB.Connection
Dim newData As ADODB.Recordset

connection.ConnectionString = "my connection string" 'not posting this for security reasons'
connection.Open

Set newData = connectionExecute(BSMARTOpenFaults )

我将查询存储为多行字符串,因为我更喜欢它,因为 SQL 语句看起来更好。
Private Const BSMARTOpenFaults = "select count(*) from call" _
& "where (" _
& " call_id between 11000000 and 11999999" _
& "or call_id between 12000000 and 12999999" _
& "or call_id between 14000000 and 14999999" _
& "or call_id between 16000000 and 19999999" _
& "or call_id between 26000000 and 26999999" _
& "or call_id between 31000000 and 31999999" _
& "or call_id between 73000000 and 73999999)" _
& "and call_status <> 2 -- all open calls" _
& "and call_type = 'FT'"

从错误判断它落在 SQL 查询上(或者它只是两个数字相同的巧合),但我不确定如何修复它,因为当我在 SQL Server Management Studio 上运行查询时,查询执行并返回结果(准确地说是 21),所以我很困惑为什么它在 VBA 中尝试时不会执行并返回无效的语法错误,这是在 VBA 中格式化 SQL 语句的不正确方法吗?

最佳答案

数字和 OR 之间没有换行符关键字。你的陈述看起来像:

and 11999999or call_id between 12000000 and 12999999or and

在数字之后和/或关键字之前添加一个空格

关于sql-server - 在 VBA 中执行 SQL 时出现运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44432767/

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