求一个永不重复的序列? ( 积分: 30 )

  • 主题发起人 主题发起人 otqsoft
  • 开始时间 开始时间
O

otqsoft

Unregistered / Unconfirmed
GUEST, unregistred user!
我现在作一个系统,要求三地(安徽,上海和海南)数据同步,所以用到了很多的触发器,如下,当每触发一次时,我要取一个单据号,函数如下: 单号的后几位是一个序列生成的,可是发现有时候取的单据号还是一样的,那位朋友有好的方法,麻烦帮帮忙吧,万分感激..
我的数据库是oracle817
create or replace function GetSendTicket return varchar2
is -- 得到发送单号
aerr varchar2(25) := '00000000000000000000';
r_no number(2) := 0;
trows number(10) := 0;
arows varchar2(20) := '';
ticket varchar2(20) := '';
begin
select to_char(sysdate,'yymmdd') as tcik into ticket from dual;
select sequence_modify_record.nextval into arows from dual;
if length(arows) = 1 then arows := '0000000000000' || arows;
elsif length(arows) = 2 then arows := '000000000000' || arows;
elsif length(arows) = 3 then arows := '00000000000' || arows;
elsif length(arows) = 4 then arows := '0000000000' || arows;
elsif length(arows) = 5 then arows := '000000000' || arows;
elsif length(arows) = 6 then arows := '00000000' || arows;
elsif length(arows) = 7 then arows := '0000000' || arows;
elsif length(arows) = 8 then arows := '000000' || arows;
elsif length(arows) = 9 then arows := '00000' || arows;
elsif length(arows) = 10 then arows := '0000' || arows;
elsif length(arows) = 11 then arows := '000' || arows;
elsif length(arows) = 12 then arows := '00' || arows;
elsif length(arows) = 13 then arows := '0' || arows;
end if;
ticket := ticket||arows;
return ticket;
exception
when others then
rollback;
return aerr;
end GetSendTicket;

create or replace trigger tg_base_area
after insert or delete or update on base_area
for each row


declare
-- 区域信息
-- insert 状态 [0] update [1] delete [2]
tSql varchar2(500):= '';
Area1 varchar2(10) := '';
statu varchar2(2) := '';
tname varchar2(50) := '';
ticket varchar2(20) := '';
twhile varchar2(100):= '';
FArea1 number(10) := 0;
begin
tname := 'base_area';
select tvalue into Area1 from send_area where id='01';
select count(*) into FArea1 from get_data_tmp where t_name=tname and opt_server=Area1;
ticket := GetSendTicket;
if inserting then statu := '0';
elsif updating then statu := '1';
elsif deleting then statu := '2';
end if;
if inserting or updating then
if inserting then
statu := '0';
twhile:= '';
elsif updating then
statu := '1';
twhile:= 'base_no='||''''||:new.base_no||'''';
end if;
/* 表 base_area
base_no,base_name,quick_code,base_leve
*/
if FArea1 = 0 then
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_while,opt_server) values(
ticket,tname,'base_no','varchar2',:new.base_no,statu,twhile,Area1);
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_while,opt_server) values(
ticket,tname,'base_name','varchar2',:new.base_name,statu,twhile,Area1);
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_while,opt_server) values(
ticket,tname,'quick_code','varchar2',:new.quick_code,statu,twhile,Area1);
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_while,opt_server) values(
ticket,tname,'base_leve','number',:new.base_leve,statu,twhile,Area1);
end if;
elsif deleting then
statu := '2';
twhile:= 'base_no='||''''||:old.base_no||'''';
if FArea1 = 0 then
insert into send_data(ticket_no,t_name,opt_statu,opt_while,opt_server) values(
ticket,tname,statu,twhile,Area1);
end if;
end if;
exception
when others then
rollback;
RAISE_APPLICATION_ERROR(-20800,'触发器向发送表发送数据错误!');
end tg_base_area;
 
