3

I want to write PL/SQL to test a function in a package. The package defines a cursor type

TYPE ref_cursor IS REF CURSOR;

I want to define a record based on that type.

My code is:

DECLARE
  cur PACKAGE_NAME.ref_cursor; 
  rec cur%ROWTYPE;

why is last line not correct?

Justin Cave
  • 227,342
  • 24
  • 367
  • 384
Alex Tang
  • 115
  • 1
  • 4
  • 12
  • Can you post the declaration of the cursor type in the package? In particular, is the package declaring a strong or a weak cursor? – Justin Cave Feb 21 '12 at 21:10
  • Hi Justin, this is declaration in the package that I want to test. (TYPE ref_cursor IS REF CURSOR;) – Alex Tang Feb 22 '12 at 00:24

1 Answers1

13

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;
Justin Cave
  • 227,342
  • 24
  • 367
  • 384
  • declare l_rc sys_refcursor; l_emp emp%rowtype; begin what if i dont know type of l_emp or do not have permission on object type – donstack Sep 03 '14 at 13:09