检索和关闭游标+检索游标+关闭游标
游标的主要属性
%found: 如果sql语句至少影响一行操作,返回true,否则返回false
%notfound: 如果sql语句至少影响一行操作,返回false,否则返回true
%isopen: 当游标打开时,返回true,关闭时返回false
%rowcount: 返回sql语句受影响的行数
检索和关闭游标
1打开游标后,游标对应的select语句也就被执行了,如果想要获取结果集中的数据,就需要检索游标。
检索游标
1从结果集中获取单行数据,并保存到定义的变量中
语法
fetch cursor_name into variable【,….】;
variable:存储结果集的单行数据变量,可以使用多个普通类型的变量,一对一的接受数据行中的列值。也可以使用%rowtype类型的变量或者自定义的记录类型变量,接收数据行中的所有值。
关闭游标
1释放游标中select语句的查询结果所占用的系统资源
语法
close cursor_name;
案例loop 输出游标信息
declare
cursor stu_cursor(num number:=1)
is
select sage,sname from student where rownum=num;
type stu is record(
age student.sage%type,
name student.sname%type
);
s stu;
begin
open stu_cursor(1);
loop
fetch stu_cursor into s;
exit when stu_cursor%notfound;
dbms_output.put_line(s.age||’———-‘||s.name);
end loop;
close stu_cursor;
end;
案例for 输出游标信息
declare
cursor stu_cursor
is
select sage,sname from student where rownum=1;
s stu_cursor%rowtype;
begin
for s in stu_cursor loop
dbms_output.put_line(s.sage||’———-‘||s.sname);
end loop;
end;