大表数据插入批量提交

来源:互联网 发布:网络歌手麦子 编辑:程序博客网 时间:2024/04/26 21:25

对大表进行插入时,数据非常多,会把undo表空间撑爆,导致插入不成功,可以使用批量提交进行插入,及时释放回滚段。


declare
  --定义cursor把数据全部读入内存中
  cursor cur is
    select nsrzhdah, sfzjlx_dm, sfzjhm,       xm,            nsrsbh,
             zlsfqq,      lydm,       yxbz,          bdbz,          hcode,
             fkid ,        fxbz,        zrrlx_dm ,   xybz ,         lrrq ,
             lrr_dm,     xgrq ,       xgr_dm ,    nsrswjg_dm,wspzxh ,
             ywxh ,     bdlx_dm , czlx_dm ,   sjlyxt ,         sjjzrq ,
             sjscrq ,     sjbgbz ,    sjtjrq ,         sjgsdq
      from j1_g3_zbq.INFT02_JBXX_ZRR;
  --自定义集合类型
  type rec is table of cur%rowtype;
  recs rec;
begin
  execute immediate 'alter table j1_ldm.ldmt02_jbxx_zrr nologging';
  open cur;
  while (true) loop
    fetch cur bulk collect
      into recs limit 10000;--10000行执行一次游标读取操作
    --把数据逐条插入
    forall i in 1 .. recs.count
      insert into j1_ldm.ldmt02_jbxx_zrr values recs (i);
    commit;
    exit when cur%notfound;
  end loop;
  close cur;
  execute immediate 'alter table j1_ldm.ldmt02_jbxx_zrr logging';
end;
/


open xxxx for select .....; 打开游标
    loop
      fetch xxxx bulk collect
        into v1,v2,v3..... limit 1000; --限制每次取1000行
      n_count := v1.count;  --获取本次FETCH得到的行数
      exit when n_count = 0;--等于0则退出循环
     forall i in v1.first .. v1.last  
      insert into ... values (v1(i),v2(i),.....);   --批量插入
      commit;   一次提交
           
    end loop;
    close XXXX; --关闭游标


declare
  2    cursor c_t is select * from t;
  3    type typ_t is table of c_t%rowtype
  4    index by binary_integer;  
  5    v_type_t  typ_t;
  6    v_row  pls_integer;
  7  begin
  8    open c_t;
  9    loop
 10    fetch c_t bulk collect into v_type_t
 11        limit 100;
 12    exit when v_type_t.count = 0;
 13    dbms_output.put_line(v_type_t.count);
 14    v_row :=v_type_t.first;
 15    while(v_row is not null)
 16      loop
 17  --   dbms_output.put_line(v_type_t(v_row).empno);
 18       null;
 19       v_row :=v_type_t.next(v_row);
 20     end loop;
 21    end loop;
 22    close c_t;
 23  end;
 24  /


0 0
原创粉丝点击