Oracle ref 游標(轉)


 

ref游標和普通游標(自定義游標)最大的區別是游標的結果集在什么地獲取。

普通游標在聲明的時候,獲得結果集,例子 cursor v_cur is select * from t_test;

而ref游標在打開的時候,獲得結果集,

例子:open infolist for select bi.* from bi_customer cf;

 

1.獲得結果集
declare 
  type refcursor is ref cursor; --ref游標類型 
 infolist     refcursor; --集合 
  customer     bi_customer%rowtype; --行 
  customercode bi_customer.customercode%type;--字段 
  customername bi_customer.corporation%type; 
begin 
  open infolist for 
    select bi.* from bi_customer cf; --全部 
  loop 
    fetch infolist 
      into customer; 
    exit when infolist%notfound; 
    dbms_output.put_line('客戶編號為;:'||''||customer.customercode||',   地址為:'||customer.address ); 
  end loop; 
  close infolist; 
end;   

               

2 過程限定結果集,帶返回記錄數 

代碼 
create or replace package ord is--包  
 
  
  type ref_type is ref cursor; 
 
  procedure p_order(firstindex in number,--開始下標 
                    lastindex  in number,--結束下標 
                    recordnum  out number,--記錄數 
                    infolist   out ref_type--結果集 
); 
end ord; 
 
create or replace package body ord is 
 
  procedure p_order(firstindex in number, 
                    lastindex  in number, 
                    recordnum  out number, 
                    infolist   out ref_type) as 
    sql_tempstr  varchar2(400); 
    sql_countstr varchar(500); 
  begin 
    sql_tempstr := 'select * 
        from (select rownum num, o.* from orders o) s 
       where 1>0'; 
    if (firstindex is not null and lastindex is not null) then 
      sql_tempstr := sql_tempstr || 'and num between ' || firstindex || 
                     ' and 
                     ' || lastindex; 
    end if; 
    sql_countstr := ' select count(*) from(' || (sql_tempstr) || ') '; 
    dbms_output.put_line(sql_tempstr); 
    dbms_output.put_line(sql_countstr); 
    open infolist for sql_tempstr; 
    Execute Immediate sql_countstr 
      into recordnum; 
  end p_order; 
 
end ord; 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM