Quick Search:
CODE
Oracle PL/SQL Code Library
JOBS
Find Or Post Oracle Jobs
FORUM
Oracle Discussion & Chat
 Oracle PL/SQL: Count rows in all tables Jump to:  
Category: >> Oracle PL/SQL >> Count rows in all tables Bookmark and Share

<< lastnext >>

Snippet Name: Count rows in all tables

Description: Count the number of rows for ALL tables in current schema.

Comment: (none)

Language: PL/SQL
Highlight Mode: PLSQL
Last Modified: March 03rd, 2009

SET serveroutput ON size 1000000
 
DECLARE
  t_c1_tname      user_tables.table_name%TYPE;
  t_command       VARCHAR2(200);
  t_cid           INTEGER;
  t_total_records NUMBER(10);
  stat            INTEGER;
  row_count       INTEGER;
  t_limit         INTEGER := 0;    -- Only show tables with more rows
  CURSOR c1 IS SELECT table_name FROM user_tables ORDER BY table_name;
BEGIN
  t_limit := 0;
  OPEN c1;
  LOOP
        FETCH c1 INTO t_c1_tname;
        EXIT WHEN c1%NOTFOUND;
        t_command := 'SELECT COUNT(0) FROM '||t_c1_tname;
        t_cid := DBMS_SQL.OPEN_CURSOR;
        DBMS_SQL.PARSE(t_cid,t_command,DBMS_SQL.native);
        DBMS_SQL.DEFINE_COLUMN(t_cid,1,t_total_records);
        stat := DBMS_SQL.EXECUTE(t_cid);
        row_count := DBMS_SQL.FETCH_ROWS(t_cid);
        DBMS_SQL.COLUMN_VALUE(t_cid,1,t_total_records);
        IF t_total_records > t_limit THEN
                DBMS_OUTPUT.PUT_LINE(RPAD(t_c1_tname,55,' ')||
                        TO_CHAR(t_total_records,'99999999')||' record(s)');
 
        END IF;
        DBMS_SQL.CLOSE_CURSOR(t_cid);
  END LOOP;
  CLOSE c1;
END;
/
 


Free
Oracle Magazine
Subscriptions
and Oracle White Papers


SQL University.net courses meet the most demanding needs of the business world for advanced education in a cost-effective manner. SQL University.net courses are available immediately for IT professionals and can be taken without disruption of your workplace schedule or processes.

Compared to traditional travel-based training, SQL University.net saves time and valuable corporate resources, allowing companies to do more with less. That's our mission, and that's what we deliver.

Click here to find out more
 
Home      :      Code Library      :      Sponsors      :      Privacy      :      Terms of Use      :      Contact Us 271 users online    © 2009 psoug.org

PSOUG LOGIN
Username: 
Password: 
Forgot your password?