oracle中bulk collect into用法

轉自:http://xuejiangtao.iteye.com/blog/1168570

通過bulk collect減少loop處理的開銷,採用bulk collect可以將查詢結果一次性地加載到collections中,而不是通過cursor一條一條地處理。 可以在select into,fetch into,returning into語句使用bulk collect。 注意在使用bulk collect時,所有的into變量都必須是collections。

舉幾個簡單的例子: 
--在select into語句中使用bulk collect 
DECLARE 
TYPE SalList IS TABLE OF emp.sal%TYPE; 
sals SalList; 
BEGIN 
-- Limit the number of rows to 100. 
SELECT sal BULK COLLECT INTO sals FROM emp 
WHERE ROWNUM <= 100; 
-- Retrieve 10% (approximately) of the rows in the table. 
SELECT sal BULK COLLECT INTO sals FROM emp SAMPLE 10; 
END; 

--在fetch into中使用bulk collect 
DECLARE 
TYPE DeptRecTab IS TABLE OF dept%ROWTYPE; 
dept_recs DeptRecTab; 
CURSOR c1 IS 
SELECT deptno, dname, loc FROM dept WHERE deptno > 10; 
BEGIN 
OPEN c1; 
FETCH c1 BULK COLLECT INTO dept_recs; 
END; 

--在returning into中使用bulk collect 
CREATE TABLE emp2 AS SELECT * FROM employees; 
DECLARE 
TYPE NumList IS TABLE OF employees.employee_id%TYPE; 
enums NumList; 
TYPE NameList IS TABLE OF employees.last_name%TYPE; 
names NameList; 
BEGIN 
DELETE FROM emp2 WHERE department_id = 30 
RETURNING employee_id, last_name BULK COLLECT INTO enums, names; 
dbms_output.put_line('Deleted ' || SQL%ROWCOUNT || ' rows:'); 
FOR i IN enums.FIRST .. enums.LAST 
LOOP 
dbms_output.put_line('Employee #' || enums(i) || ': ' || names(i)); 
END LOOP; 
END; 

Oracle數組

轉自:http://www.cnblogs.com/lll3344/archive/2011/03/09/1978366.html


Oracle數組一般可以分爲固定數組和可變數組 
固定數組
declare  
type v_ar is varray(10) of varchar2(30);   
my_ar v_ar:=v_ar('g','m','d','龔','帥');   
begin  
      for i in 1..my_ar.count  
      loop   
          dbms_output.put_line(my_ar(i));   
      end loop;   
end; 
可變數組 
一維數組 
declare  
type v_table is table of varchar2(30) index by binary_integer;   
--類型可以是前面的類型定義,index by binary_integer子句代表以符號整數爲索引,   
--這樣訪問表類型變量中的數據方法就是“表變量名(索引符號整數)”。   
my_table v_table;   
begin  
      for i in 1..20   
     loop   
          my_table(i):=i;   
          dbms_output.put_line(my_table(i));   
      end loop;   
end; 
多維數組--多條記錄
declare  
type v_table is table of liutest%rowtype index by binary_integer;   
my_table v_table;   
begin  
      select * bulk collect into my_table from liutest;   
      for i in 1..my_table.count/10 --my_table.count/10取到的值爲四捨五入值   
      loop   
          dbms_output.put_line('name--'||my_table(i).name);   
          dbms_output.put_line('address---'||my_table(i).address);   
          dbms_output.put_line('age----'||my_table(i).age);  
      end loop;   
end; 
多維數組--單條記錄
declare  
type v_table is table of liutest%rowtype index by binary_integer;   
my_table v_table;   
begin  
      select * into my_table(1) from liutest where id=1;   
      --my_table(i) i可以爲任意整數,但取值時必須保持以i一致;   
      dbms_output.put_line('--name--'||my_table(1).name||'--address--'||my_table(1).address);    
end; 
自定義數組 
create or replace type varray_list as varray(30) of varchar2(50);   
--使用自定義數組   
create or replace procedure show_list(p_varlist in varray_list)   
is  
v_str varchar2(50);   
begin  
      for i in 1..p_varlist.count    
      loop   
          v_str:=p_varlist(i);   
          dbms_output.put_line('v_str='||v_str);   
          dbms_output.put_line('p_varlist('||i||')='||p_varlist(i));   
     end loop;   
end;   
  
declare  
my_var varray_list:=varray_list('g','m','d','龔','帥');   
begin  
     show_list(my_var);    
end; 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章