使用内部查询进行ORDER BY,使ORA-00907缺少右括号

时间:2014-02-18 16:35:28

标签: sql oracle

我正在尝试测试查询并收到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

1 个答案:

答案 0 :(得分:3)

编码风格和不必要的嵌套括号使得这很难阅读和解释。但它确实有帮助,与一些评论相反,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中引用:

  

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

     

...
  这是Bug 4944718的预期行为   子查询中的ORDER BY不应该起作用,因为行的顺序传递给外部查询并且没有影响。

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