Chr☆s Kwok 的技术笔记

.NET, C#, WPF, WCF, WF, .NetCore & LINQ ... I know how it works because I know why it works ...

博客园 首页 新随笔 订阅 管理

【ORACLE】调整序列的当前种子值

--必须用SYS用户执行脚本;或具有SYSDBA角色登录;
CREATE OR replace
PROCEDURE      seq_reset_startnum(v_seqname varchar2, v_startnum number) AS n number(10); 
v_step number(10):=1;--步进 
tsql varchar2(200); 
BEGIN 
EXECUTE immediate 'select '||v_seqname||'.nextval from dual' INTO n; 
n:=v_startnum - n - v_step;--从10000001开始 
tsql:='alter sequence '||v_seqname||' increment by '|| n; 
EXECUTE immediate tsql; 
EXECUTE immediate 'select '||v_seqname||'.nextval from dual' INTO n; 
tsql:='alter sequence '||v_seqname||' increment by '||v_step; 
EXECUTE immediate tsql; 
END seq_reset_startnum; 

--必须用SYS用户执行脚本;或具有SYSDBA角色登录;
EXEC seq_reset_startnum('"CONCEPT"."DOCMETADATA_METADATAID_SEQ"', 1110000000);

当前值与最大ID值相差不是太大的可以使用下面脚本:
(注意:如果相差过大,譬如相差1个亿,则执行脚本脚本会很耗时)

--重置"CONFIG"."DOCFIELD"序列的当前值
DECLARE
 indexnum number;
 seqnumber number;
 tablenum number;
 indexintr number;
 BEGIN
   SELECT MAX(DOCFIELDID) INTO indexnum FROM "CONFIG"."DOCFIELD";
   SELECT  "CONFIG"."DOCFIELD_DOCFIELDID_SEQ".nextval INTO seqnumber FROM DUAL;
   indexintr := indexnum - seqnumber;
   IF indexintr > 0 THEN
     FOR ind IN 1..indexintr loop
          SELECT "CONFIG"."DOCFIELD_DOCFIELDID_SEQ".nextval INTO tablenum FROM DUAL;
     END loop ;
   END if;
end;
/

 

posted on 2018-11-14 17:17  Chr☆s  阅读(729)  评论(0编辑  收藏  举报