Get row_count of partitions of a table - oracle database script

Get row_count of partitions of a table - Oracle Database Script.

SET serverout ON SIZE 1000000
SET verify OFF DECLARE sql_stmt varchar2(1024);

ROW_COUNT number;


CURSOR get_tab IS
SELECT TABLE_NAME,
       partition_name
FROM dba_tab_partitions
WHERE table_owner=upper('&&TABLE_OWNER')
  AND TABLE_NAME='&&TABLE_NAME';

BEGIN dbms_output.put_line('Checking Record Counts for table_name');

dbms_output.put_line('Log file to numrows_part_&&TABLE_OWNER.lst ....');

dbms_output.put_line('....');


FOR get_tab_rec IN get_tab LOOP BEGIN sql_stmt := 'select count(*) from &&TABLE_OWNER..'||get_tab_rec.table_name ||' partition ( '||get_tab_rec.partition_name||' )';

EXECUTE IMMEDIATE sql_stmt INTO ROW_COUNT;

dbms_output.put_line('Table '||rpad(get_tab_rec.table_name ||'('||get_tab_rec.partition_name||')', 50) ||' '||TO_CHAR(ROW_COUNT)||' rows.');


EXCEPTION WHEN others THEN dbms_output.put_line ('Error counting rows for table '||get_tab_rec.table_name);

END;

END LOOP;

END;

/
SET verify ON