我有一个Excel/VBA电子表格通过ADODB连接到Oracle数据库。连接工作正常,普通查询运行良好。但我试图有点聪明,并避免使用with as
子句运行多个select语句。这个环节主要讲解什么,我试图做的:当它直接运行而不Excel中VBA/Excel:不接受SQL子条款
https://dba.stackexchange.com/questions/6368/how-to-select-the-first-row-of-each-group
我的SQL工作正常。但是,Excel/VBA在With As
子句中存在问题,并引发“运行时错误”3704;“应用程序定义的或对象定义的”错误。这是我的SQL和代码的简化版本:
SQL
with ORDERED as (select
start_value, country
from
MYTABLE
where country = '840') select * from ORDERED
VBA代码
Dim dbaRecordSet As ADODB.Recordset
Dim gloDatabase As ADODB.Connection
dim sSQL as string
sSQL = "with ORDERED as (select start_value, country from MYTABLE where country = '840') select * from ORDERED"
Set gloDatabase = New ADODB.Connection
gloDatabase.ConnectionString = My_Connection_String
gloDatabase.Open
gloDatabase.CursorLocation = adUseClient
Set dbaRecordSet = New ADODB.Recordset
dbaRecordSet.ActiveConnection = DBConnection
dbaRecordSet.CursorLocation = adUseClient
dbaRecordSet.Source = sSQL
dbaRecordSet.Open
有谁知道为什么的Excel/VBA拒绝接受With As()
条款?如果我删除该条款并将其作为普通select
语句运行,则一切正常。谢谢你的建议。
您可以添加您收到的运行时错误吗?它可以帮助人们回答这个问题,并帮助未来的访问者找到问题。 – RubberDuck
其运行时错误'3704';应用程序定义或对象定义的错误 – user3046742
太棒了,请将其编辑到您的问题中 – RubberDuck