1)記住批量綁定是什麼 2)用什麼完成的,各個工具之間都有什麼方法及使用即可
批量綁定:執行單詞SQL操作能傳遞所有集合元素的資料,使用forall,bulk_collect語句完成,
bulk_collect子句用于取得批量資料,隻能用于select,fetch和dml傳回字句中,
forall語句隻試用于執行批量的dml執行,
好處:可以極大地提高資料處理速度,提高應用程式的性能,
demo示例:
create table demo (
id number(6) primary key,name varchar2(10)
);
不使用批量綁定
declare
type id_table_type is table of number(6) index by binary_integer;
type name_table_type is table of varchar2(20) index by binary_integer;
id_table id_table_type;
name_table name_table_type;
start_time number(10);
end_time number(10);
begin
for i in 1..5000 loop
id_table(i):=i;
name_table(i):='Name'||to_char(i);
end loop;
start_time:=dbms_utility.get_time();
for i in 1..id_table.count loop
insert into demo values(id_table(i),name_table(i));
end_time:=dbms_utility.get_time();
dbms_output.put_line('Alltime(s):'||to_char((end_time-start_time)/100));
end;
SQL> /
Alltime(s):.24
PL/SQL procedure successfully completed
使用批量綁定
forall i in 1..id_table.count
Alltime(s):.09
forall有三種文法:
1)forall index in lower_bound..upper_bound
sql_statement;
2)forall index in indices of collection 用于處理null值
[between lower_bound.and. upper_bound]
3)forall index in values of index_collection
該語句還有一個方法是取部分的下标,sql%bulk_rowcount屬性用于取得在執行批量綁定操作時第i個元素所作用的行數,
bulk_collect子句用于取得批量資料,用于select into ,fetch into,dml子句。将批量資料放于pl/sql集合變量中。
1)select into中使用bulk_collect子句,select into隻能傳回一行資料,但是用bulk_collect卻可以一次将select語句的多行結果檢索到集合變量中,
type emp_table_type is table of emp%ROWTYPE index by binary_integer;
emp_table emp_table_type;
begin
select * bulk_collect into emp_table from emp where deptno=&no;
for i in 1..emp_table.count loop
dbms_output.put_line('Employee name :'||emp_table(i).ename);
end loop;
end;
/
2)dml傳回字句中使用
type emp_table_type is table of emp%ROWtype index by binary_integer;
emp_table emp_table_type;
delete from emp where deptno=&no
returning ename bulk collect into ename_table;
for i in 1..emp_table.count loop
dbms_output.put_line(emp_table(i).ename);
/
本文轉自 aklaus 51CTO部落格,原文連結:http://blog.51cto.com/aklaus/1966342