Fetch and bulk collect from a REF CURSOR returned by a procedure

两盒软妹~` 提交于 2019-12-07 00:17:25

It's not allowed to use cursor variables in the for cursor loop (FOR i IN myCursor). You have to fetch from the cursor variable explicitly one row at a time, using FETCH INTO statement and regular loop statement for instance or use FETCH BULK COLLECT INTO to populate a collection. For instance:

SQL> declare
  2    TYPE t_clientID_nt IS TABLE OF dual%rowtype;
  3    clientID_nt t_clientID_nt;
  4  
  5    l_cur sys_refcursor;
  6  
  7    procedure OpenAndPopulateCursor(p_cur in out sys_refcursor) is
  8    begin
  9      open p_cur for
 10        select *
 11         from dual;
 12    end;
 13  
 14  begin
 15    OpenAndPopulateCursor(l_cur);
 16  
 17    if l_cur%isopen
 18    then
 19      fetch l_cur bulk collect into clientID_nt;
 20    end if;
 21  
 22    dbms_output.put_line(concat( to_char(clientID_nt.count) 
 23                               , ' record(s) has/have been fetched.'));
 24  end;
 25  /

 1 record(s) has/have been fetched.

 PL/SQL procedure successfully completed
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!