3

I know i need to use this query to get the list of tables for a schema: select table_name from all_tables where owner='schema'

I know the following query counts the record in a table: select count(*) from schema.table

There are 2400+ tables in that schema. My question is how to count the number of records from all the tables using one step?

2 Answers 2

2

You can use DBMS_XMLGEN.GETXMLTYPE function to do this in one shot:

SQL> select table_name
  2       , to_number
  3         ( extractvalue
  4           ( dbms_xmlgen.getxmltype('select count(*) c from ' || table_name)
  5           , '/ROWSET/ROW/C'
  6           )
  7         ) cnt
  8    from user_tables
  9   order by table_name
 10  /        

TABLE_NAME                            CNT
------------------------------ ----------
... [output removed] ...

71 rows selected.

But if your schema contains a lot of data, this might take a long time. Just selecting NUM_ROWS might be sufficient if estimations are ok as well.

Regards,
Rob.

Sign up to request clarification or add additional context in comments.

Comments

1

The table ALL_TABLES contains the column NUM_ROWS. (You can get a description of the table with the following SQL statement: DESCRIBE ALL_TABLES;)

The following statement shows the number of records for every table:

SELECT TABLE_NAME, NUM_ROWS FROM ALL_TABLES WHERE OWNER='SCHEMA';

To get the number of records in all tables of your schema, use:

SELECT SUM(NUM_ROWS) FROM ALL_TABLES WHERE OWNER='SCHEMA';

2 Comments

NUM_ROWS Is populated when statistics are gathered. It is therefore almost always (a) an estimate and (b) out of date. Depending on how, and how often, the stats are gathered it may be wildly innaccurate. It might be useful for a quick hint on which tables are likely to be largest, but other than that I wouldn't rely on it...
Thank you all, it really helped. As I only needed to know which tables are populated and proximate row count was enough, the NUM_ROWS column did it for me. Thanks again! T.

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.