Home » SQL & PL/SQL » SQL & PL/SQL » how to simplify the following trigger code (10g)
how to simplify the following trigger code [message #382861] |
Sun, 25 January 2009 22:59  |
jyothsna1612
Messages: 68 Registered: June 2008
|
Member |
|
|
hi,
i've written the following code that is as per the requirements but how can that code even be reduced
either using INSERT ALL option or other way::
create table src(seq number ,len number);
create table dest(mod varchar2(1),out varchar2(1),
sequence number , length number);
create or replace trigger trig1
after insert or update or delete
of seq,len
on src
referencing new as new old as old
for each row
declare
vseq src.seq%type;
vlen src.len%type;
vmod varchar2(1);
vout varchar2(1);
begin
if updating ('seq') or inserting then
vseq:=:new.seq;
vlen:=:new.len;
vmod:='i';
vout:='n';
insert into dest(mod,out,sequence,length) values(vmod,vout,vseq,vlen);
end if;
if updating('seq') or deleting then
vseq:=:old.seq;
vlen:=:old.len;
vmod:='d';
vout:='y';
insert into dest(mod,out,sequence,length) values(vmod,vout,vseq,vlen);
end if;
if updating('len') then
vseq:=:new.seq;
vlen:=:new.len;
vmod:='u';
vout:='n';
insert into dest(mod,out,sequence,length) values(vmod,vout,vseq,vlen);
end if;
end;
SQL> insert into src values(1,2);
1 row created.
SQL> select * from src;
SEQ LEN
---------- ----------
1 2
SQL> select * from dest;
M O SEQUENCE LENGTH
- - ---------- ----------
i n 1 2
SQL> update src set seq=2;
1 row updated.
SQL> select * from dest;
M O SEQUENCE LENGTH
- - ---------- ----------
i n 1 2
i n 2 2
d y 1 2
SQL> update src set len=3;
1 row updated.
SQL> select * from dest;
M O SEQUENCE LENGTH
- - ---------- ----------
i n 1 2
i n 2 2
d y 1 2
u n 2 3
SQL> delete from src;
1 row deleted.
SQL> select * from dest;
M O SEQUENCE LENGTH
- - ---------- ----------
i n 1 2
i n 2 2
d y 1 2
u n 2 3
d y 2 3
SQL>
thanks
|
|
|
|
Re: how to simplify the following trigger code [message #382903 is a reply to message #382861] |
Mon, 26 January 2009 03:55   |
michael_bialik
Messages: 621 Registered: July 2006
|
Senior Member |
|
|
Another possibility is to remove all inrtermidiate variables:
begin
if updating ('seq') or inserting then
insert into dest(mod,out,sequence,length)
values('i','n',:new.seq,:new.len);
end if;
if updating('seq') or deleting then
insert into dest(mod,out,sequence,length)
values('d','y',:old.seq,:old.len);
end if;
if updating('len') then
insert into dest(mod,out,sequence,length)
values('u','n',:new.seq,:new.len);
end if;
end;
Make your choice.
HTH.
|
|
|
|
Goto Forum:
Current Time: Thu Feb 13 03:40:21 CST 2025
|