--定义包含有自定义的游标类型
create or replace
package upk_select_test
as type uc_test is ref cursor; -- 声明ref游标类型
end upk_select_test;
--定义返回游标的存储过程
create or replace
procedure getref(outref out upk_select_test.uc_test)
is
begin
open outref for select * from playbill_temp;
end getref;
DECLARE
v_pgmid number;
v_exception EXCEPTION;
j NUMBER;
type out_ref is ref cursor;--自定义游标类型
outref out_ref;--声名游标变量
v_row playbill_temp%rowtype; -- 匹配t_test表中一行所有的数据类型(表名:playbill_temp)
begin
j:=0;
--练习游标的定义
DECLARE cursor pgmid_test is
select * from pgmid_temp; --只有一个id字段的表可以根据自己的表名修改
begin open pgmid_test;
loop
fetch pgmid_test into v_pgmid;
exit when pgmid_test%notfound;
dbms_output.put_line('maxid:'||v_pgmid);
end loop ;
CLOSE pgmid_test;
end;
--for循环的练习
for i in 1..50
loop
dbms_output.put_line('maxid2:'||i );
j:=i;
--if条件判断的练习
if i>50 then
--自定义异常的抛出练习
raise v_exception;
end if;
end loop;
--游标返回值的存储过程的练习
begin
--返回游标
getref(outref);
loop
fetch outref into v_row;
exit when outref%notfound;
dbms_output.put_line(v_row.channelno);
end loop;
close outref;
end;
--异常接收的练习
exception when v_exception then
dbms_output.put_line('i'||j||'v_exception');
when others then
dbms_output.put_line('others exception');
end;
--显示dbms_output.put_line的内容,v_row.channelno数字可以正常显示,汉字显示的是乱码(查找资料解决乱码问题)
set serveroutput size 3000