查询适用于Oracle 10g但不适用于11g?

时间:2010-12-06 08:25:48

标签: sql oracle plsql oracle11g ora-00979

DECLARE
  trn  VARCHAR2(2) := 'DD';
  cur  SYS_REFCURSOR;
BEGIN
  OPEN cur FOR
    SELECT
      TRUNC(some_date, trn),
      NULL AS dummy_2,
      COUNT( DISTINCT dummy_1 )
    FROM
      (SELECT SYSDATE AS some_date, ROWNUM AS dummy_1 FROM dual)
    GROUP BY
      TRUNC(some_date, trn);
END;

这适用于Oracle 10,但Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production导致:

  

ORA-00979:不是GROUP BY表达式
  ORA-06512:第5行


有人可以复制/解释这个吗?谢谢!

4 个答案:

答案 0 :(得分:6)

如果您有权访问支持,则看起来像Bug 9478304: LOOP FAILING WITH ORA-00979: NOT A GROUP BY EXPRESSION。这似乎仅影响11.2.0.1。

答案 1 :(得分:2)

我怀疑您的问题是NULL中的SELECT需要在GROUP BY中,即使它是常数。我无法想象为什么它在Oracle 10中会起作用,但不会在11中起作用。

如果您删除NULL AS dummy_2

是否有用

答案 2 :(得分:1)

这没有错误:

DECLARE
  trn  VARCHAR2(2) := 'DD';
  cur  SYS_REFCURSOR;
BEGIN
  OPEN cur FOR
    SELECT
      TRUNC(some_date, dtrn),
      NULL AS dummy_2,
      COUNT( DISTINCT dummy_1 )
    FROM
      (SELECT SYSDATE AS some_date, ROWNUM AS dummy_1 FROM dual) data1,
      (SELECT trn AS dtrn FROM dual) data2
    GROUP BY TRUNC(some_date, dtrn);
END;

问题在于使用trn函数中的TRUNC变量和变量。也许是bug。

答案 3 :(得分:1)

截断内部选择中的sysdate似乎工作正常:

DECLARE
  trn  VARCHAR2(2) := 'DD';
  cur  SYS_REFCURSOR;
BEGIN
  OPEN cur FOR
    SELECT
      some_date,
      NULL AS dummy_2,
      COUNT( DISTINCT dummy_1 )
    FROM
      (SELECT trunc(SYSDATE, trn) AS some_date, ROWNUM AS dummy_1 FROM dual)
    GROUP BY
      some_date;
END;