我在写一个触发器的时候,就是当A表的一个字段更新时,B表的相应字段也更新,但老是报错,是不是直接这样写不行?我考虑了一下,可能还有这种情况,就是当A表的一个字段更新时,B表也要存在相应的字段才行,那这一样一来的话,是写存储过程方便还是触发器方便?如果可以最好能写个示例,谢谢!

解决方案 »

  1.   

    --你说对了的,更新A表中是a1字段,同时更新对应B表中的b1字段,B表与A表的链接条件是 A.id=B.id
    --如果A表的id在B表不存在,那么就更新不到B表create or replace trigger trigger_name
    after update on A
    for each row
    begin
       update B set b1=:new.a1 where id=:new.id;
    end;
      

  2.   

    这个需要用到事务自处理机制,具体的你去查查,个人觉得是trigger比较好
      

  3.   


    就是当A表的一个字段更新时,B表的相应字段也更新  要有对应的连接字段才行
    create or replace trigger up_tbb before update on tba for each row
    as
    begin
    update tbb set col1=:new.col1 where col:=:new.col;
    end;
      

  4.   

    那可不可在更新前以加入判断呢?像if (id(主键)= select * from  B where  b.id=a.id)!=null
    这种类似判断这条记录存不存在的做法呢?
      

  5.   

    create or replace trigger up_tbb before update on tba for each row
    declare
    v_count number;
    begin
    select count(1) into v_count from tbb where col=:new.col;
    if v_count>0 then
    update tbb set col1=:new.col1 where col:=:new.col;
    end if;
    end;
      

  6.   

    那我现在需求变态一点
      if v_count>0 then 
     就把查到的这条记录插入到另一张表里,你看怎么写比较简洁一点?
      

  7.   

    create or replace trigger up_tbb before update on tba for each row
    declare
    v_count number;
    begin
    select count(1) into v_count from tbb where col=:new.col;
    if v_count>0 then
    insert into tbb values(:new.col1);
    end if;
    end;
      

  8.   

    create or replace trigger tri_name 
    update on tb for each row
    declare
    v_count number;
    begin select count(1) into from tb where col=:good.col
    if v_count>0
    insert into tb values(good.col);
    end if;
    end;
    /