Oracle dml trigger 语法概括
create or replace trigger trigger_name
before/after insert/update/delete [of column] on [schema.]table_name
[for each row [when(条件)]]
begin
select id from :new;
select id from :old;
update table set column = value where id=:old.tableid;
end trigger_name;
其中,for each row 是行级触发器,如果不加就是语句级触发器,在语句级触发器中不能使用:new和:old对象
:new 只在insert、update 时有效
:old 只在update、delete时有效
在语句中可以用 “if inserting then 语句 end if;” 判断(deleting、updating)
原则上行级的触发器代码中不能操作该表,包括select
为了在语句级触发器中访问新插入后修改后的记录,可以增加行级触发器,将更新的记录插入临时表中,然后在语句级触发器中扫描临时表,获得修改后的记录。临时表的表结构一般与关联表的结构一致。
在行级触发器中如果使用count、sum等函数会报ORA-04091错误,此时需要加 declare 语句:declare pragma autonomous_transaction; 遇到另一个问题 ORA-06519,解决办法在dml语句后边加commit
自己编写之后发现这种方式有个问题,当时更新的那条记录并不会在after 行触发器中读取到,after行触发器中能获取到的是旧数据
注:最后end后不能忘了加 ";"
更多详细介绍可参考:https://www.cnblogs.com/huyong/archive/2011/04/27/2030466.html#!comments

浙公网安备 33010602011771号