Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle: select * from (select table_name from ... )?

Tags:

sql

oracle

Given a query that returns the name of tables, is it possible to evaluate the name and use it in a subsequent query?

E.G.

select count(1) from x where x in 
    (select table_name from ALL_TABLES where table_name like 'MY_TABLE_%');

Obviously this is invalid syntax, but it should illustrate what I'm trying to do.

like image 741
Synesso Avatar asked Jan 20 '23 03:01

Synesso


1 Answers

You can, but it requires that you resort to an XML query.

select
       table_name,
       to_number(
         extractvalue(
           xmltype(
             dbms_xmlgen.getxml('select count(*) c ' ||
                                ' from '||owner||'.'||table_name))
           ,'/ROWSET/ROW/C')) count
   from all_tables
  where table_name like 'MY_TABLE_%'
like image 189
Justin Cave Avatar answered Jan 31 '23 08:01

Justin Cave