Oracle中通常將自增sequence重置爲初始1時,都是刪除再重建,這種方式有不少弊端,依賴它的函數和存儲過程將失效,須要從新編譯。不過還有種巧妙的方式,不用刪除,利用步長參數,先查出sequence的nextval,記住,把遞增改成負的這個值(反過來走),而後再改回來。html
假設須要修改的序列名:seq_namesql
一、select seq_name.nextval from dual; //假設獲得結果5656函數
二、alter sequence seq_name increment by -5655 //注意是-(n-1)spa
三、select seq_name.nextval from dual; //再查一遍,走一下,重置爲1了code
四、alter sequence seq_name increment by 1; //還原htm
能夠寫個存儲過程,如下是完整的存儲過程,而後調用傳參便可:blog
1 create or replace procedure seq_reset(v_seqname varchar2) 2 as cur_seq number(28); 3 tsql varchar2(200); 4 begin 5 execute immediate 'select ' || v_seqname || '.nextval from dual' into cur_seq; 6 cur_seq:=-(cur_seq-1); 7 tsql:='alter sequence ' || v_seqname || ' increment by ' || cur_seq; 8 execute immediate tsql; 9 execute immediate 'select ' || v_seqname || '.nextval from dual' into cur_seq; 10 tsql:='alter sequence ' || v_seqname || ' increment by 1'; 11 execute immediate tsql; 12 end seq_reset; 13 /
須要調用時,執行(如sequence名稱爲SEQ_QT_PRCS_ID):rem
1 BEGIN 2 seq_reset('SEQ_QT_PRCS_ID'); 3 END; 4 /
轉載自《Oracle重置序列(不刪除重建方式)》get