使用含有参数的游标和嵌套循环: --含有条件的游标
--实现目标是,查询每个班的学生
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;