带有条件的触发器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
- oralcle job的应用实例 (11月 7th, 2007)
- rhel5下安装oracle10g笔记 (11月 5th, 2007)
- java 、 oracle中日期字段的处理 (9月 14th, 2007)
- oracle 建表 sequence 及 trigger的创建过程 (2月 20th, 2007)