游标基本结构及属性(cursor)

本文介绍了Oracle数据库中的游标,包括显式游标和隐式游标的概念与区别。通过示例展示了如何使用游标进行数据操作,并详细解释了游标的几个关键属性:%ISOPEN、%FOUND、%NOTFOUND和%ROWCOUNT,以及它们在游标操作中的作用。
摘要由CSDN通过智能技术生成
游标分类
显式游标:使用cursor语句显式定义游标,游标被定义后,需要打开并提取游标,关闭游标。
隐式游标:由oracle为每一个dml语句创建一个隐式游标也叫做SQL游标。

BEGIN
  update emp
    set comm = comm *1.12
    where empno = 7346;
  dbms_output.put_line(SQL%ROWCOUNT || '行被更新');--使用隐式游标
  if sql%notfound --使用隐式游标
  then
    dbms_output.put_line('不能更新员工号为7346的员工');
  end if;
  commit;
end;
--由于隐式游标会自动关闭,因此SQL%isopen都是false.

declare
  cursor emp_cursor (p_deptno in number) return emp%rowtype --定义游标并指定参数 和返回值
  is 
    select * from emp where deptno = p_deptno;
begin
  open emp_cursor(20);
end;

游标属性
%ISOPEN:判断对应的游标变量是否打开,如果打开则返回true否则返回false.
declare 
  cursor emp_cursor (p_deptno in number)
  is 
    select * from emp where deptno = p_deptno;
begin
  if not emp_cursor%isopen then open emp_cursor(20);
  end if;
  if emp_cursor%isopen then 
    dbms_output.put_line('游标已经被打开');
  else
    dbms_output.put_line('游标还未被打开');
  end if;
  close emp_cursor;
end;

%FOU
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值