sys_refcursor 和 cursor 優缺點比較
優點比較
優點一:sys_refcursor,可以在存儲過程中作為參數返回一個table格式的結構集(我把他認為是table類型,容易理解,其實是一個游標集), cursor 只能用在存儲過程,函數,包等的實現體中,不能做參數使用。
優點二:sys_refcursor 這東西可以使用在包中做參數,進行數據庫面向對象開放。哈哈。我喜歡。cursor就不能。
缺點比較:
缺點:sys_refcursor 不能用open,close ,fetch 進行操作。不好學,難理解。
cursor可以用 open,close ,fetch操作,容易學,易懂
其他就目前不知道,至於游標的的基礎概念,去google,百度一大堆的。這里就不累贅了。看例子:
建立一個存儲過程
create or replace procedure up_test(o out sys_refcursor) is
begin
open o for select * from lq_test;
end;
返回的類型是sys_refcursor;
建立第二個存儲過程
create or replace procedure up_getData(aMsg out varchar2) is
type p_table_type is table of lq_test%rowtype;
p_table p_table_type;
v sys_refcursor;
begin
up_test(v);
fetch v bulk collect into p_table;
for i in 1..p_table.count loop
dbms_output.put_line('字段1:'||p_table(i).v1 || ' 字段2:' || p_table(i).v2);
end loop;
end;
這里要注意fetch 帶參數的用法,bulk collect ,這是第集合的操作,必須先定義一個結合類。見上面的例子,還不懂就google了。用法就簡單,沒啥好介紹的。
取集合的值應該這樣p_table(i).v1,其中i標識幾行,帶上字段,即可了。呵呵,容易理解。