业务需求是:二元化编号规则:RYH+年月+001(开始),按月计算,每月1号重置为001
数据库中已有序列和函数如下:
解决方法:采用Oracle定时任务,每月1号重置该序列从1开始增长,SQL如下:
select GET_RYH_NO from dual; select SEQ_RYH_NO_ID.NEXTVAL from dual; select * from user_jobs;/*查询定时任务*/ /*新建每月01号重置二元化序列为1起始的存储过程*/ CREATE OR REPLACE procedure RESET_SEQ_RYH_NO_ID AS n NUMBER(10); tsql VARCHAR2(100); begin if TO_CHAR(SYSDATE,'DD') = '01' then /*每月1号重置*/ EXECUTE IMMEDIATE 'select SEQ_RYH_NO_ID.nextval from dual' INTO n; n := - (n - 1); tsql := 'alter sequence SEQ_RYH_NO_ID increment by ' || n; EXECUTE IMMEDIATE tsql; EXECUTE IMMEDIATE 'select SEQ_RYH_NO_ID.nextval from dual' INTO n; tsql := 'alter sequence SEQ_RYH_NO_ID increment by 1'; EXECUTE IMMEDIATE tsql; end if; end RESET_SEQ_RYH_NO_ID; /*定义定时任务*/ declare job number; BEGIN DBMS_JOB.SUBMIT( JOB => job, /*自动生成JOB_ID*/ WHAT => 'RESET_SEQ_RYH_NO_ID;', /*需要执行的存储过程名称或SQL语句*/ NEXT_DATE => sysdate, /*初次执行时间*/ INTERVAL => 'trunc(sysdate+1)' /*每隔1天执行一次*/ ); commit; end; /*移除定时任务*/ begin dbms_job.remove(126);/*括号中传入定时任务id*/ end;