- 自增长
create or replace trigger [TABLE_NAME] before insert
on [TABLE_NAME] for each row
declare
integrity_error exception;
errno integer;
errmsg char(200);
dummy integer;
found boolean;
begin
select [SEQ_NAME].NEXTVAL INTO :new.id from dual;
exception
when integrity_error then
raise_application_error(errno, errmsg);
end;
- 空值自增长
create or replace trigger [TRI_NAME] before insert
on [TABLE_NAME] for each row
declare
integrity_error exception;
errno integer;
errmsg char(200);
dummy integer;
found boolean;
begin
-- Column Id uses sequence Seq_Shp_Project
if :new.Id is null or :new.ID = 0 then
select [SEQ_NAME].NEXTVAL INTO :new.Id from dual;
end if;
if :new.type = 1 then
insert into [TABLE_NAME] ([Column_NAME],[Column_NAME],[Column_NAME]) values(:new.stid,'jzbg',:new.Id);
end if;
-- Errors handling
exception
when integrity_error then
raise_application_error(errno, errmsg);
end;
:new 插入对象
定义:在行级触发器中,为了获取某列在DML操作前后的数据,Oracle提供了两种特殊的标识符——:OLD和:NEW,通过:OLD.column_name的形式可以获取该列的旧数据,而通过:NEW.column_name则可以获取该列的新数据。
使用场景:
insert触发器只能使用:NEW.column_name;
delete触发器只能使用::OLD.column_name;
update触发器则两种都可以使用。
:NEW.column_name通常使用于before insert or update;
:OLD.column_name通常使用于after delete or update;
提示:如果在创建DML触发器时不使用 for each row 子句,则表示创建的是语句级触发器,语句级触发器对所有受影响的数据行只触发一次,因此无法使用:NEW与:OLD标识符获取某列的新旧数据。
For each row 理解为受影响的行都触发一次
没有for each row 即使n行受影响也只触发一次