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='||''''||ld.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;
我的数据库是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='||''''||ld.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;