create or replace trigger sy befor insert on 表4 for each row
begin
 select classNeme into :new.className1 from 表2 
  where classid = 
   (select classid from 表3 
      where addressid = :new.addressid and rownum <= 1);  select classNeme into :new.className2 from 表2 
   where classid = 
   (select classid from 表3 
      where addressid = :new.addressid and rownum <= 1 
         and classid <> :new.className1);  EXCEPTION
   WHEN OTHERS THEN
   null;
 end;

解决方案 »

  1.   

    create or replace trigger sy 
    before insert on 表4 
    for each row
    begin
    select ClassName into 
    select (select ClassName from 表2 b where b.ClassID=C.ClassID) INTO :new.ClassName1 from 表3 c where  AddressID=:new.AddressID;
    end;
    /
      

  2.   

    请问楼主,建表3的时候根据什么条件?也就是AddressID和ClassID如何对应?
      

  3.   

    注意,你无法在一个表里插入记录时,再通过后台的TRIGGER来触发修改这个表的字段。因此,你只能在插入记录时,在前台同时完成相关字段的赋值。你可以在pre insert 事件完成下面的代码。
    good luck!DECLARE
        n_temp number;
        class_name1  YourType;
        class_name2  YourType;
        CURSOR class_name IS
    SELECT a.ClassName FROM 表2 a,表3 b
    WHERE  a.ClassId=b.ClassId  and b.AddressId=:new.AddressId;
    begin
    if inserting then
    n_temp:=1
    for RC in class_name loop
    if n_temp=3 then
    exit;
    end if;
    if n_temp=1 then
    class_name1:=RC.ClassName;
    end if;
    if n_temp=2 then
    class_name2:=RC.ClassName;
    end if; n_temp:=n_temp+1;
    end loop;
    ClassName1:=class_name1;
    ClassName2:=class_name2;
    end if;
    end;