2

I have a database and I want to get the names of the tables in that database along with the number of data rows in those tables.

select table_name from dba_tables where owner = 'owner'

returns the names of all the tables in the database, but I can't get the number of rows in each table.

Querying the num_rows column would not give the row count in real time since it requires all the tables to run the "analyze" command on them.

Is there a way to get both columns, the table name and the row count of each respective table using a single query?

2 Answers 2

3

Use:

SELECT 
   table_name, 
   num_rows,
   last_analyzed
FROM dba_tables 

You may need to update statistics before:

exec dbms_stats.gather_schema_stats(ownname => 'NAME');
Sign up to request clarification or add additional context in comments.

Comments

0
with function row_count(tab_name in varchar2) return number as
    rc number;
begin
    execute immediate 'select count(*) from ' || tab_name into rc;
    return rc;
end;
select table_name, row_count(table_name) as row_count from all_tables
    where owner = 'owner';
/

1 Comment

Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.