Difference between revisions of "Oracle, Maximo - Resetting Sequences"

From ExxtremeWiki
Jump to: navigation, search
m
m
 
Line 142: Line 142:
  
 
Thanks to Simon at Simon@utilityapp.com for posting these scripts.
 
Thanks to Simon at Simon@utilityapp.com for posting these scripts.
Categories: Oracle | Maximo 6.x
+
[[Categories: Oracle, Maximo 6.x]]

Latest revision as of 20:20, 15 January 2012

Oracle, Maximo - Resetting Sequences

Oracle procedure to reset Maximo sequence for selected table, column

Provides a running log of what it is doing so that you know that it worked.


Example for running procedure for the Asset table below

set serveroutput on ; begin MAXIMOIMPORT_RESET_SEQUENCE('ASSET','ASSETID'); end;


The procedure can also be run from SQL Plus with the following EXECUTE command

execute MAXIMOIMPORT_RESET_SEQUENCE('ASSET','ASSETID');


NOTE: MAXSEQUENCE table is case sensitive, so use upper case for table and ID field names


FOr WOOWNERHISTORY

execute MAXIMOIMPORT_RESET_SEQUENCE('WOOWNERHISTORY','WOOWNERHISTORYID');


The SQL statement is:

create or replace

PROCEDURE "MAXIMOIMPORT_RESET_SEQUENCE" ( p_tbname IN STRING

, p_fieldname IN STRING

) AS

BEGIN

declare
v_tbname varchar2(200):= p_tbname;
v_fieldname varchar2(200):= p_fieldname;
v_SequenceName varchar2(100);
v_CurrentSequence int:=0;
v_MaxSequence int:=1;
v_SQL varchar2(1000);


begin

--Find the sequence name for this field

select sequencename into v_SequenceName
 from maxsequence
 where tbname = v_tbname and name = v_fieldname;

dbms_output.put_line(v_SequenceName);

--Find the max value from the table

v_SQL := 'select max(' || v_fieldname || ') from ' || v_tbname;

execute immediate v_SQL into v_MaxSequence;
dbms_output.put_line(v_maxSequence);


--Find current sequence value
v_SQL := 'SELECT ' || v_SequenceName || '.NEXTVAL FROM dual';
execute immediate v_SQL into v_CurrentSequence;
dbms_output.put_line(v_CurrentSequence);
--Reset sequence to 0
v_SQL := 'ALTER SEQUENCE ' || v_SequenceName || ' INCREMENT BY -' || (v_CurrentSequence) || ' MINVALUE 0 NOCACHE';
execute immediate v_SQL;
dbms_output.put_line(v_SQL);
v_SQL := 'SELECT ' || v_SequenceName || '.NEXTVAL FROM dual';
execute immediate v_SQL into v_CurrentSequence;
dbms_output.put_line(v_SQL);
dbms_output.put_line(v_CurrentSequence);


--Reset sequence to max value found in table

v_SQL := 'ALTER SEQUENCE ' || v_SequenceName || ' increment by ' || (v_MaxSequence-1) || ' MINVALUE 0 NOCACHE';
execute immediate v_SQL;
dbms_output.put_line(v_SQL);
v_SQL := 'SELECT ' || v_SequenceName || '.NEXTVAL FROM dual';
execute immediate v_SQL into v_CurrentSequence;
dbms_output.put_line(v_SQL);
dbms_output.put_line(v_CurrentSequence);
--Reset sequence increment to 1
v_SQL := 'ALTER SEQUENCE ' || v_SequenceName || ' increment by 1 MINVALUE 0 CACHE 20';
execute immediate v_SQL;
dbms_output.put_line(v_SQL);
v_SQL := 'SELECT ' || v_SequenceName || '.NEXTVAL FROM dual';
execute immediate v_SQL into v_CurrentSequence;
dbms_output.put_line(v_SQL);
dbms_output.put_line(v_CurrentSequence);
--Update maximo table
update MAXSEQUENCE SET MAXRESERVED = v_MaxSequence WHERE TBNAME = v_tbname and name = v_fieldname;
commit;


end;

END MAXIMOIMPORT_RESET_SEQUENCE;


Thanks to Simon at Simon@utilityapp.com for posting these scripts. Categories: Oracle, Maximo 6.x