How to use record to loop a ref cursor?

淺唱寂寞╮ 提交于 2019-12-04 16:12:57

You can't define a record type based on a weakly-typed REF CURSOR. Since the cursor type defined in the package can be used to return data from an arbitrary query with arbitrary columns, the PL/SQL compiler can't determine an appropriate record type to fetch the data into.

If you know the actual data being returned from the function, you could declare a record of that type to fetch the data into. For example, if I declare a function that returns a weakly-typed cursor type but I know that the cursor really returns a cursor based on the EMP table, I can fetch the data into an EMP%ROWTYPE record (note that SYS_REFCURSOR is a system-defined weakly-typed REF CURSOR type)

create or replace function f1
  return sys_refcursor
is
  l_rc sys_refcursor;
begin
  open l_rc
   for select *
         from emp;
  return l_rc;
end;

declare
  l_rc sys_refcursor;
  l_emp emp%rowtype;
begin
  l_rc := f1;
  loop
    fetch l_rc into l_emp;
    exit when l_rc%notfound;
    dbms_output.put_line( l_emp.empno );
  end loop;
end;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!