当前位置: 代码迷 >> Oracle开发 >> 触发器select 有关问题
  详细解决方案

触发器select 有关问题

热度:42   发布时间:2016-04-24 06:35:12.0
触发器select 问题
我有表 table1 和 table2  两个表结构是 一样的   字段 id和name, 我需求是 给 table2  建立触发器  执行 insert  语句时 判断  将要插入的数据是否在  table2 里有  如果有 就将数据插入到  table1 中。

这是我写的代码:
  create or replace trigger table2_insert
    after insert on table2 
    for each row
declare 
   v_count  := 0;
 begin 
   select count(*)  into v_count from  table2 where  nvl(id,'1')=nvl(:new.id,'1') and nvl(name,'1')=nvl(:new.name,'1') ;
  if  v_count  > 0 then
  insert into table1 values(:new.id,:new.name);
 end if;
end;

在我执行
insert into  table2 values('1','www');
错误:
ora-04091 表system.table2 发生变化,触发器/函数不能读它。

我尝试去除 
select count(*)  into v_count from  table2 where  nvl(id,'1')=nvl(:new.id,'1') and nvl(name,'1')=nvl(:new.name,'1') ; 这句
能正常插入。

求大牛指点,我刚学oracle不久

------解决思路----------------------
after TR 没有  :new 。 建议 LZ 查查 :new 和 :old 与 insert , update , delete 的对应关系。
------解决思路----------------------
将after 改成before  另外再创建个临时表,当插入到临时表的数据在table2里有的时候就将数据插入到table1,否则插入到table2里 
  相关解决方案