--pkg
create or replace package cur_pkg as
type t_cur is ref cursor;
procedure open_cur_spr_ppl (spr_id in number,
spr_name in varchar2,
spr_family in varchar2);
end cur_pkg;
--pkg_body
create or replace package body cur_pkg as
procedure open_cur_spr_ppl (spr_id in number,
spr_name in varchar2,
spr_family in varchar2)
is
v_curs t_cur;
begin
open v_curs for
select spravochnik_id, spravochnik_name, spravochnik_family
from spravochnik_people
where spravochnik_id >= 1770;
loop
FETCH v_curs INTO spr_id, spr_name, spr_family;
EXIT WHEN v_curs%notfound;
dbms_output.put_line('ID:' || spr_id || 'Name:' || spr_name || 'Family:' || spr_family);
end loop;
close v_curs;
end open_cur_spr_ppl;
end cur_pkg;
I want to output the data for the cursor. Please advise how to implement it correctly. The problem is these lines. I don't know how to do it correctly.
FETCH v_curs INTO spr_id, spr_name, spr_family;
EXIT WHEN v_curs%notfound;
dbms_output.put_line('ID:' || spr_id || 'Name:' || spr_name || 'Family:' || spr_family);
One of the errors in your procedure was trying to use:
Where
spr_id, spr_name, spr_familyare all declared asINparameters and cannot be written to. You would want to declare them as local variables instead.When you want to retrieve a cursor, normally you do not open a cursor and print it to the database console immediately as that makes the procedure only useful for that one thing and most times you are going to want to return the cursor without reading it.
Instead, you would open the cursor and then return it as an
OUTparameter to the calling scope so that they can read it and, if necessary, format and output it separately. If you did want to print it to the console then you can split that off into a separate procedure:Then if you want to call it you can use:
Which, for the sample data:
Outputs:
db<>fiddle here