Oracle游标使用
一、游标是什么
游标字母理解就是游动的光标。
用数据库语言描述:游标是映射在结果集中的一行数据的实体,有了游标,用户就可以访问结果集中的任意一行数据,将游标定位到某行后即可对该行数据进行操作,例如提取当前行的数据等。
二、游标的分类
显示游标和隐士游标
显示游标的使用需要4步:
1、声明游标
cursor mycursor(vartype number) is
select id from table1
where id=vartype
2、打开游标
open mycursor(000627)
3、读取游标
fetch mycursor into varno
4、关闭游标
close mycursor
三、游标属性
oracle游标有4个属性:
%isopen判断游标是否被打开,如果打开%isopen等于true,否则等于false
%found、%notfound判断游标所在行是否有效,如果有效%found等于true,否则等于false
%rowcount返回当前位置为止游标读取的记录行数
四、示例
declare
varno varchar(20);
varprice varchar(20);
cursor mycursor(vartype number) is
select emp_no.emp_zc from cus_emp_basic
where com_no=vartype
begin
if mycursor%isopen=false then
open mycursor(000627)
end if;
fecth mycursor into varno.varprice;
while mycursor%found
dbms_output.putlne(varno||'.'varprice);
if mycursor%rowcount=2 then
exit;
end if;
fetch mycursor into varno.varprice;
end;