Oracle動態語句中返回遊標

regonly1發表於2016-06-22

本來以為動態語句只能返回類似int、varchar2這種型別,今天測試了下,發現還支援遊標,現測試如下:

建立返回遊標的函式:
create or replace function testf return sys_refcursor as
    v_refcur sys_refcursor;
begin
    open v_refcur for
    select * from test;
   
    return v_refcur;
end;

測試動態語句返回遊標到變數的情況:
declare
     type tbl_numlist is table of pls_integer;
     v_numlist tbl_numlist;
     v_refcur sys_refcursor;
 begin
    --方法1:
     execute immediate 'select testf() from dual' into v_refcur;
    
     fetch v_refcur bulk collect into v_numlist;
    
     for i in 1 .. v_numlist.count loop
         dbms_output.put_line(v_numlist(i)); 
     end loop;
     close v_refcur;
    
    --方法2:
    execute immediate 'begin :1 := testf(); end;' using out v_refcur;
   
    fetch v_refcur bulk collect into v_numlist;
   
    for i in 1 .. v_numlist.count loop
        dbms_output.put_line(v_numlist(i)); 
    end loop;
    close v_refcur;
 end;

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12932950/viewspace-2120701/,如需轉載,請註明出處,否則將追究法律責任。

相關文章