现在有一张表Table1,其中包含字段:产品编号ID、日期D01、日产量D02。
另一张表Table2,其中包含字段:产品编号ID、月份D03、月产量D04。
现在需要编写一个触发器,当Table1执行INSERT操作后,在Table1中查找日期中月份的值等于Table2的月份,将这些记录中字段日产量D02值求和后,更新到表Table2中月产量D04中。只要表Table1中日产量一更新,表Table2中月产量马上更新。

解决方案 »

  1.   

    补充:。。在Table1中查找日期中月份的值等于Table2的月份(和产品ID等于Table2中的产品ID),将这些记录中字段日产量D02值求和后。。
      

  2.   

    自己先试着写一个!挺容易的update table2 set D04=D04+:new.D02
    where id=:new.id and to_char(D03,'yyyymm')= to_char(:new.d02,'yyyymm');
      

  3.   

    是否存在table2没有 当月记录创建月份记录的问题
      

  4.   

    我写的是这样的,本来下面的过程是要放在触发器里面的,但是修改表数据的时候通不过(不是高手啊),后来找分开来了,其实也可以用一个包来做的:
    --drop  table test1;
    create table TEST1
    (
      NUM_COL  NUMBER,
      CHAR_COL VARCHAR2(60)
    );--drop table test2;
    create table TEST2
    (
      NUM_COL  NUMBER,
      CHAR_COL VARCHAR2(60)
    );
    --drop procedure uptest2
    create or replace procedure uptest2 as
    v_num number(10);
    v_char varchar2(20);
    cursor gettest1 is select sum(num_col),char_col  from test1 group  by char_col;
    begin
    open gettest1;
    loop
    fetch gettest1 into v_num,v_char;
    update test2 set num_col=v_num where char_col=v_char;
    commit;
    exit when gettest1%notfound;
    end loop;
    close gettest1;
    end;
    /--drop trigger updatetest2;
    create or replace trigger updatetest2
    after insert or update of num_col  on test1 for each row
    declare 
    v_str varchar2(200):='execute uptest2';
    begin
    execute immediate v_str;
    commit;
    end;
    /