Select all tables without records

I need to display all tables with null records.

I tried,

select * from user_all_tables where (select count(*) from user_all_tables)=0;

But that does not work. How do I redesign this query? Thank.

+5
source share
5 answers

If all your tables are parsed, you can check the num_rowstable column user_tables.

Otherwise, you will need PL / SQL to do this work. This will output all the tables of your current user without records (use all_tablesif you need tables of other users):

Set Serveroutput On;

Declare
  cnt PLS_INTEGER;
Begin
  For c In ( Select table_name From user_tables ) Loop
    Execute Immediate 'Select Count(*) From "' || c.table_name || '" where rownum=1'
            Into cnt;
    If( cnt = 0 ) Then
      dbms_output.put_line( c.table_name );
    End If;
  End Loop;
End;
+6
source

PL/SQL select (*) . dbms_xmlgen, :

select table_name
  from ( select table_name
              , extractvalue
                ( dbms_xmlgen.getxmltype('select count(*) c from '|| table_name)
                , '/ROWSET/ROW/C'
                ) cnt
              , rownum to_prevent_predicate_push
           from user_tables
       )
 where cnt = '0'

, .

+3

Change the accepted answer, but using a more efficient method.

Declare
  cnt PLS_INTEGER;
Begin
  For c In ( Select table_name From user_tables ) Loop
    begin
       Execute Immediate 'Select 1 From dual where exists (select 1 from ' || c.table_name ||')' Into cnt;
    exception when no_data_found then
      dbms_output.put_line( c.table_name );
    end;  
  End Loop;
End;
+2
source
select TABLE_NAME
from USER_ALL_TABLES
where NUM_ROWS = 0
0
source

This answer is one Fetch-per-table more efficient than Rene's. SELECT INTO requires Extra Fetch to find out if the TOO_MANY_ROWS exception should be thrown. We can take control of this process with an explicit cursor and DO NOT make unnecessary additional selection.

Declare
  cnt PLS_INTEGER;
  s_Cur Varchar2(255);
  c_Cur Sys_refcursor;
Begin
   For c In ( Select table_name From user_tables ) Loop

      s_Cur := 'Select 1  From dual where exists (select 1 from ' || c.table_name ||')';    

      Open c_Cur For s_cur ;
      Fetch c_cur into cnt;        
      If c_cur%NOTFOUND then 
          dbms_output.put_line( c.table_name );
      end if;

   End Loop;
End;
0
source

All Articles