我现在有这样的一个需求:
  我们有三张表A,B,C。其中A,B,两个表主键相同,也是C表的参考表。当初设计时候,当做一个个表处理,会造成字段太多,所以就拆开了做两个表。
  现在想实现这样的一个功能:
  我在向C表插入一条记录的时候,同时向D表插入一条记录。D表的数据来源于A,B,C表的部分字段。
  我的实现如下:create or replace trigger TR_C_insert
  before insert on C  
  for each row
declare
--local variables here
ARGS_a1 NVARCHAR2(30);
ARGS_a2 NVARCHAR2(30);
ARGS_a3 RAW(16);
ARGS_b1 NVARCHAR2(30);
ARGS_b2NVARCHAR2(30);
ARGS_b3 NVARCHAR2(30);begin
  begin 
    select a1 into ARGS_a1
    from a 
    where a.id = :new.ref;
  exception when others then 
    ARGS_a1:=null;
  end;begin 
    select a2 into ARGS_a2
    from a 
    where a.id = :new.ref;
  exception when others then 
    ARGS_a2:=null;
  end;
 ------接下来还需要写很多这样的句子 
  INSERT INTO D values(:new.id
                            ,ARGS_a1
                            ,ARGS_a2
                            ,ARGS_a3
                            ,ARGS_a4
                            ,ARGS_b1
                            ,ARGS_b2
                           
  );
  
end ;如果我D表的字段很多,那我这个trigger将会写的很长,有没有好的实现方法。谢谢!
  

解决方案 »

  1.   

    TRIGGER中的代码最好不要太长,如果非要太长可以通过调用存储过程来实现~
      

  2.   

    我那里面只用了一些关于查询其他表的字段赋值的语句。只是这样的select xx into varable 过多。所以看看有没有其他的方法。
      

  3.   


    begin
      begin 
        select a1 into ARGS_a1
        from a 
        where a.id = :new.ref;
      exception when others then 
        ARGS_a1:=null;
      end;begin 
        select a2 into ARGS_a2
        from a 
        where a.id = :new.ref;
      exception when others then 
        ARGS_a2:=null;
      end;
    你的这两句可以合成一句呀--那就简单了~~~
     select a1,a2 into ARGS_a1,ARGS_a2
        from a 
        where a.id = :new.ref;
      

  4.   

    我以前一直尝试这样去写:
    select xx into args ,xx1 into args1 from xx ;报错
    原来是你这样写的谢谢了啊!