Count No .Of Rows in tables under a schema (anonymous proc).
run as sys:
declare
--
-- Anonymous pl/sql code to count rows in tables of interest for v8.1+
--
-- basic logic
-- create list of target tables (cursor)
-- while more tables in list
-- dynamically generate select count
-- print or store results
-- ---------------------------------------------------------------------
v_ct number := 0 ;
v_sqlcode number := 0 ;
v_stmt varchar2(90) ;
-- modify cursor select for tables of interest, order by.
cursor c_tbl is
select owner, table_name
from sys.dba_tables
where owner = '&OWNER';
--
r_tbl c_tbl%rowtype;
--
begin
open c_tbl;
loop
fetch c_tbl into r_tbl;
exit when c_tbl%notfound;
v_stmt := 'select count(*) from '||r_tbl.owner||'.'||r_tbl.table_name;
execute immediate v_stmt into v_ct;
v_sqlcode := SQLCODE;
if v_sqlcode = 0
-- An insert into a row count history table should probably be here
then dbms_output.put_line('Table '||r_tbl.owner||'.'||
rpad(r_tbl.table_name,30)||
' count is '||to_char(v_ct,'999999999990')
);
else dbms_output.put_line('Bad return code'||v_sqlcode||
' on select of '||r_tbl.owner||
'.'||r_tbl.table_name
);
end if;
end loop;
close c_tbl;
end;
******************
No comments:
Post a Comment