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;
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.
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.
SELECT Count(*) FROM DBA_TABLES; to get the count of tables. or SELECT TABLE_NAME, COUNT(*) OVER () FROM DBA_TABLES to get both.
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:
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.
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;
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.
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'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With