bulk collect批量定期插入删除行(续)
  k8ZcpAZ2WGbd 2023年11月12日 31 0


create table t1 as select * from dba_objects where object_id is not null; 
create table t2 as select object_id from t1 where rownum<75201;
create index idx_t1_object_id on t1(object_id);
根据T2的字段去删除T表的内容,并定期按行数提交更新
declare
v_count number;
cursor c1 is select  object_id from t2;                 --定义一游标
type t_c1 is table of number index by binary_integer;   --定义存储数据类型为NUMBER,下标为整数的索引表
i_c1 t_c1;
begin
  v_count:=0;
open c1;
 loop
  fetch c1 bulk collect into i_c1 limit 500;            --每次fetch500行,根据资源情况分配
forall idx_c1 in 1..i_c1.count
   delete from t1 where object_id=i_c1(idx_c1);         --批量绑定
v_count:=v_count+1;
   if v_count=2 then                                    --达到1000行提交一次
    commit;
    v_count:=0;
   end if;
   exit when c1%NOTFOUND;                               --遍历完成退出,避免死循环!
 end loop;
 commit;                                                --剩余的提交
close c1;
exception                                               --异常处理
when others then
rollback;
end;
/

-------------------------------------------------------------------------

t1 t2
truncate table t1;
create table t1 as select * from dba_objects where object_id is not null; 
create table t2 as select * from dba_objects where object_id is not null;
将T2表的数据批量插入到T1表并定期按行数更新;
declare
v_count number;
cursor c1 is select  rowid from t2;
type t_c1 is table of rowid index by binary_integer;                    --定义索引表
i_c1 t_c1;
begin
  v_count:=0;
open c1;
 loop
  fetch c1 bulk collect into i_c1 limit 500;
forall idx_c1 in 1..i_c1.count
   insert into t1 select * from t2 where rowid=i_c1(idx_c1);            --插入整行数据
v_count:=v_count+1;
   if v_count=2 then
    commit;
    v_count:=0;
   end if;
   exit when c1%NOTFOUND;
 end loop;
 commit;
close c1;
exception 
when others then
rollback;
end;
/




【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月12日 0

暂无评论

推荐阅读
k8ZcpAZ2WGbd