Oracle:无法更改此触发器类的New值
今天做ERP维护和管理工作中遇到一个问题,当一个值发生变化时,指定内容的值需要发生变化,
即
1、若该值由0变成1时,系统不做任何改变;
2、若该值由1变成0时,系统根据实际需求,删除或清空该类的小项内容。
如图:
结合该需求,笔者了解了触发器的原理和用途,选择运用触发器原理来解决这一需求。
一开始,笔者未了解清楚:new的使用范畴,因此在书写触发器时将其写在after之后执行,编译时报出了“无法更改此触发器类的New值”异常,寻思未解,于是开始寻找原因。之后才知道After时:new值只有读的权限,没有写的权限,而Before时:new有读写权限,因此将After改为Before,编译成功!
SQL如下:
修改前:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
create or replace trigger tri_app1_mstr_upd after update on app1_mstr for each row begin if updating then if :new.app_hardware = 0 and :old.app_hardware = 1 then :new.app_1_dept := ''; :new.app_1_mgr1 := ''; :new.app_1_summary := ''; :new.app_1_engineer1 := ''; :new.app_1_engineer2 := ''; :new.app_1_engineer3 := ''; delete from appd6_det where appd6_flow = :old.app_flow; end if; end if; end tri_app1_mstr_upd;
修改后:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
create or replace trigger tri_app1_mstr_upd before update on app1_mstr for each row begin if updating then if :new.app_hardware = 0 and :old.app_hardware = 1 then :new.app_1_dept := ''; :new.app_1_mgr1 := ''; :new.app_1_summary := ''; :new.app_1_engineer1 := ''; :new.app_1_engineer2 := ''; :new.app_1_engineer3 := ''; delete from appd6_det where appd6_flow = :old.app_flow; end if; if :new.app_structure = 0 and :old.app_structure = 1 then :new.app_2_dept :=''; :new.app_2_mgr1 :=''; :new.app_2_summary :=''; :new.app_2_engineer1 :=''; :new.app_2_engineer2 :=''; :new.app_2_engineer3 :=''; delete from appd7_det where appd7_flow = :old.app_flow; end if; if :new.app_software = 0 and :old.app_software = :old.app_flow then :new.app_3_dept :=''; :new.app_3_mgr1 :=''; :new.app_3_summary :=''; :new.app_3_engineer1 :=''; :new.app_3_engineer2 :=''; :new.app_3_engineer3 :=''; delete from appd8_det where appd8_flow = :old.app_flow; end if; if :new.app_other = 0 and :old.app_other = 1 then :new.app_4_dept :=''; :new.app_4_mgr1 :=''; :new.app_4_summary :=''; :new.app_4_engineer1 :=''; :new.app_4_engineer2 :=''; :new.app_4_engineer3 :=''; delete from appd9_det where appd9_flow = :old.app_flow; end if; if :new.app_module = 0 and :old.app_module = 1 then :new.app_5_dept :=''; :new.app_5_mgr1 :=''; :new.app_5_summary :=''; :new.app_5_engineer1 :=''; :new.app_5_engineer2 :=''; :new.app_5_engineer3 :=''; delete from appd12_det where appd12_flow = :old.app_flow; end if; end if; end tri_app1_mstr_upd;
与你共亲到无可亲密时,便知友谊万岁是尽头。