create or replace trigger pay_result_update
before update on pay_result 
for each row
declare
num number;
begin
 select conut(1) into num from pay_his where where batch=:new.batch and payyear=:new.payyear 
 and paymonth=:new.paymonth and p_id=:new.p_id 
 and payitem=:new.payitem;
 if num then
 update pay_his set cost_ceneter='nothing'
 where batch=:new.batch and payyear=:new.payyear 
 and paymonth=:new.paymonth and p_id=:new.p_id 
 and payitem=:new.payitem;
 else
 insert into pay_his values(:new.col_name,.....);
 end if;
end;
/

解决方案 »

  1.   

    declare
    t_temp number;
    select count(1) into number from pay_his where batch=:old.batch and payyear=:old.payyear 
     and paymonth=:old.paymonth and p_id=:old.p_id 
     and payitem=:old.payitem;
    if number>0 then
    update ....
    else
    insert into ....
    end if;
    end;
      

  2.   

    谢谢楼上的两位,你们的方法是可以行的。
    不过你们没有明白我的意思。
    我是说从 update 的语句中应该有个返回值表明你 update 了那一行。
    所以我可以直接通过知道 update 失败来知道table中没有这个数据。
    而不用通过 select 预先查寻。
    不是说这个提议好,但是我想知道该如何做,如何去得到那个update后的row.
    谢谢你们。
    请再发言。
      

  3.   

    在update之后,通过下面的语句判断
    if SQL%ROWCOUNT = 0 then
       insert into ....
    end if;
      

  4.   

    用全局变量sql%rowcount查看本次操作影响了多少行,就知道该如何操作了。
      

  5.   

    ....
    begin
    update ....;
    if sql%notfound then
    insert into ....;
    end if;
    end;
    /
      

  6.   

    谢谢大家,Lastdrop(空杯) 完全正确。
    给分。结贴。