Skip to Main Content
  • Questions
  • "Table override": can I use this feature?

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question.

Asked: February 08, 2017 - 2:57 pm UTC

Last updated: February 15, 2017 - 3:10 am UTC

Version: 11g

Viewed 1000+ times

You Asked

Hi!

I created an empty table, lets say, tb_test (col1 varchar2(20 char), col2 varchar2(20 char));

Now I created a view of this table, lets say, vw_test as select * from tb_test;

If I do select * from vw_test; I will get nothing, it is empty.

If I prepend a with clause with the same structure of my empty table, lets say, tb_test (col1, col2) as (select 'a', 'b' from dual) and do select * from vw_test; I get data from tb_test (not the table one, but the subquery temp table one), like some sort of table override feature.

My question is: is that really the expected behavior? If yes, is there any real possibility of changes on this behavior? If no, so I think I can try some of these in real production solutions.

Thank you very much! Best regards,

EDIT:

Just to simplify, I'm running the following SQL through SQL Developer:

with

my_table as
(
select dummy from dual
)

select *
from my_table
;

The table my_table has 530982 in my schema, but this query returns a single row with the column dummy and value X.

I tried it via LiveSQL, but it was just a different behavior. It does select from the table of the schema, not the subquery temporary one. As I'm not the DB administrator, I don't know what exactly "enables" this behavior on our database. Do you know what could it be?

Thanks again!

and Connor said...

We evaluate names of objects from "inside out", ie within the current SQL, then within the current schema, then public syonyms etc etc.

SQL> create table my_table as select table_name from all_tables where rownum <= 10;

Table created.

SQL> select * from my_table;

TABLE_NAME
------------------------------
TYPE_MISC$
ATTRCOL$
ASSEMBLY$
LIBRARY$
VIEWTRCOL$
ICOLDEP$
OPQTYPE$
REFCON$
NTAB$
SUBCOLTYPE$

10 rows selected.

--
-- the WITH "overrides" the table name
--
SQL>
SQL> with
  2  my_table as
  3  (
  4  select dummy from dual
  5  )
  6  select *
  7  from my_table
  8  ;

D
-
X

1 row selected.

--
-- and here things look 'confusing' but noticed the small
-- typo, the WITH refers to "mytable" not "my_table" so we 
-- end up using the one in the dictionary
--
SQL>
SQL> with
  2  mytable as
  3  (
  4  select dummy from dual
  5  )
  6  select *
  7  from my_table
  8  ;

TABLE_NAME
------------------------------
TYPE_MISC$
ATTRCOL$
ASSEMBLY$
LIBRARY$
VIEWTRCOL$
ICOLDEP$
OPQTYPE$
REFCON$
NTAB$
SUBCOLTYPE$

10 rows selected.

SQL>




I've done the same example with live sql with the same results

https://livesql.oracle.com/apex/livesql/s/eki7h1of4il3wnuxlyamej23y


Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

but what about the "override of table" on a view query?

A reader, February 13, 2017 - 1:35 pm UTC

Hey Connor and asktom team, thanks for the reply.

So, this "inside out" table evaluation is the expected way it should work... nice! That's the answer I was looking for. :)

Also, one thing still confuses me... I sent you two examples on my question. My first example is a bit different from the second. The second worked well for me and you, "overriding" a selected table by including a query level temp table of same name. But the first example works for me and doesn't for you. Also, it doesn't on livesql.

On the first example a view is created and selected and the with clause "overrides" the table from the view's query. Why this works for me and not for you or on livesql? I saw that livesql version was 12c. Tried to find some switch to change its version to 11g, but haven't found it. Are there any config that might be enabling this behavior for me?

If you wish to convert your livesql from second example to the first one you could do:

1- get the query from statement 3 and create a view of it;
2- get the query from statement 4 and replace "from my_table" to "from <view>";

Thanks again and sorry for any typos... peace.
Connor McDonald
February 15, 2017 - 3:10 am UTC

Notice in my answer I give a top-to-bottom test case...

You could perhaps do the same :-)