bulk collect批量定期插入删除行(续)

本文介绍如何使用PL/SQL批量删除及插入数据,并通过游标和索引表进行高效处理,适用于大规模数据更新场景。
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;
/



在Oracle中,可以使用数组结合`BULK COLLECT`和`FORALL`语句来实现批量插入。以下是一个示例,结合了引用中的部分概念: ### 示例代码 ```sql -- 定义一个记录类型,用于存储要插入的数据 DECLARE -- 定义记录类型 TYPE t_record_insert IS RECORD( country_iso_code countries.country_iso_code%type, country_name countries.country_name%type ); -- 定义数组类型,该数组存放上述记录类型 TYPE t_insert_array IS TABLE OF t_record_insert INDEX BY BINARY_INTEGER; v_insert_array t_insert_array; BEGIN -- 从源表中批量收集数据到数组中 SELECT country_iso_code, country_name BULK COLLECT INTO v_insert_array FROM countries WHERE rownum < 4; -- 使用 FORALL 进批量插入 FORALL i IN 1..v_insert_array.COUNT INSERT INTO another_countries (country_iso_code, country_name) VALUES (v_insert_array(i).country_iso_code, v_insert_array(i).country_name); -- 提交事务 COMMIT; END; ``` ### 代码解释 1. **定义记录类型**:`t_record_insert`用于存储每一插入的数据,其字段类型与源表`countries`和目标表`another_countries`的字段类型匹配。 2. **定义数组类型**:`t_insert_array`是一个索引表,用于存放`t_record_insert`类型的记录。 3. **批量收集数据**:使用`BULK COLLECT`将源表`countries`中满足条件(`rownum < 4`)的数据收集到数组`v_insert_array`中。 4. **批量插入数据**:使用`FORALL`语句将数组中的数据批量插入到目标表`another_countries`中。 5. **提交事务**:使用`COMMIT`语句提交插入操作。 ### 注意事项 在使用这种方法时,要保证源表和目标表的字段类型和数量匹配。同时,设计并执SQL语句前,应该全面、仔细地考虑各种特殊情况,并在程序运中仔细进测试和监测,以保证程序稳定、高效、可靠[^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值