Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Oracle dynamic find number of rows and columns per table

I have the following query, which returns the number of rows per table in a schema.

Can this also be modified to RETURN the number of columns per table too?


CREATE table stats (
table_name VARCHAR2(128),
num_rows NUMBER,
num_cols NUMBER 
);
/

DECLARE
val integer;
BEGIN
for i in (SELECT table_name FROM all_tables WHERE owner = 'Schema')
LOOP
EXECUTE IMMEDIATE 'SELECT count(*) from ' || i.table_name INTO val;
INSERT INTO stats VALUES (i.table_name,val);
END LOOP;
END;
/

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use the ALL_TAB_COLS dictionary table:

DECLARE
  val integer;
BEGIN
  FOR i IN (
    SELECT table_name,
           COUNT(*) AS num_cols
    FROM   all_tab_cols
    WHERE  owner = 'Schema'
    GROUP BY table_name
  )
  LOOP
    EXECUTE IMMEDIATE 'SELECT count(*) from ' || i.table_name INTO val;
    INSERT INTO stats VALUES (i.table_name,val, i.num_cols);
  END LOOP;
END;
/

db<>fiddle here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading