create or replace trigger production_new_trigger
    before insert on table_a
    for each row
declare
  cursor cursor_table_b is select t.a,t.b,t.c from table_b t where t.c = 123;
  cur_table_b_type cursor_table_b%rowtype;
 begin
   open  cursor_table_b;
   -- 这个地方开始出问题.用while loop来循环的时候,一行都没插入.
   --但因为用loop循环的话最后会多出一行重复记录,这是loop先执行后判断照成的,所以不能用loop.
   --loop
   while cursor_table_b%found loop   
   fetch cursor_table_b into cur_table_b_type;
   insert into table_c values (cur_table_b_type.a,cur_table_b_type.b,cur_table_b_type.c)
   end loop;
   --exit when  cursor_table_b%notfound;
   close cursor_table_b;
   end;
--各位兄弟有知道原因的不吝赐教. 

解决方案 »

  1.   

    insert into table_c values (cur_table_b_type.a,cur_table_b_type.b,cur_table_b_type.c)
    这句后面有个;  手误没打上.
      

  2.   

    exit when  cursor_table_b%notfound;放到insert 前面
    create or replace trigger production_new_trigger
        before insert on table_a
        for each row
    declare
      cursor cursor_table_b is select t.a,t.b,t.c from table_b t where t.c = 123;
      cur_table_b_type cursor_table_b%rowtype;
     begin
       open  cursor_table_b;
       loop   
       fetch cursor_table_b into cur_table_b_type;
       exit when  cursor_table_b%notfound;
       insert into table_c values (cur_table_b_type.a,cur_table_b_type.b,cur_table_b_type.c)
       end loop;
       close cursor_table_b;
       end;
      

  3.   

       while cursor_table_b%found loop   
       fetch cursor_table_b into cur_table_b_type;
       insert into table_c values (cur_table_b_type.a,cur_table_b_type.b,cur_table_b_type.c)
       end loop;
       --exit when  cursor_table_b%notfound;
       close cursor_table_b;你还没fetch就用%found来判断了
    改成
       loop
          fetch cursor_table_b into cur_table_b_type;
          exit when  cursor_table_b%notfound;
          insert into table_c values (cur_table_b_type.a,cur_table_b_type.b,cur_table_b_type.c)
       end loop;
       close cursor_table_b;