FORALL与BULK COLLECT是实现批量SQL
1. 示例1
declare
/* type card_rec_type is record
( dn_no channel.dn_no%type,
channel_id channel.channel_id%type);
type nested_card_type is table of card_rec_type;
card_tab nested_card_type; */
cursor card_rec is
select dn_no,channel_id from channel;
type nested_card_type is table of card_rec%rowtype;-->基于游标的嵌套表类型
card_tab nested_card_type;
begin
select dn_no,channel_id
bulk collect into card_tab
from channel
where lan_id=7;
forall i in card_tab.first .. card_tab.last
insert into (select dn_no,channel_id from tb_card)
values card_tab(i);
commit;
dbms_output.put_line(‘The total ‘||card_tab.count||‘ has insert into tb_card‘);
end;
/--如果我们在forall抛出异常的时候进行捕获,在Exception处理语句中进行commit的话,我们成功更新的记录是可以保存下来
示例2
declare cursor cur is select object_id,object_name from dba_objects a where a.OBJECT_TYPE=‘TABLE‘; l_count number :=0; l_commit number :=100; type t_target is table of cur%rowtype; l_target t_target; l_limit number default 10; begin open cur; loop fetch cur bulk collect into l_target limit l_limit; exit when l_target.count=0; forall i in l_target.first .. l_target.last insert into tmp_lbx (id,name) values(l_target(i).object_id,l_target(i).object_name); l_count :=l_count + l_target.count; if l_count >= l_commit then commit; l_count :=0; end if; end loop; commit; close cur;end;
2. 异常处理示例 Forall中Bulk_Exceptions捕获
declare
type id_list is table of t.object_id%type index by binary_integer;
id_info id_list;
i number;
ERRORS_NUM number;
begin
select object_id
bulk collect into id_info from t;
forall i in id_info.first .. id_info.last save exceptions
update t set subobject_name=object_type||timestamp
where object_id=id_info(i);
commit;
exception
when others then
errors_num :=sql%bulk_exceptions.count;
dbms_output.put_line(‘Total Exception is :‘||to_char(errors_num));
if errors_num > 10 then
errors_num := 10;
end if;
for i in 1 .. errors_num loop
dbms_output.put_line(‘Error #‘||i||‘ is ‘||sql%bulk_exceptions(i).error_index||‘ ‘ ||sqlerrm(-sql%bulk_exceptions(i).error_code));
end loop;
rollback;
end;
/
3. forall与rowcount
declare
type id_list is table of t.object_id%type index by binary_integer;
id_info id_list;
i number;
j number;
ERRORS_NUM number;
begin
select object_id
bulk collect into id_info from t;
forall i in id_info.first .. id_info.last save exceptions
update t set subobject_name=object_type||timestamp
where object_id=id_info(i);
for j in id_info.first .. id_info.last loop
dbms_output.put_line(‘Iteration# ‘||j||‘ : ‘||sql%bulk_rowcount(j)||‘ rows.‘);
end loop;
commit;
end;
/
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。