行级的触发器代码中不能操作该表,包括select,所以报错!

当然解决方法就是要根据原因了,正因为限定了行级触发器的操作,只能选择表级的触发器了,但是在表级的触发器又不能获得:new和:old的值,那就只能采取两种触发器并用的方法了,并且还要包或者临时表加以辅助.

首先在行级触发器中将所需的,:new或者:old的值,写到包或者临时表中

然后在表级触发器中处理包或者临时表中已经写入的数据,操作成功后可以按照需求再删除临时表的数据.

下面是一个实例:

-- 创建一个临时表
 

  1. create global temporary table TEMP_TEMP  
  2. (  
  3. NOTICEID NUMBER(10),  
  4. VERSION NUMBER(10)  
  5. )  
  6. on commit preserve rows;  

--存储过程
 

  1. create or replace trigger savetempvalue  
  2. after insert or delete on notice_comment_data   
  3. for each row  
  4. begin 
  5. insert into temp_temp values(:new.noticeId,:new.version);  
  6. end savetempvalue;  

--主存储过程
 

  1. create or replace trigger UpdateReplyCount  
  2. after insert or delete on notice_comment_data   
  3.  
  4. declare 
  5. t_noticeId notice_comment_data.noticeid%type;  
  6. t_version notice_comment_data.version%type;   
  7. begin 
  8. select noticeId,version into t_noticeId,t_version from temp_temp;  
  9. update notice_data   
  10. set replyCount=(select count(*) from notice_comment_data   
  11. where notice_comment_data.noticeid=t_noticeId and version=t_version )   
  12. where notice_data.noticeid=t_noticeId   
  13. AND notice_data.version=t_version;  
  14. end UpdateReplyCount;