ORDER BY 与内部查询,给出 ORA-00907 缺少右括号

Posted

技术标签:

【中文标题】ORDER BY 与内部查询,给出 ORA-00907 缺少右括号【英文标题】:ORDER BY with Inner query, giving ORA-00907 missing right parenthesis 【发布时间】:2014-02-18 16:35:28 【问题描述】:

我正在尝试测试查询并收到 ORA-00907 错误。我相信错误是由于我用于RUN.RN_RUN_ID 的内部查询。 内部查询本身工作正常,我给出内部查询的结果,外部查询工作正常。任何关于我做错了什么的见解,将不胜感激。

Select
    RELEASES.REL_NAME AS RELEASE_NAME,
    RELEASE_CYCLES.RCYC_NAME AS CYCLE_NAME,
    TESTCYCL.TC_TESTCYCL_ID AS TEST_INSTANCE_ID,
    TEST.TS_NAME AS TEST_CASE_NAME,
    TEST.TS_USER_26 AS TEST_POST_CONDITIONS,
    TEST.TS_USER_25 AS TEST_PRE_CONDITIONS,
    TEST.TS_TEST_ID AS TEST_CASE_ID,
    TEST.TS_DESCRIPTION AS TEST_CASE_DESCRIPTION,
    TESTCYCL.TC_STATUS  AS TEST_ACTUAL_RESULTS,
    TEST.TS_DEV_COMMENTS AS TEST_CASE_COMMENTS,
    TEST.TS_RESPONSIBLE AS TEST_CASE_AUTHOR,
    TEST.TS_CREATION_DATE AS TEST_CASE_CREATION_DATE,
    TESTCYCL.TC_EXEC_DATE AS EXECUTION_DATE,
    TESTCYCL.TC_EXEC_TIME AS EXECUTION_TIME,
    TESTCYCL.TC_ACTUAL_TESTER AS EXECUTED_BY,
    STEP.ST_ID AS STEP_ID,
    STEP.ST_STEP_NAME AS STEP_NUMBER,
    STEP.ST_DESCRIPTION AS STEP_DESCRIPTION,
    STEP.ST_EXPECTED AS EXPECTED_RESULTS,
    STEP.ST_ACTUAL AS ACTUAL_RESULTS,
    STEP.ST_USER_07 AS STEP_COMMENT,
    STEP.ST_STATUS AS STEP_STATUS,
    STEP.ST_STEP_ORDER AS STEP_ORDER,
    STEP.ST_USER_01 AS STEP_TYPE,
    RUN.RN_RUN_ID AS RUN_ID From ((((((RELEASES JOIN RELEASE_CYCLES
    ON RELEASES.REL_ID = RELEASE_CYCLES.RCYC_PARENT_ID 
    AND RELEASES.REL_NAME = 'XYZ') LEFT JOIN CYCLE
    ON RELEASE_CYCLES.RCYC_ID = CYCLE.CY_ASSIGN_RCYC) LEFT JOIN TESTCYCL
    ON CYCLE.CY_CYCLE_ID = TESTCYCL.TC_CYCLE_ID) LEFT JOIN RUN
    ON TESTCYCL.TC_TESTCYCL_ID = RUN.RN_TESTCYCL_ID) LEFT JOIN TEST
    ON TESTCYCL.TC_TEST_ID = TEST.TS_TEST_ID) LEFT JOIN STEP
    ON RUN.RN_RUN_ID = STEP.ST_RUN_ID) WHERE (TEST.TS_TYPE = 'MANUAL' OR 
    TEST.TS_TYPE  = 'QUICKTEST_TEST' OR TEST.TS_TYPE = 'LR-SCENARIO')
    AND RUN.RN_RUN_ID in(Select max(RUN.RN_RUN_ID) From (((((((RELEASES JOIN RELEASE_CYCLES
    ON  RELEASES.REL_ID = RELEASE_CYCLES.RCYC_PARENT_ID AND RELEASES.REL_NAME = 'XYZ') 
    LEFT JOIN  CYCLE ON RELEASE_CYCLES.RCYC_ID = CYCLE.CY_ASSIGN_RCYC)
    LEFT JOIN TESTCYCL ON CYCLE.CY_CYCLE_ID = TESTCYCL.TC_CYCLE_ID)
    LEFT JOIN RUN ON TESTCYCL.TC_TESTCYCL_ID = RUN.RN_TESTCYCL_ID)
    LEFT JOIN TEST ON TESTCYCL.TC_TEST_ID = TEST.TS_TEST_ID)
    LEFT JOIN STEP ON RUN.RN_RUN_ID = STEP.ST_RUN_ID)
    LEFT JOIN LINK ON STEP.ST_ID  = LINK.LN_ENTITY_ID AND LINK.LN_ENTITY_TYPE = 'STEP')
    WHERE (TEST.TS_TYPE = 'MANUAL' OR TEST.TS_TYPE = 'QUICKTEST_TEST' OR 
    TEST.TS_TYPE = 'LR-SCENARIO')AND TEST.TS_TEST_ID =145965
    ORDER BY TESTCYCL.TC_TESTCYCL_ID)
    ORDER BY TESTCYCL.TC_TESTCYCL_ID,STEP.ST_STEP_ORDER

