oracle数据库中游标的使用,Oracle数据库中游标的使用

使用含有参数的游标和嵌套循环: --含有条件的游标

--实现目标是,查询每个班的学生

declare

--先要定义两个游标

--定义tab_class游标

cursor s_tab_class is

select * from tab_class;

--定义tab_stu游标,但是这里需要一个参数,就是班级的id

cursor s_tab_stu(v_class_id number) is

select * from tab_stu where class_id=v_class_id;

--定义两个存储变量

--定义tab_class

v_tab_class_rowtype tab_class%rowtype;

--定义tab_stu

v_tab_stu_rowtype tab_stu%rowtype;

begin

--打开游标

open s_tab_class;

loop

--提取数据

fetch s_tab_class into v_tab_class_rowtype;

exit when s_tab_class%notfound;

dbms_output.put_line('班级:'||v_tab_class_rowtype.class_name||'含有');

--嵌套循环,遍历当前班级的学生

open s_tab_stu(v_tab_class_rowtype.class_id);

loop

fetch s_tab_stu into v_tab_stu_rowtype;

exit when s_tab_stu%notfound;

dbms_output.put_line(v_tab_stu_rowtype.stu_name);

end loop;

close s_tab_stu;

end loop;

close s_tab_class;

end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值