【发布时间】:2014-10-12 19:42:38
【问题描述】:
以下查询在几乎所有数据库中都是perfectly valid(提供或获取dual 虚拟表),包括Oracle:
select 'A' as x from dual union all
select 'B' from dual
order by x asc
返回:
| X |
|---|
| A |
| B |
现在这个查询仍然是相当标准的 SQL,但是在 Oracle 上是 doesn't work
select 'A' as x from dual union all
select 'B' from dual union all
select 'C' from dual
order by x asc
我来了
ORA-00904: "X": invalid identifier
不过,这个works:
select 'A' as x from dual union all
select 'B' as x from dual union all
select 'C' from dual
order by x asc
我一直在研究这个问题,并发现显然,至少第一个子选择和 second-last (??) 子选择需要有一个名为 x 的列。在第一个示例中,两个子选择似乎只是重合。 Working example:
select 'A' as x from dual union all
select 'B' from dual union all
select 'C' from dual union all
select 'D' from dual union all
select 'E' from dual union all
select 'F' as x from dual union all
select 'G' from dual
order by x asc
你可能已经猜到了,这个wouldn't work:
select 'A' as x from dual union all
select 'B' from dual union all
select 'C' from dual union all
select 'D' from dual union all
select 'E' as x from dual union all
select 'F' from dual union all
select 'G' from dual
order by x asc
有趣的旁注:
派生表似乎不受此限制的影响。 This works:
select * from (
select 'A' as x from dual union all
select 'B' from dual union all
select 'C' from dual
)
order by x asc
问题:
这是 Oracle SQL 解析器中的一个(已知?)错误,还是语言语法中有任何非常微妙的细节绝对需要第一个和倒数第二个子选择来保存从ORDER BY 子句?
【问题讨论】:
-
只是补充一点,在
SQL Server 2008 R2的第一个选择 - 列上必须有别名。 -
@Elias:是的,到目前为止,我已经尝试过使用 SQL Server、PostgreSQL 和 H2。
-
似乎已作为错误 14196463 提出,但没有解决就关闭了。在社区线程 3561546 中也提到过。仅供参考,在 11.2.0.3 和 10.2.0.5 中转载。
标签: sql oracle oracle11g sql-order-by union-all