在PL / SQL中的FORALL循环中插入

时间:2016-09-13 08:31:18

标签: performance plsql forall

是否可以在pl / sql中使用FORALL进行批量插入?

TYPE c_type1 IS RECORD
(
column1 table1.column1%TYPE,
column2 table1.column2%TYPE,
client table2.client%TYPE
);
type1 c_type1;

CURSOR cur_t IS  select * BULK COLLECT INTO recs from table3 ;

begin
  FOR recs IN cur_t
  LOOP

  SELECT * INTO type1 FROM  (select a.column1, a.column2,imm.client 
  ...
    from table1 a, table2 imm        
   WHERE 
    a.column1 = recs.column1
  ) WHERE ROWNUM=1;

  INSERT INTO table2 values (recs.column1,type1.column2);

  ...

P.S:要插入更多80列。

1 个答案:

答案 0 :(得分:0)

您的问题不是很明确,但查看您的代码我有以下内容。检查这是否是您要找的。

declare

CURSOR cur_t IS  
                select t3.column1 , t1.column2
                from table3 t3
                inner join table1 t1
                on t3.column1 = t1.column1;

type var_cur is table of cur_t%rowtype;

var var_cur;

begin

open cur_t;

  LOOP

    FETCH cur_t bulk collect into var limit 100;       
     EXIT WHEN cur_t%NOTFOUND;

     FORALL i IN 1 .. var.count SAVE EXCEPTIONS
      INSERT INTO TABLE2 
     VALUES var(i);         

    END LOOP;

  CLOSE distinctUserIdCursor;

COMMIT;

EXCEPTION
    WHEN OTHERS THEN
        dbms_output.put_line('Error in Insertion of record' || '~~~~' || SQLERRM);
      FOR indx IN 1 .. SQL%BULK_EXCEPTIONS.COUNT
         LOOP
            DBMS_OUTPUT.put_line (SQL%BULK_EXCEPTIONS (indx).ERROR_INDEX|| ': '
               || SQL%BULK_EXCEPTIONS (indx).ERROR_CODE);
      END LOOP;

end;