表1 Table1
结构SID    部件1T1    部件2T2    部件3T3
A型         1           3          5
B型         5           5          9
C型         5           7          11表2 Table2
部件类别TID     特征值   参量 .....
1            .....    .....
3            .....    .....
5            .....    .....
7            .....    .....
9            .....    .....
11            .....    .....当我从部件类别中删除一行数据的时候,同时需要将表1中的对应值更新为0,例如当删除表2第5行的时候。结果应当为:A型      1        3         0
B型      0        0         9
C型      0        7         11请问这个语句应当怎么写啊?sqlsql语句

解决方案 »

  1.   

    create table table1
    (
    SID varchar2(20),
    t1 number,
    t2 number,
    t3 number
    );
    create table table2
    (
    TID number
    );insert into table1
    values('A',1,3,5);insert into table1
    values('B',5,5,9);insert into table1
    values('C',5,7,11);select * from table1;
    insert into table2
    values(1);
    insert into table2
    values(3);
    insert into table2
    values(5);
    insert into table2
    values(7);
    insert into table2
    values(9);
    insert into table2
    values(11);CREATE OR REPLACE TRIGGER del_on_table2 before
      DELETE ON table2 FOR EACH row DECLARE v_tid NUMBER;
      BEGIN
        v_tid:= :OLD.TID;
        UPDATE table1
        SET t1= DECODE(t1, v_tid ,0,t1),
          t2  = DECODE(t2,v_tid,0,t2),
          t3  = DECODE(t3, v_tid ,0,t3);
      END;
      

  2.   

    +1,写个触发器解决。直接用sql的话可以调用yinan9 的触发器里面的sql,当然参数需要自己传递、