When the same table alias is used to refer a table or view in the join list as well as a "with" view, priority is given to the former.
Is this expected? If so, why?
Example:
========
with t1 as (select 'DUMMY' X from dual)
select t2.*
from bibtest t1,
bibtest_2 t2
where t1.a = t2.a;
Additional details are present in the link as well.
Kindly review and help.
Thanks.
When writing a query, you can use aliases that match the names of other schema objects:
create table t as
select level c1 from dual
connect by level <= 5;
create or replace view vw as
select count(*) c from t;
select vw.* from t vw;
C1
1
2
3
4
5
select * from vw;
C
5
In the first query above, the table alias matches the name of the view we created. The database resolves this to the table because the view
isn't referenced in a from clause. Thus it's out-of-scope.
Think of the with clause as defining an on-the-fly view. So even though a table alias matches the named subquery, you've not accessed the CTE in the query itself. Thus the query never references the CTE's contents.
For example, the query below uses a with clause having the same name as a table alias, but the main query
doesn't select from the CTE. So just like any other view in the schema, the database ignores its contents and dual is not read by the query:
set serveroutput off
with t1 as (
select dummy from dual
)
select * from t t1;
C1
1
2
3
4
5
select *
from dbms_xplan.display_cursor( format => 'BASIC LAST' );
----------------------------------
| Id | Operation | Name |
----------------------------------
| 0 | SELECT STATEMENT | |
| 1 | TABLE ACCESS FULL| T |
----------------------------------
If you do access the CTE, the database is no longer able to resolve the query and throws an error:
with t1 as (
select dummy from dual
)
select * from t t1, t1;
ORA-00918: column ambiguously defined