定期重置数据库序列

Posted johnnyc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了定期重置数据库序列相关的知识,希望对你有一定的参考价值。

//建立一个从0开始的序列
drop sequence seq_sss_id;
create sequence seq_sss_id
increment by 1
start with 0
minvalue 0 
maxvalue 999999999;

//重置序列的存储过程

create or replace procedure reset_jis_sequence as
n number(10);
tsql varchar2(100);
begin
execute immediate ‘select seq_sss_id.nextval from dual‘ into n;
n:=-n;
tsql:=‘alter sequence seq_sss_id increment by ‘|| n;
execute immediate tsql;
execute immediate ‘select seq_sss_id.nextval from dual‘ into n;
tsql:=‘alter sequence seq_sss_id increment by 1‘;
execute immediate tsql;
end reset_jis_sequence;

//job定时(每天0:00定时运行reset_jis_sequence 储存过程)
declare
job number;
begin
sys.dbms_job.submit(job => job,
what => ‘reset_jis_sequence;‘,
next_date => to_date(‘21-04-2018‘, ‘dd-mm-yyyy‘),
interval => ‘TRUNC( SYSDATE + 1)‘);
commit;
end;

//查询job的id
select job,what,failures,broken from user_jobs

//根据job的ID执行job
begin
dbms_job.run(jobId);
end;

//根据job的ID删除job
BEGIN

SYS.DBMS_JOB.REMOVE(jobId);

COMMIT;

END;
































以上是关于定期重置数据库序列的主要内容,如果未能解决你的问题,请参考以下文章

将序列重置为特定值

不同步时如何重置postgres的主键序列?

不同步时如何重置postgres的主键序列?

批量生成序列重置SQL

Heroku - ClearDB:连接中止 - 对等方重置连接

从 Android 发送文件时从 WCF 服务重置连接