Sei sulla pagina 1di 5

11/10/2017 oracle - Query to find all empty tables - Stack Overflow

Query to find all empty tables Ask Question

Considering that I have a Schema named SBST

3 I want to find all empty tables list in this SBST Schema. Is there any PL/SQL procedure to find that.
I found few. But those were using user tables where I was not able specify the Schema name
SBST.

I was using this

1 select table_name from dba_tables where owner ='SBST'


having count(*)=0 group by table_name

What's wrong in the above query?

oracle oracle11g

share improve this question edited Jun 26 '12 at 7:16 asked Jun 21 '12 at 9:11
Sathya user472625
14.6k 20 77 111 60 2 4 13
add a comment

4 Answers active oldest votes

Similar to @shareef's answer, but using dynamic SQL to avoid having to create the
temporary .sql file. You'll need dbms_output to be visible, e.g. with set serveroutput on in
6 SQL*Plus - don't know about Toad.

declare
cursor c(p_schema varchar2) is
select 'select ''' || table_name || ''' from ' ||
p_schema ||'.' || table_name || ' where rownum < 2 ' ||
' having count(*) = 0' as query
from all_tables
where owner = p_schema
order by table_name;
l_table all_tables.table_name%TYPE;
begin
for r in c('SBST') loop
begin
execute immediate r.query into l_table;
exception
when no_data_found then continue;
end;

dbms_output.put_line(l_table);
https://stackoverflow.com/questions/11134992/query-to-find-all-empty-tables 1/5
11/10/2017 oracle - Query to find all empty tables - Stack Overflow

end loop;
end;
/

Using all_tables seems more useful than dba_tables here so you know you can select from the
tables it lists. I've also included the schema in the from clause in case there other users have
tables with the same name, and so you can still see it if you're connected as a different user -
possibly avoiding synonym issues too.

Specifically what's wrong with your query... you've got the having and group by clauses the
wrong way around; but it will always return no data anyway because if SBST has any tables
then count (*) from dba_tables must be non-zero, so the having always matches; and if it
doesn't then, well, there's no data anyway so there's nothing for the having to match against.
You're counting how many tables there are, not how many rows are in each table.

share improve this answer edited Jun 21 '12 at 15:40 answered Jun 21 '12 at 11:13
Alex Poole
105k 6 73 131

1 having clause is not necessary. SELECT 1 FROM owner.table WHERE rownum < 2 woud be enough to
determine whether there is at least 1 row in the table. – ipip Jun 21 '12 at 15:25

@ipip - yes, and that's slightly faster on my data, which includes some reasonably big tables; but gives you
tables that do have data, and the OP wants those that don't. – Alex Poole Jun 21 '12 at 15:31

No, I'm talking rubbish; it's a lot faster if I run it on the DB that does actually have significant data, as you'd
expect. But it does list tables with data; combining both clauses seems to do the trick though. Thanks. –
Alex Poole Jun 21 '12 at 15:42

Taht's OK, I just wanted to point out the way, how to find an empty table. I often find myself doing the same
:-)– ipip Jun 22 '12 at 9:19

@peter:That helped a lot Alex – user472625 Jun 26 '12 at 7:23

add a comment

You can fire below query to find the list of tables haveing no data

3 select * from ALL_TABLES


where owner ='SBST'
AND NUM_ROWS = 0;

share improve this answer edited Aug 19 '15 at 12:26 answered Aug 19 '15 at 12:23
Andy ♦ user5243338
25.9k 17 84 133 31 1
add a comment

the straight forward answer is

2 select 'select ''' || table_name || ''' from ' || table_name || '


having count(*) = 0;' from dba_tables where owner='SBST';

https://stackoverflow.com/questions/11134992/query-to-find-all-empty-tables 2/5
11/10/2017 oracle - Query to find all empty tables - Stack Overflow

EXPLANATION

You can run this... it will just output the table names of those having no rows: assuming you use
sqlplus but i used toad to test it and it worked very well set echo off heading off feedback off lines
100 pages 0;

spool tmp.sql
select 'select ''' || table_name || ''' from ' || table_name || '
having count(*) = 0;' from user_tables where owner='SBST';
spool off;

@tmp.sql

If you open the "tmp.sql" file, you'll see for all tables....

select 'PERSONS' from PERSONS having count(*) = 0;


select 'DEPARTMENT' from DEPARTMENT having count(*)=0;

in your case you want a schema and schema is a user right the above code if you connect with the
user SBST but if you connect with other then you have to use DBA_TABLES and
assign ownerattribute to SBST

USER_TABLES is tables which you own ALL_TABLES is tables which own, and tables owner
by other users, which you have been granted excplicit access to DBA_TABLES is all tables in
the database

like this

set echo off heading off feedback off lines 100 pages 0;

spool tmp.sql
select 'select ''' || table_name || ''' from ' || table_name || '
having count(*) = 0;' from dba_tables where owner='SBST';
spool off;

@tmp.sql

All three are views of the underlying SYS tables, but the USER_ and ALL_ views joing in your
username/security info to limit the results

https://stackoverflow.com/questions/11134992/query-to-find-all-empty-tables 3/5
11/10/2017 oracle - Query to find all empty tables - Stack Overflow

**SUMMARY **

PLEASE JUST RUN THIS QUERY

select 'select ''' || table_name || ''' from ' || table_name || '


having count(*) = 0;' from dba_tables where owner='SBST';

share improve this answer edited Jun 26 '12 at 7:24 answered Jun 21 '12 at 9:20
shareef
3,805 4 35 60

@peter Is there any way to specify SBST directly in the query.I am executing in Toad.Datswhy –
user472625 Jun 21 '12 at 9:32

i added it above in the edit select 'select ''' || table_name || ''' from ' || table_name || '
having count(*) = 0;' from dba_tables where owner='SBST'; – shareef Jun 21 '12 at 9:32

@peter:The above prints the tablename along with having count.This means i want to copy all the result in a
file and execute this.Is there any query as such? – user472625 Jun 21 '12 at 10:12

@peter just run this <br> select 'select ''' || table_name || ''' from ' || table_name || '
having count(*) = 0;' from dba_tables where owner='SBST'; – shareef Jun 21 '12 at 10:56

add a comment

If table stats are up to date then you could use:

1 SELECT TABLE_NAME
FROM ALL_TAB_STATISTICS
WHERE (OWNER = 'ME')
AND (NUM_ROWS = 0);
https://stackoverflow.com/questions/11134992/query-to-find-all-empty-tables 4/5
11/10/2017 oracle - Query to find all empty tables - Stack Overflow

https://stackoverflow.com/questions/11134992/query-to-find-all-empty-tables 5/5

Potrebbero piacerti anche