带有条件的触发器trigger

再建好sequences 后,对oracle中的自增序列进行插入容易出现问题,

比如:用户如果在进行数据插入时指定了 CHATICON_SEQ.NEXTVAL 为自增ID,那么写好的触发器也会自动增加序列的,这样插入一条记录时,sequence就增加2个。为此需要在trigger中设置条件,设定当用户插入时id的值为空,才让触发器生效,这样可以避免序列重复增加。

以下是例子:

create or replace trigger trg_CHATICON
  before insert on CHATICON
  for each row
declare
nextid number;

begin
if :new.id is null then
  select CHATICON_SEQ.NEXTVAL into nextid FROM dual;
 :new.id := nextid;
end if;
end;

related articles



Leave a Reply

You must be logged in to post a comment.