使用bulk collect limit处理大量数据

Posted by Vincent on September 17, 2008

很多时候需要出来大量的数据,这个时候分批获取和提交对减少系统的负载是非常有帮助的!

例子:

create table t
as
select * from dba_objects;
------------------------------

alter table T add constraint PK_T primary key (OBJECT_ID);
------------------------------

create table t1 (names varchar2(50));
----------------------------------

对应的存储过程
[cc lang="sql"]
/*
creator:玄风
date:2008-9-17
function:分批处理大量数据例子
*/
create or replace procedure SP_TEST_BULK(id_begin in number,
id_end in number) is

v_cnt number;

type rec_ids is table of varchar2(50);
v_ids rec_ids;

cursor c1 is
select object_name
from t
where object_id >= id_begin
and object_id <= id_end;

begin
open c1;
loop
fetch c1 bulk collect
into v_ids limit 5000;

v_cnt := v_ids.count;

for i in 1 .. v_cnt loop
insert into t1 (names) values (v_ids(i));

if mod(i, 10) = 0 then
commit;
end if;
end loop;

exit when c1%notfound;
end loop;

commit;
close c1;

end SP_TEST_BULK;
[/cc]
--EOF--


This work is licensed under a CC A-S 4.0 International License.