Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 在VBA中执行SQL时出现运行时错误_Sql Server_Vba_Excel - Fatal编程技术网

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

Sql server 在VBA中执行SQL时出现运行时错误,sql-server,vba,excel,Sql Server,Vba,Excel,我在尝试将数据从SQL server数据库拉入VBA中的记录集时遇到运行时错误“-2147217900 80040e14语法不正确新的“1199999”,这是我第一次使用VBA,请原谅我的不足 我的代码如下 Set connection = New ADODB.Connection Dim newData As ADODB.Recordset connection.ConnectionString = "my connection string" 'not posting this for s

我在尝试将数据从SQL server数据库拉入VBA中的记录集时遇到运行时错误“-2147217900 80040e14语法不正确新的“1199999”,这是我第一次使用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语句的错误方法

常规提示在VBA中调试SQL时应执行的操作:

将BSMARTOpenFaults声明为测试的公共。 转到即时窗口并写入?BSMARTOpenFaults 查看输出。 检查输出。 看看那个电话,一个字在哪里。 将代码更改为Const BSMARTOpenFaults=select count*from call u,因此单词为2。
常规提示在VBA中调试SQL时应执行的操作:

将BSMARTOpenFaults声明为测试的公共。 转到即时窗口并写入?BSMARTOpenFaults 查看输出。 检查输出。 看看那个电话,一个字在哪里。 将代码更改为Const BSMARTOpenFaults=select count*from call u,因此单词为2。
数字和或关键字之间没有换行符。你的陈述如下:

and 11999999or call_id between 12000000 and 12999999or and

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

数字和/或关键字之间没有换行符。你的陈述如下:

and 11999999or call_id between 12000000 and 12999999or and

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

不要跳过字符串每个部分末尾的空格。在字符串返回时:

select count(*) from callwhere (...
callwhere可能不是模式中的表。OR条款也存在同样的问题。试试这个:

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 " _
& "and call_type = 'FT'"

不要跳过字符串每个部分末尾的空格。在字符串返回时:

select count(*) from callwhere (...
callwhere可能不是模式中的表。OR条款也存在同样的问题。试试这个:

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 " _
& "and call_type = 'FT'"

谢谢,我没有意识到我忘记了报价之间的空格,因为很明显,我在管理工作室运行的正常形式没有我没有意识到的QUTOE谢谢,我没有意识到我忘记了引号之间的空格,因为很明显,我在管理工作室运行的正常形式没有我没有意识到的QUTOE