使用oracle序列+oracle定时任务获取每月从1开始的流水码
--创建序列
–入库
create sequence rk_seq;
–出库
create sequence ck_seq;
–移库
create sequence yk_seq;
–创建存储过程
create or replace procedure reset_seq(rk_seq_name in varchar2,
ck_seq_name in varchar2,
yk_seq_name in varchar2) is
l_val number;
begin
–rk
execute immediate ‘select ‘ || rk_seq_name || ‘.nextval from dual’
INTO l_val;
execute immediate ‘alter sequence ‘ || rk_seq_name || ‘ increment by -‘ ||
l_val || ‘ minvalue 0’;
execute immediate ‘select ‘ || rk_seq_name || ‘.nextval from dual’
INTO l_val;
execute immediate ‘alter sequence ‘ || rk_seq_name ||
‘ increment by 1 minvalue 0’;
–ck
execute immediate ‘select ‘ || ck_seq_name || ‘.nextval from dual’
INTO l_val;
execute immediate ‘alter sequence ‘ || ck_seq_name || ‘ increment by -‘ ||
l_val || ‘ minvalue 0’;
execute immediate ‘select ‘ || ck_seq_name || ‘.nextval from dual’
INTO l_val;
execute immediate ‘alter sequence ‘ || ck_seq_name ||
‘ increment by 1 minvalue 0’;
–yk
execute immediate ‘select ‘ || yk_seq_name || ‘.nextval from dual’
INTO l_val;
execute immediate ‘alter sequence ‘ || yk_seq_name || ‘ increment by -‘ ||
l_val || ‘ minvalue 0’;
execute immediate ‘select ‘ || yk_seq_name || ‘.nextval from dual’
INTO l_val;
execute immediate ‘alter sequence ‘ || yk_seq_name ||
‘ increment by 1 minvalue 0’;
end;
–创建定时任务
declare
job number;
begin
dbms_job.submit(job,
‘ begin reset_seq(”rk_seq”, ”ck_seq”, ”yk_seq”);end;’,
sysdate,
‘TRUNC(LAST_DAY(SYSDATE))+1+1/24’);
end;
背景:数据WCS_TASK(WCS_任务)表中有一个WCS_ID(任务编码)的字段,任务编号规则:1位任务类型+4位年月+五位流水码 ,如12103000001 (TYPE–任务类型: 1–入库,2–出库,3–移库)
要求:新增WCS_TASK数据时,WCS_ID字段程序中自动生成,而且每个月的流水码要从1开始
难点:在于并发情况下生成流水码不能重复,并且每月重置
解决办法:利用数据库的序列来获取流水码,然后创建数据库定时任务,每个月1号重置序列
用法:数据库创建好序列和定时任务,直接在程序中查询相应序列即可获取不重复的流水码,序列的重置工作由定时任务自动执行