2012年6月8日 星期五

BULK_COLLECT 與 FORALL


大量資料時的 select, insert, update, 適時使用 BULK_COLLECT 與 FORALL 可增加程式效能

DECLARE
   TYPE rec_type IS TABLE OF emp%rowtype INDEX BY PLS_INTEGER;
   rec        rec_type         ;
BEGIN

   select em.*
     bulk collect
     into rec
     from emp  em
    order by 1;

   forall i in 1 .. rec.count
   save exceptions
   insert into emp_temp2
   values (9999, sysdate, rec(i).empno, rec(i).ename, rec(i).sal);

EXCEPTION
  when others then
    dbms_output.put_line('There are '|| sql%bulk_exceptions.count ||' exceptions');
    for m in 1 .. sql%bulk_exceptions.count loop
        dbms_output.put_line(SQL%bulk_exceptions(m).error_index || ' -- ' ||
                             m || ' -- ' ||
                             SQL%bulk_exceptions(m).error_code || ' -- ' ||
                             SQLERRM(-SQL%bulk_exceptions(m).error_code)  );
    end loop;
END;
/

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。