You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Oracle子查询加order by报ORA-00907缺失右括号错误如何解决

Oracle ORA-00907报错解决方案

报错原因

ORA-00907错误是因为Oracle数据库不支持MySQL风格的limit语法,SQL解析器遇到limit关键字时无法识别,因此抛出缺失右括号的错误。

修复方案

分两种场景处理:

场景1:使用Oracle 12c及以上版本

可以直接使用Oracle官方推出的行限制语法替换limit 1,子查询修改为:

select X
from table_expe
where A = expe.A
order by C desc
fetch first 1 row only

场景2:使用Oracle 11g及更早版本

需要先做排序再通过rownum取首行,避免rownum在排序前生效导致结果不符合预期,子查询修改为:

select X
from (
    select X
    from table_expe
    where A = expe.A
    order by C desc
)
where rownum = 1

完整修改后的SQL示例(全版本兼容)

select exp.X,
       exp.Y,
       exp.Z,
       expe.A
from table_exp exp
inner join table_expe expe on exp.B = expe.B
where expe.X = (
    select X
    from (
        select X, A
        from table_expe
        order by C desc
    ) t
    where t.A = expe.A
      and rownum = 1
)

内容的提问来源于stack exchange,提问作者Imrik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 00:27:04