user1596826
user1596826

Reputation: 13

how to call plsql procedure out parameter as a user-defined ref cursor?

I have a package P and a precedure A in it.

create or replace package pkg_get_users_info_by_role
as 
type user_info_ref_cur is ref cursor;
procedure get_user_info_proc 
(p_role_name varchar2, p_user_info out user_info_ref_cur);
end pkg_get_users_info_by_role;
/

and the body;

create or replace package body pkg_get_users_info_by_role
as 
procedure get_user_info_proc
(p_role_name varchar2, p_user_info out user_info_ref_cur)
as
begin
open p_user_info for 
select user_id,username,user_password,role_name from user_info,role_info
where user_info.user_role=role_info.role_id 
and role_info.role_name like p_role_name; 
end; 
end pkg_get_users_info_by_role;

My question is, how can I call the procedure? Do I need a variable of pkg_get_users_info_by_role.user_info_ref_cur type to call it? I cannot create a variable of this type. Is there any way to solve this?

Thanks!!

Upvotes: 1

Views: 9684

Answers (1)

DCookie
DCookie

Reputation: 43533

Yes, you simply define your cursor in your calling program as the package.type. Here's a simple-minded illustration that should work on most any oracle database:

CREATE OR REPLACE PACKAGE pkg AS
TYPE rc IS REF CURSOR;
PROCEDURE get_rc(p_rc OUT rc);
END pkg;
/
CREATE OR REPLACE PACKAGE BODY pkg AS
PROCEDURE get_rc(p_rc OUT rc) IS
BEGIN
  OPEN p_rc FOR
  SELECT t.owner, t.table_name FROM all_tables t;
END get_rc;
END pkg;
/

DECLARE
  crsr pkg.rc;
  v1 VARCHAR2(32);
  v2 VARCHAR2(32);
BEGIN
  pkg.get_rc(crsr);
  LOOP
    EXIT WHEN crsr%NOTFOUND;
    FETCH crsr INTO v1, v2;
    dbms_output.put_line(v1||': '||v2);
  END LOOP;
  CLOSE crsr;
END;
/

Upvotes: 3

Related Questions