VBA/Excel:不接受SQL子类
我有一个Excel/VBA电子表格,通过ADODB连接到Oracle数据库。连接工作正常,普通查询运行正常。但是,我试着聪明一点,通过使用VBA/Excel:不接受SQL子类,vba,excel,oracle10g,common-table-expression,Vba,Excel,Oracle10g,Common Table Expression,我有一个Excel/VBA电子表格,通过ADODB连接到Oracle数据库。连接工作正常,普通查询运行正常。但是,我试着聪明一点,通过使用with as子句来避免运行多个select语句。这个链接基本上解释了我要做的事情: 我的SQL在没有Excel的情况下直接运行时运行良好。但是,Excel/VBA在As子句的中存在问题,并引发“运行时错误'3704';应用程序定义或对象定义”错误。这是我的SQL和代码的简化版本: SQL VBA代码 Dim dbaRecordSet As ADODB.Re
with as
子句来避免运行多个select语句。这个链接基本上解释了我要做的事情:
我的SQL在没有Excel的情况下直接运行时运行良好。但是,Excel/VBA在As子句的中存在问题,并引发“运行时错误'3704';应用程序定义或对象定义”错误。这是我的SQL和代码的简化版本:
SQL
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
语句运行,则一切正常。谢谢您的建议。您必须在VBA中以稍微不同的方式编写嵌套的select语句。试一试
select * from (
select
start_value, country
from
MYTABLE
where country = '840'
) ORDERED
您是否尝试过使用临时表而不是WITHAS子句
select start_value, country
into #TempTable
from MYTABLE where country = '840'
Select * from #TempTable
DROP TABLE #TempTable
能否添加收到的运行时错误?它可以帮助人们回答这个问题,并帮助未来的访问者找到这个问题。它的“运行时错误‘3704’;应用程序定义或对象定义错误。太棒了。请将其编辑到您的问题中。
select start_value, country
into #TempTable
from MYTABLE where country = '840'
Select * from #TempTable
DROP TABLE #TempTable