PL/SQL语句块基本语法的的综合练习例子

本文详细介绍了在Oracle数据库中如何使用PL/SQL进行游标操作,包括自定义游标类型的定义、存储过程的创建及游标的打开与读取等关键步骤。通过具体示例展示了游标在实际应用中的作用。

--定义包含有自定义的游标类型

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;

--PL/SQL语句块

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值