ORA-00907:子查询缺少右括号

时间:2019-06-06 09:25:45

标签: sql oracle

在下面的SQL中获取错误。

ORA-00907: missing right parenthesis
00907. 00000 -  "missing right parenthesis"
*Cause:    
*Action: Error at Line: 65 Column: 13

此SQL由Hibernate生成。

select
        * 
    from
        ( select
            planrun0_.id as id1_38_,
            planrun0_.created_by as created_by2_38_,
            planrun0_.creation_date as creation_date3_38_,
            planrun0_.last_updated_by as last_updated_by4_38_,
            planrun0_.last_update_date as last_update_date5_38_,
            planrun0_.last_update_login as last_update_login6_38_,
            planrun0_.version as version7_38_,
            planrun0_.tenant_id as tenant_id8_38_,
            planrun0_.plan_id as plan_id10_38_,
            planrun0_.run_type as run_type9_38_,
            (
            SELECT
                status.id 
            from
                C_BIA_PLANRUN_STATUS status 
            WHERE
                status.plan_run_id = planrun0_.id 
            ORDER BY
                planrun0_.last_update_date desc LIMIT 1
                ) as formula3_ 
        from
            v2_planrun planrun0_ 
        where
            planrun0_.run_type=''
            and planrun0_.tenant_id='c2' 
        order by
            planrun0_.id asc ) 
        where
            rownum <= 10

2 个答案:

答案 0 :(得分:0)

删除LIMIT 1

(
        SELECT
            status.id 
        from
            C_BIA_PLANRUN_STATUS status 
        WHERE
            status.plan_run_id = planrun0_.id 
        ORDER BY
            planrun0_.last_update_date desc       -- LIMIT 1
            ) as formula3_ 

相反,您可以使用ROW_NUMBER,例如

select ...
  from (select ...
          row_number() over (order by planrun0_.last_update_date desc) rn
        from ...
       )
  where rn = 1

答案 1 :(得分:0)

您不能在子查询中使用order by。该条款有误;它并没有使用LIMIT关键字(在Oracle中也是无效的)。

乍一看,等价于:

        (
        SELECT
            MAX(status.id) KEEP (DENSE_RANK LAST ORDER BY planrun0_.last_update_date)
        from
            C_BIA_PLANRUN_STATUS status 
        WHERE
            status.plan_run_id = planrun0_.id 
            ) as formula3_ 

...但是通过外部子句中的值在子查询顺序中使用解析子句实际上没有任何意义,所以我认为您实际上想要一个联接,例如:

select
    * 
from
    ( select
        planrun0_.id as id1_38_,
        planrun0_.created_by as created_by2_38_,
        planrun0_.creation_date as creation_date3_38_,
        planrun0_.last_updated_by as last_updated_by4_38_,
        planrun0_.last_update_date as last_update_date5_38_,
        planrun0_.last_update_login as last_update_login6_38_,
        planrun0_.version as version7_38_,
        planrun0_.tenant_id as tenant_id8_38_,
        planrun0_.plan_id as plan_id10_38_,
        planrun0_.run_type as run_type9_38_,
        max(status.id) keep (dense_rank last order by planrun0_.last_update_date)
          over (partition by planrun0_.id) as formula3_ 
    from
        v2_planrun planrun0_ 
    join
        c_bia_planrun_status status 
    on
        status.plan_run_id = planrun0_.id 
    where
        planrun0_.run_type=''
        and planrun0_.tenant_id='c2' 
    order by
        planrun0_.id asc
    ) 
    where
        rownum <= 10

但这绝不是真的:

planrun0_.run_type=''

它必须是

planrun0_.run_type is null