bulk collect支持emp%rowtype形式
forall插入式不支持emp%rowtype形式,如果有多个字段,要定义多个数组来存放,如果插入前有判断条件,可以使用values of
关于limit参数
可以根据实际需要来调整limit参数大小,来达到你最优的性能。limit 参数会影响到 pga (程序全局区)的使用率。而且也可以在 fetch bulk 中省略 limit 参数,写成fetch all_contacts_cur bulk collect into v_contacts;
测试数据
create table T2
(
A CHAR(10),
B CHAR(10)
)
create table t3 as select * from t2
添加200条数据
begin
for i in 1..200 loop
insert into t2 values( ABS(MOD(DBMS_RANDOM.RANDOM,100)) ,TRUNC(100+900*dbms_random.value) );
commit;
end loop;
开始试验
declare
type RecTyp is RECORD(
t dbms_sql.varchar2_table,
b dbms_sql.varchar2_table);
l_rec RecTyp;
v_i int:=0;
cursor c is
select * from t2;
BEGIN
open c;
loop
fetch c bulk collect into l_rec.t,l_rec.b limit 50;
v_i:=v_i+1;
forall i in 1..l_rec.t.count
insert into t3(a,b) values(l_rec.t(i),l_rec.b(i));
exit when c%notfound;
end loop;
dbms_output.put_line(v_i);
commit;
close c;
end;
可以看到v_id的最终结果是5,也就是说游标循环了5次,也就是说结果批量取了四次;
forall的用法可以参照前面的文章
declare
type description_table_type is table of t1.description%type;
type t1_table_type is table of t1%rowtype;
description_table description_table_type:=description_table_type('FIVE', 'SIX', 'SEVEN');
t1_table t1_table_type;
begin
forall i in description_table.first..description_table.last
insert into t1 values(t1_seq.nextval,description_table(i))
returning id ,description bulk collect into t1_table;
for i in t1_table.first..t1_table.last loop
DBMS_OUTPUT.put_line('INSERT ID=' || t1_table(i).id ||'DESC='|| t1_table(i).description);
end loop;
end;
forall的主要作用就是提高dml操作的效率