landolakes
IS-IT--Management
I am trying to get an accurate row count for all tables owned by a user at a specific moment in time. Querying the user_tables table seemed like the ideal solution... just run a dbms stats, and then query the table. However, since the num_rows columns in dba_tables and user_tables only seems accurate when a dbms_stats is run with COMPUTE option on, and therefore inaccurate when an ESTIMATE is used on dbms_stats, I cannot use that column as I had wanted.
I therefore began attemps to run real-time counts of records based on the table names in user_tables and write to another table the results. I cannot get my simple cursor loop to work.
Is there a better way to get accurate record counts or are there any suggestions to get my PL/SQL to work? Thanks!
I therefore began attemps to run real-time counts of records based on the table names in user_tables and write to another table the results. I cannot get my simple cursor loop to work.
Is there a better way to get accurate record counts or are there any suggestions to get my PL/SQL to work? Thanks!
Code:
CREATE TABLE TABLECNT
(TABLE_NAME VARCHAR2(40 CHAR),
NUM_ROWS INTEGER);
DECLARE
C_TABLE_NAME VARCHAR2(40);
CURSOR CNTCURS IS (SELECT TABLE_NAME FROM USER_TABLES);
BEGIN
OPEN CNTCURS;
LOOP
FETCH CNTCURS INTO C_TABLE_NAME;
EXIT WHEN CNTCURS%NOTFOUND OR CNTCURS%NOTFOUND IS NULL;
INSERT INTO TABLECNT (TABLE_NAME, NUM_ROWS)
VALUES(C_TABLE_NAME, (SELECT COUNT(1) FROM C_TABLE_NAME));
END LOOP;
END;