oracle 如何禁止在一个查询中使用同一个表别名两次?

uajslkp6  于 2023-04-20  发布在  Oracle
关注(0)|答案(1)|浏览(135)

运行Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production Version 19.8.0.0.0
考虑以下示例:

create table tab_a as
          select 1 as id from dual
union all select 2 as id from dual
union all select 3 as id from dual
;

create table tab_b as
          select 1 as id, 'b1' as val, 'bbb1' as val_b from dual
union all select 2 as id, 'b2' as val, 'bbb2' as val_b from dual
;

create table tab_c as
          select 1 as id, 'c1' as val, 'ccc1' as val_c from dual
union all select 3 as id, 'c3' as val, 'ccc1' as val_c from dual
;

select
  a.id
 ,b.val
 ,b.val_b
 ,b.val_c
from
  tab_a a
  left join tab_b b on b.id = a.id
  left join tab_c b on b.id = a.id
order by
  a.id
;

正如你所看到的,我在另一个表(重复)上重用了“B”别名。这是结果:

ID VAL        VAL_2      VAL_3
---------- ---------- ---------- ----------
         1 b1         bbb1       ccc1
         2 b2         bbb2       (null)
         3 (null)     (null)     ccc1

所以:

  • 两个表中都存在列“瓦尔”取自最先声明的tab_b
  • 列“瓦尔_B”取自tab_b
  • 列“瓦尔_c”取自tab_c

看起来Oracle为两个表分配了具有优先级的别名,并且它没有将其报告为不正确的语法。但这是我想避免的事情。我很惊讶在我的查询中发现了这个Bug。这种行为已知吗?可以做些什么来强制使用唯一的别名吗?

fgw7neuy

fgw7neuy1#

由于我的声誉,我不能发表评论,但最近有这个问题,想提的是固定在23c。

SQL> select
  2    a.id
  3   ,b.val
  4   ,b.val_b
  5   ,b.val_c
  6  from
  7    tab_a a
  8    left join tab_b b on b.id = a.id
  9    left join tab_c b on b.id = a.id
 10  order by
 11    a.id
 12  ;
 ,b.val
  *
ERROR at line 3:
ORA-00918: column ambiguously defined

相关问题