欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

定期重置数据库序列

程序员文章站 2023-01-16 07:57:36
//建立一个从0开始的序列drop sequence seq_sss_id;create sequence seq_sss_idincrement by 1start with 0minvalue 0 maxvalue 999999999; //重置序列的存储过程 create or replace ......

//建立一个从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;