大量繫結(FORALL)

來源:互聯網
上載者:User

1.大量繫結是指執行單次SQL操作能傳遞所有集合元素的資料,可以極大的加快資料處理速度,提高程式的效能,它是使用Bulk Collect子句和ForALL子句來完成的。

2.ForAll:該子句只適用於執行批量的DML操作,只能修飾一條語句,它包括3種文法:

a.ForAll index in lower_bound..upper_boundsql_statement;例如:create table my_demo( mid number(6) primary key, mname varchar2(50));declare type my_index_table1 is table of my_demo.mid%type index by binary_integer; type my_index_table2 is table of my_demo.mname%type index by binary_integer; my_id my_index_table1; my_name my_index_table2; start_time number(10); end_time number(10);begin for i in 1..1000 loop  my_id(i):=i;  my_name(i):=to_char(i)||'NAME'; end loop; start_time:=dbms_utility.get_time; forall i in 1..1000   insert into My_DEMO values(my_id(i),my_name(i)); end_time:=dbms_utility.get_time;  dbms_output.put_line(end_time-start_time);end;b.ForAll index in indices of collection[Between lower_bound.and. upper.bound]sql_statement;(和迭代器一樣,遍曆集合)例如:declare  type id_table_type is table of demo%rowtype index by binary_integer;  id_table id_table_type;  type index_table_type is table of Pls_Integer index by binary_integer;  index_table index_table_type;begin  select object_id,object_name bulk collect into id_table from all_objects where rownum<=10;  index_table(1):=1;  index_table(2):=2;  index_table(4):=6;  index_table(6):=8;  index_table(8):=10;  forall i in indices of index_table--其中index_table中是1,2,4,6,8         insert into demo(did) values(id_table(i).did);--id_table(1,2,4,6,8)           end; c.ForAll index in values of index_collectionsql_statement;(遍曆集合裡面的值)例如:declare  type id_table_type is table of demo%rowtype index by binary_integer;  id_table id_table_type;  type index_table_type is table of PLS_Integer index by binary_integer;  index_table index_table_type;begin  select object_id,object_name bulk collect into id_table from all_objects where rownum<=10;  index_table(1):=1;  index_table(2):=2;  index_table(4):=6;  index_table(6):=8;  index_table(8):=10;  forall i in values of index_table--其中index_table中的值是1,2,6,8,10         insert into demo(did) values(id_table(i).did);--id_table(1,2,6,8,10)           end; 
3.Bulk Collect:該子句用於取得批量資料,只能用於Select、Fetch和DML返回子句中。
declare       type eaa is record(            empno scott.emp.empno%type,            ename scott.emp.ename%type,            sal scott.emp.sal%type       );       type emp_table_type is table of scott.emp%rowtype       index by binary_integer;       emp_table emp_table_type;              type aa is table of scott.emp%rowtype;       a aa;              type bb is varray(1000) of scott.emp%rowtype;       b bb;       begin       select * bulk collect into emp_table from scott.emp;       for i in 1..emp_table.count loop        dbms_output.put_line(emp_table(i).ename);       end loop;                        dbms_output.new_line;             select * bulk collect into b from scott.emp;       for i in 1..b.count loop        dbms_output.put_line(b(i).ename);       end loop;end;       

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.