Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get counts of all tables in a schema

Tags:

oracle

plsql

I am trying to get the record counts of all tables in a schema. I am having trouble writing the PL/SQL. Here is what I have done so far, but I am getting errors. Please suggest any changes:

DECLARE v_owner varchar2(40); v_table_name varchar2(40);  cursor get_tables is select distinct table_name,user from user_tables where lower(user) = 'SCHEMA_NAME';   begin  open get_tables; fetch get_tables into v_table_name,v_owner;      INSERT INTO STATS_TABLE(TABLE_NAME,SCHEMA_NAME,RECORD_COUNT,CREATED)     SELECT v_table_name,v_owner,COUNT(*),TO_DATE(SYSDATE,'DD-MON-YY') FROM         v_table_name;  CLOSE get_tables;  END; 
like image 475
Ram Avatar asked May 22 '12 15:05

Ram


People also ask

How do you get the count of all tables in a schema in SQL?

declare v_count integer; begin for r in (select table_name, owner from all_tables where owner = 'SCHEMA_NAME') loop execute immediate 'select count(*) from ' || r. table_name into v_count; INSERT INTO STATS_TABLE(TABLE_NAME,SCHEMA_NAME,RECORD_COUNT,CREATED) VALUES (r.

How do I find the row count of all tables in a schema?

Aggregate row counts per schema This can be achieved by using the following query. SELECT table_schema, SUM(row_count) AS total_rows FROM ( SELECT table_schema, count_rows_of_table(table_schema, table_name) AS row_count FROM information_schema.

How do you count the number of tables in a schema?

SELECT Count(*) FROM DBA_TABLES; to get the count of tables. or SELECT TABLE_NAME, COUNT(*) OVER () FROM DBA_TABLES to get both.

How do I Count rows in a schema?

Row count at SQL execution time: The "real" current row count, which requires that you actually issue SQL to count the rows in all of the tables (time consuming). To count all of the rows in real time, a simple SQL*Plus script will suffice: Here is a PL/SQL approach for counting all tables in a schema. Here, you first vonnect as the schema owner:

How to get row counts for each table in SQL Server?

sys.partitions is a SQL Server System Catalog view which contains a row for each partition of all the tables and most types of indexes in the database. Using this you can get a row counts for each tables in database.

How to query the number of rows in each table through SSMS?

Query the number of rows in each table through SSMS is a simple task, just follow these steps: Select the Object Explorer panel; Click to expand until the desired database; Select the Tables folder; See this output SQL script in the image below. In the Tables folder select the Object Explorer panel, so let's see the Object Explorer Details panel;

How to get row counts for each table in adventureworks2017?

sys.partitions is a SQL Server System Catalog view which contains a row for each partition of all the tables and most types of indexes in the database. Using this you can get a row counts for each tables in database. You can see, it returns row counts for each table in database Adventureworks2017.


2 Answers

This can be done with a single statement and some XML magic:

select table_name,         to_number(extractvalue(xmltype(dbms_xmlgen.getxml('select count(*) c from '||owner||'.'||table_name)),'/ROWSET/ROW/C')) as count from all_tables where owner = 'FOOBAR' 
like image 79
a_horse_with_no_name Avatar answered Sep 20 '22 01:09

a_horse_with_no_name


This should do it:

declare     v_count integer; begin      for r in (select table_name, owner from all_tables               where owner = 'SCHEMA_NAME')      loop         execute immediate 'select count(*) from ' || r.table_name              into v_count;         INSERT INTO STATS_TABLE(TABLE_NAME,SCHEMA_NAME,RECORD_COUNT,CREATED)         VALUES (r.table_name,r.owner,v_count,SYSDATE);     end loop;  end; 

I removed various bugs from your code.

Note: For the benefit of other readers, Oracle does not provide a table called STATS_TABLE, you would need to create it.

like image 33
Tony Andrews Avatar answered Sep 21 '22 01:09

Tony Andrews