create or replace function Send_sendfp(Btick in varchar2, tArea in varchar2)
return varchar2 -- 发货单审核
is
retY varchar2(10) := 'yes';
retN varchar2(10) := 'no';
tSql varchar2(500) := '';
Area varchar2(20) := '';
Aname varchar2(50) := '';
Aticket varchar2(30) := ''; -- 传输单号
Bticket varchar2(30) := ''; -- 业务单号

cursor a_base(ticket varchar2) is
select ticket_no, hh, khbh, fzr, ywy, fkfs, depot, ypbh, ph, sl, jj, dj, je, rq, zd, flag, tax,
send, w_man, w_unit, create_user, link_man, link_tel,opt_fh from sendfp where ticket_no=ticket;
begin
Area := tArea;
Aname := 'sendfp';
Bticket := Btick;
for r_add in a_base(Bticket) loop
Aticket:= GetSendTicket;
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_server) values(
Aticket,Aname,'ticket_no','varchar2',r_add.ticket_no,'0',Area);
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_server) values(
Aticket,Aname,'hh','varchar2',r_add.hh,'0',Area);
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_server) values(
Aticket,Aname,'khbh','varchar2',r_add.khbh,'0',Area);
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_server) values(
Aticket,Aname,'fzr','varchar2',r_add.fzr,'0',Area);
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_server) values(
Aticket,Aname,'ywy','varchar2',r_add.ywy,'0',Area);
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_server) values(
Aticket,Aname,'fkfs','varchar2',r_add.fkfs,'0',Area);
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_server) values(
Aticket,Aname,'depot','varchar2',r_add.depot,'0',Area);
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_server) values(
Aticket,Aname,'ypbh','varchar2',r_add.ypbh,'0',Area);
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_server) values(
Aticket,Aname,'ph','varchar2',r_add.ph,'0',Area);
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_server) values(
Aticket,Aname,'sl','number',r_add.sl,'0',Area);
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_server) values(
Aticket,Aname,'jj','number',r_add.jj,'0',Area);
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_server) values(
Aticket,Aname,'dj','number',r_add.dj,'0',Area);
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_server) values(
Aticket,Aname,'je','number',r_add.je,'0',Area);
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_server) values(
Aticket,Aname,'rq','date',to_char(r_add.rq,'yyyy-mm-dd hh24:mi:ss'),'0',Area);
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_server) values(
Aticket,Aname,'zd','varchar2',r_add.zd,'0',Area);
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_server) values(
Aticket,Aname,'flag','number',r_add.flag,'0',Area);
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_server) values(
Aticket,Aname,'tax','number',r_add.tax,'0',Area);
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_server) values(
Aticket,Aname,'send','varchar2',r_add.send,'0',Area);
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_server) values(
Aticket,Aname,'w_man','varchar2',r_add.w_man,'0',Area);
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_server) values(
Aticket,Aname,'w_unit','varchar2',r_add.w_unit,'0',Area);
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_server) values(
Aticket,Aname,'create_user','varchar2',r_add.create_user,'0',Area);
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_server) values(
Aticket,Aname,'link_man','varchar2',r_add.link_man,'0',Area);
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_server) values(
Aticket,Aname,'link_tel','varchar2',r_add.link_tel,'0',Area);
insert into send_data(ticket_no,t_name,t_field,t_type,t_value,opt_statu,opt_server) values(
Aticket,Aname,'opt_fh','varchar2',r_add.opt_fh,'0',Area);
end loop;
commit;
return retY;
exception
when others then
rollback;
return retN;
end Send_sendfp;
这个是手工发送时触发的一个函数,取单号的函数和触发器相同,可以有时候也会出现重复的单号?
 
那位朋友有这方面的经验,麻烦指点一下吧,谢谢啦。。。。
 
帮你顶一下。
 
问题解决了,根据序列求一个流水号,并不会重复,是程序在传输过程中重复了
 
后退
顶部