CREATE OR REPLACE TRIGGER MiscellaneousCost
after insert or update on EHU_AWBCHG
for each row
declare
PRAGMA AUTONOMOUS_TRANSACTION;
begin
if (:new.LAMT <> 0 and :new.CHG_CD <> 'FRT') then
update EHU_AWB t1
set t1.EXPORT_OTH =
(select sum(t2.LAMT)
from EHU_AWBCHG t2
where t2.AWB_NO = t1.AWB_NO
and t2.CHG_CD <> 'FRT')
where t1.AWB_NO = :new.AWB_NO;
end if;
commit;
end MiscellaneousCost;
上面的代码是如果不使用事务会出现写oracle行级触发器时,不能操作本表,报"表 *** 发生了变化,触发器/函数不能读"的错误法,改用了自治事务,但是为什么每次update资料的时候,得到的结果都不正确,有时候少一步,这个要怎么解决。
...
end if;
commit;
exception --发生异常时,回滚提交,这样能保持事物的原子性
when others then
rollback;
end MiscellaneousCost;
--使用自治事物要注意,当你的操作失败后,涉及到此次操作的一些子过程是能够独立完成的
--并且他们不会因为触发器的失败而回滚