--创建或修改存储过程,存储过程名为findclass,输入参数为classId,输出className
create or replace procedure findclass(classId in int,classStudents out sys_refcursor,className out varchar2)
as
--定义一个游标的方式有多种,可以显示定义CURSOR cursor_name is select * from table,也可以定义动态游标,游标关键词CURSOR
TYPE ref_cursor_type IS REF CURSOR; --定义一个动态游标
students ref_cursor_type; --定义班级集合为一个游标类型
student_row student%rowtype; --定义班级类型,类型为student表行类型
--存储过程开始
begin
--把查询出来的class_name 赋值给输出变量className,查询条件为classId
select class_name into className from class where id = classId;
--打开游标并赋值
open students for select * from student where fk_class =classId;
--把查询结果赋值给输出变量,实际上可以直接open classStudents for select * from student where fk_class =classId;
classStudents := students;
--循环输出游标,循环有三种方式,for in循环,fetch循环,while循环
--fetch循环
loop
fetch classStudents into student_row;
--当循环到空跳出循环
EXIT WHEN classStudents%NOTFOUND;
DBMS_OUTPUT.put_line('学生名:'||student_row.student_name);
end loop;
DBMS_OUTPUT.put_line('班级名:'||className);
--存储过程结束
end findclass;
into前面写table列名,后面写需要赋值的变量名,顺序要对
select student_name,class_name into studentName,className from class;转载自https://www.jianshu.com/p/159f3f41f4cc