作用:处理多行记录集
类似指针,一次可以处理其中一行
游标的主要属性
%found: 如果sql语句至少影响一行操作,返回true,否则返回false
%notfound: 如果sql语句至少影响一行操作,返回false,否则返回true
%isopen: 当游标打开时,返回true,关闭时返回false
%rowcount: 返回sql语句受影响的行数
隐式游标
平常的select查询,DML操作,oracle都会自动创建
声明隐式游标来处理结果数据
原因:rowtype是存一行数据,而我返回太多行了
修正后
显式游标
如需要完成特定功能的select ,DML操作,可以创建一个显式游标
使用显示游标步骤
1)声明游标
语法
cursor cursorName【parameter【,param….】】【return returnType】
is
select语句
parameter:游标的输入参数
parameter格式:parameterName【in】dataType【{:=|default expression}】
2)打开游标
open cursorName(参数)
3)检索数据
fetch cursorName into 变量名;
4)关闭游标
close cursorName;
案例loop 输出游标信息
declare cursor stu_cursor(s in varchar2) is select sage,sname from student where rownum=1; type stu is record( age student.sage%type, name student.sname%type ); s stu; begin open stu_cursor(‘cursorOpen’); 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;