Tuesday, May 31, 2011

Missing Sequence Numbers in a Table...

Title says it all I guess.
But still want to blog out though...
Its quite common that at least one column in one table in the database will be depending on Oracle SEQUENCE and there will be a code in place to get the NEXTVAL from that sequence to populate.
And I am pretty sure that because of few reasons the NEXTVAL that is fetched is not really committed to the table and thus there will be a sequence miss in the table.

The following query gets you all the sequences that were generated but never made it table.
----------------------------------------------------------------------------------------------
--with one sql. only catch is that the number of rows in the actual table should be
--less than or equal to the number of rows in dba_objects.
select missing_seqs
from (
select rownum missing_seqs from dba_objects
minus
select seq from santest --replace with the actual column an table name here
)
where missing_seqs <= (select max(seq) from santest); --replace with the actual column an table name here
----------------------------------------------------------------------------------------------
--pl/sql block for without any catches and gotchas...
set serveroutput on size unlimited
declare
t_missing_seq number;
t_start_seq number := 1; --replace this with the actual seq value that is started off in the table
t_max_seq number;
x char;
begin
select max(seq) into t_max_seq from santest; --replace santest with the actual table where the sequence column is. also replace (seq) witht the actual column name
for i in t_start_seq..t_max_seq loop
begin
select 'x'
into x
from santest --replace santest with the actual table
where seq = i; --replace seq with the actual column name
exception
when no_data_found then
dbms_output.put_line('Missing Sequence --> '||i);
end;
end loop;
end;
/
----------------------------------------------------------------------------------------------

Wednesday, May 25, 2011

Number of Rows per Partition in a Table

Simple and handy anonymous block to share that provides the total number of rows per partition in a given table:

___________________________________________________________________
set serveroutput on size unlimited
set echo off
set verify off
declare
cursor c1 is
select table_name,partition_name
from all_tab_partitions
where table_name = upper('&Table_Name');
t_count number:=0;
begin
for i in c1
loop
execute immediate 'select count(1) from '||i.table_name||' partition('||i.partition_name||')' into t_count;
dbms_output.put_line('The Partition '||i.partition_name||' of table '||i.table_name||' has '||t_count||' rows');
end loop;
end;
/
___________________________________________________________________