【问题标题】:VBA/Excel: Not accepting SQL subclausesVBA/Excel:不接受 SQL 子条款
【发布时间】:2015-05-21 18:55:24
【问题描述】:

我有一个通过 ADODB 连接到 Oracle 数据库的 Excel/VBA 电子表格。连接工作正常,普通查询运行良好。但是我试图变得聪明一点,并通过使用with as 子句来避免运行多个选择语句。这个链接基本上解释了我想要做什么:

https://dba.stackexchange.com/questions/6368/how-to-select-the-first-row-of-each-group

在没有 Excel 的情况下直接运行我的 SQL 可以正常工作。但是,Excel/VBA 的 With As 子句存在问题,并抛出 "Run-Time error '3704'; Application-defined or object-defined" 错误。这是我的 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 语句运行,则一切正常。感谢您的建议。

【问题讨论】:

  • 您能否添加您收到的运行时错误?它可以帮助人们回答这个问题,并帮助未来的访问者找到问题。
  • 它的“运行时错误'3704';应用程序定义的或对象定义的错误。
  • 太棒了。请把它编辑成你的问题。

标签: vba excel oracle10g common-table-expression


【解决方案1】:

您必须在 VBA 中以不同的方式编写嵌套选择语句。试试

select * from (
    select
        start_value, country
    from
        MYTABLE
    where country = '840'
) ORDERED

【讨论】:

    【解决方案2】:

    您是否尝试过使用临时表而不是 With As 子句?

    select start_value, country
    into #TempTable
    from MYTABLE where country = '840'
    
    Select * from #TempTable
    
    DROP TABLE #TempTable
    

    【讨论】:

      猜你喜欢
      • 2018-06-24
      • 2016-05-29
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多