采用bulk collect可以将查询结果一次性地加载到collections中,而不是通过cursor一条一条地处理。
可以在select into,fetch into,returning into语句使用bulk collect。
注意:在使用bulk collect时,所有的into变量都必须是collections
create table t_test as
select object_id, object_name, object_type
from dba_objects
where wner = 'TEST';
1、在select into语句中使用bulk collect
declare
type object_list is table of t_test.object_name%type;
objs object_list;
begin
select object_name bulk collect
into objs
from t_test
where rownum <= 100;
for r in objs.first .. objs.last loop
dbms_output.put_line(' objs(r)=' || objs(r));
end loop;
end;
/
2、在fetch into中使用bulk collect
declare
type objecttab is table of t_test%rowtype;
objs objecttab;
cursor cob is
select object_id, object_name, object_type
from t_test
where rownum <= 10;
begin
open cob;
fetch cob bulk collect
into objs;
close cob;
for r in objs.first .. objs.last loop
dbms_output.put_line(' objs(r)=' || objs(r).object_name);
end loop;
end;
/
以上为把结果集一次fetch到collect中,我们还可以通过limit参数,来分批fetch数据,如下:
declare
type objecttab is table of t_test%rowtype;
objs objecttab;
cursor cob is
select object_id, object_name, object_type
from t_test
where rownum <= 10000;
begin
open cob;
loop
fetch cob bulk collect
into objs limit 1000;
exit when cob%notfound;
dbms_output.put_line('count:' || objs.count || ' first:' || objs.first ||
' last:' || objs.last);
for r in objs.first .. objs.last loop
dbms_output.put_line(' objs(r)=' || objs(r).object_name);
end loop;
end loop;
close cob;
end;
/
你可以根据实际来调整limit参数的大小,来达到最优的性能。limit参数会影响到PGA的使用率。
3、在returning into中使用bulk collect
declare
type id_list is table of t_test.object_id%type;
ids id_list;
type name_list is table of t_test.object_name%type;
names name_list;
begin
delete from t_test
where object_id <= 87510 returning object_id, object_name bulk collect into ids,
names;
dbms_output.put_line('deleted ' || sql%rowcount || ' rows:');
for i in ids.first .. ids.last loop
dbms_output.put_line('object #' || ids(i) || ': ' || names(i));
end loop;
end;
4.用于动态语句
declare
v_query_sql varchar2(500);
type type_emp_table is table of emp%rowtype index by binary_integer;
v_emp_table type_emp_table;
begin
v_query_sql := 'select * from emp';
execute immediate v_query_sql bulk collect into v_emp_table;
forall i in 1..v_emp_table.count
insert into emp_bak values v_emp_table(i);
end;
declare
v_query_sql varchar2(500);
type type_emp_table is table of emp%rowtype index by binary_integer;
v_emp_table type_emp_table;
type type_ename_table is table of emp.ename%type index by binary_integer;
v_ename_table type_ename_table;
begin
update emp set flag=1 where deptno=20 returning flag bulk collect into v_ename_table;
for i in 1..v_ename_table.count loop
dbms_output.put_line(v_ename_table(i));
end loop;
end;
分享到:
相关推荐
Oracle 中使用 fetch bulk collect into 批量效率的读取游标数据
详细介绍了使用 BULK COLLECT 进行批量操作 提高sql的执行效率 使用MERGE INTO USING 一条sql搞定 新增和修改 使用connect by 进行递归树查询
采用bulk collect可以将查询结果一次性地加载到collections中。而不是通过cursor一条一条地处理。可以在select into,fetch into,returning into语句使用bulk collect。注意在使用bulk collect时,所有的into变量都...
结合一个存储过程的实例,介绍了 bulk collect 的使用
记录 集合 BULK COLLECT FORALL 执行计划
sal,ename BULK COLLECT INTO sal,ename BULK COLLECT INTO sal,ename BULK COLLECT INTO vsal,ename BULK COLLECT INTO sal,ename BULK COLLECT INTO sal,ename BULK COLLECT INTO sal,ename BULK COLLECT ...
bulk collect函数的使用,可以可以提高数据的访问效率
bulk collect bulk collect bulk collect例子特殊应用
我们知道PL/SQL程序中运行SQL语句是存在...下面详解这爷俩㈠ 通过BULK COLLECT 加速查询⑴ BULK COLLECT 的用法采用BULK COLLECT可以将查询结果一次性地加载到collections中,而不是通过cursor一条一条地处理可以在sele
abap collect的用法与实例 abap collect的用法与实例
BULK COLLECT(成批聚合类型)和数组集合type类型is table of 表%rowtype index by binary_integer用法笔记。对oracle批量查询 删除 更新使用bulk collect提高效果的相关知识感兴趣的朋友通过本文一起学习吧
Oracle 遍历游标的四种方式(for、fetch、while、bulk collect+forall) 2.问题分析 我们可以把游标想象成一张表,想要遍历游标,就要取到游标的每行数据,所以问题的关键就成了:如何取到行数据? 3.解决方案 ...
代码如下: — Created on 2010/11/04 by WANGNAN declare — Local variables here i integer; TYPE t_table IS TABLE OF VARCHAR2(10 ) INDEX BY VARCHAR2 (2); v_t_table t_table; TYPE t_pg3 IS TABLE OF asis.PG...
在一般的情况下,使用批量fetch的几率并不是很多,但是Oracle提供了这个功能我们最好能熟悉一下,说不定什么时候会用上它。 代码如下:declare cursor c1 is select * from t_depart; v_depart t_depart%...
关于Oracle FORALL用法的小结
大家好,又见面了。 在我前面的文章《 吃透JAVA的Stream流操作,多年实践总结》中呢,对Stream的整体情况进行了细致全面的讲解,也大概介绍了下结果收集器Collectors的常见用法 ——