Now I created a package:
create or replace package test_refcursor asCreate test package body:
TYPE DynamicCursorType IS REF CURSOR;
procedure test( id in number,
result out DynamicCursorType );
end test_refcursor;
/
create or replace package body test_refcursor asThe procedure test returns all the departments with department_id lower than passed in ID.
procedure test (
id in number,
result out DynamicCursorType
)
as
begin
open result for select department_id,department_name
from departments where department_id < id;
end;
end test_refcursor;
/
Now the first way to test it in a PL/SQL block:
set serveroutput onThe result you can see:
declare
p test_refcursor.DynamicCursorType;
v_id number(4);
v_name varchar2(100);
begin
test_refcursor.test( 50, p );
-- The cusor has already opened, no need to open again.
loop
fetch p into v_id, v_name;
exit when p%NOTFOUND;
dbms_output.put_line( v_id || ', ' || v_name );
end loop;
end;
/
10, AdministrationThere's another way even easier in SQL Plus:
20, Marketing
30, Purchasing
40, Human Resources
var p refcursorIt'll print as if you select the data from a table:
exec test_refcursor.test( 50, :p );
print p
DEPARTMENT_ID DEPARTMENT_NAME
------------- ----------------------------------
10 Administration
20 Marketing
30 Purchasing
40 Human Resources
No comments:
Post a Comment