0

I have a requirement I need your help with.

Number of rows in a table : 130

That is the only data I have. Based on this, Is it possible to find out the table names from an Oracle Database that contain 130 rows in them.

Thanks Sam

2
  • if statistics are up to date, you can try to find it using the all_tables table and filter on the num_rows column.
    – Kobi
    Jan 20, 2016 at 13:57
  • Atleast you know the schema? In that case, you can do a dynamic SQL by fetching count of every table. It would not depend on statistics, but very costly if table count is more. Jan 20, 2016 at 14:38

3 Answers 3

1
SELECT TABLE_NAME FROM dba_tables WHERE num_rows = 130  
-- num_rows = 130 can be replaced with any requirement you have 
3
  • This is not reliable, depending on statistics. If stats are not up to date, this result may be wrong.
    – Aleksej
    Jan 20, 2016 at 15:07
  • @Aleksej I answered based on the OP's needs. There are indeed a lot of constraints that might affect the final result. Jan 20, 2016 at 15:15
  • @Shadow Do not forget to mark your selected answer after you are fully satisfied with one. Jan 20, 2016 at 17:36
0

You can try with some dynamic SQL:

declare
  n number;
begin
    for t in ( 
               select owner || '.' || table_name as tab
               from dba_tables
               where owner = 'YOUR_SCHEMA'  /* IF YOU KNOW THE SCHEMA */
              )
    loop
        execute immediate 'select count(1) from ' || t.tab into n;
        if n = 130 then
            dbms_output.put_line('Table ' || t.tab );
        end if;
    end loop;
end; 

Please consider that, depending on the number of tables/records in your DB, this can take very long to run.

0

I hope this query may help you:

Query 1 : SELECT CONCAT('SELECT COUNT(*) as cnt FROM ', table_name, ' union all') FROM information_schema.tables WHERE table_schema = 'aes';

Query 2: select max(attendance) from ( paste the results obtained from the above query and remove the last union all) as tmptable;

Reference: Find Table with maximum number of rows in a database in mysql

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.