【问题讨论】:

左括号太多!!靠近(((((((RELEASES 添加右括号。 我们不是人类编译器。剥离您的查询并逐步添加回部分。 我总是尽量避免那些主要由查询设计器程序生成的连接括号。 sql 很快变得无法维护。 【参考方案1】:

编码风格和不必要的嵌套括号使这很难阅读和解释。但它也确实有帮助,与某些 cmets 不同,ORA-00907 并不总是意味着括号数不均匀,它可能表示导致解析器退出的更一般的语法错误。在这种情况下,它不是很有帮助。

问题是倒数第二行的order by 子句,在您与in 进行比较的子查询中:

...
AND RUN.RN_RUN_ID in(Select max(RUN.RN_RUN_ID) From (((((((RELEASES JOIN RELEASE_CYCLES
...
TEST.TS_TYPE = 'LR-SCENARIO')AND TEST.TS_TEST_ID =145965
ORDER BY TESTCYCL.TC_TESTCYCL_ID)
ORDER BY TESTCYCL.TC_TESTCYCL_ID,STEP.ST_STEP_ORDER

最终的排序显然是允许的,但在那个子查询中却不是。所以它应该结束:

...
TEST.TS_TYPE = 'LR-SCENARIO')AND TEST.TS_TEST_ID =145965)
ORDER BY TESTCYCL.TC_TESTCYCL_ID,STEP.ST_STEP_ORDER

我无法测试,因为我没有您的架构,但更简单的演示可能有助于演示:

select d1.dummy
from dual d1
where d1.dummy in (
  select dummy
  from dual d2
  order by d2.dummy
)
order by d1.dummy;

Error at Command Line : 6 Column : 3
Error report -
SQL Error: ORA-00907: missing right parenthesis
00907. 00000 -  "missing right parenthesis"

去除内层order by

select d1.dummy
from dual d1
where d1.dummy in (
  select dummy
  from dual d2
)
order by d1.dummy;

DUMMY
-----
X     

它期望看到) 而不是order by,所以一旦你知道它出了什么问题,这个错误确实是有道理的;但它并不能真正帮助您缩小范围。

顺便提一下,Oracle 支持文档 731577.1 中对此进行了引用:

在子查询中使用ORDER BY 子句时获取ORA-00907: missing right parenthesis。删除 ORDER BY 子句后,查询运行不会出错。

... 这是每个错误 4944718 的预期行为 ORDER BY 在子查询中不应该工作,因为行的顺序被传递给外部查询并且没有影响。

在内联视图中允许/忽略它,但在嵌套子查询中不允许。 (虽然可能有异常仍然不会抛出错误...)

【讨论】:

谢谢亚历克斯。我删除了内部订单,它工作正常。

以上是关于ORDER BY 与内部查询,给出 ORA-00907 缺少右括号的主要内容,如果未能解决你的问题,请参考以下文章

由于 ORDER BY 子句,Partition By Clause 给出不同的结果

使用 ORDER BY id 时 MySQL 查询慢

在 MySQL 中优化 ORDER BY LIMIT 查询

DB2 order by子句在不同的OS上给出了不同的结果

mysql:优化查询 w/混合升序 ORDER BY

SQL 查询 MATCH 与别名和 order by 并优化查询