Oracle之游标——使用(续)

游标

上节回顾:
1、游标
隐式游标:
select… into… from… where
DML命令
属性:SQL%isopen–假的 关闭的
SQL%found
SQL%notfound
SQL%rowcount—受影响行数

显式游标:
select… from… where 可以返回多行记录
声明游标、打开游标、提取数据、关闭游标
不带参数 带参数
%isopen %found 、%notfound相当于@@fetch_status
%rowcount :已经从游标中提取的记录的条数

——fetch 游标名 into
记录变量名 表名%rowtype
记录变量名 游标名%rowtype

本讲内容:
1、直接使用for循环

eg:

begin
 for vSC in (select Sno,Sname from Student) loop--或者 into游标名
   dbms_output.put_line('Sno:'||vSC.Sno||',Sname:'||vSC.Sname);
 end loop; 
end;

输出:

Sno:2017005,Sname:向华
Sno:2017006,Sname:肖战
Sno:2017001,Sname:杨幂
Sno:2017002,Sname:李四
Sno:2017003,Sname:郭雅琦
Sno:2017004,Sname:王五
Sno:2017007,Sname:王一博
Sno:2017008,Sname:迪丽热巴

2、使用游标来更新或删除数据
使用游标来更新数据时要加上 for update
当前行:通过where current of 游标名 来标识游标当前提取的数据行
eg:

declare
  cursor curSC is select Sno,Cno,Grade from SC for update;
  vCno char:='1';
begin
 for vSC in curSC loop
  if vSC.Cno=vCno then
    dbms_output.put_line('Sno:'||vSC.Sno||',Grade:'||vSC.Grade);
    update SC set Grade =Grade-10 where current of curSC;
  end if;
 end loop;
end;

输出:
在这里插入图片描述
3、游标变量:
基于refcursor 类型所定义的变量:

declare
  type refCursor is ref cursor;--定义refcursor类型
  vCursor refCursor;--定义了一个游标变量
  vSname Student.Sname%type;
  vSno Student.Sno%type;
begin
  open vCursor for select Sno,Sname from Student where Ssex='男';
  loop
   fetch vCursor into vSno,vSname;
   exit when vCursor%notfound;
   dbms_output.put_line('Sno:'||vSno||',Sname:'||vSname);
 end loop;
   close vCursor;
end;

输出:
在这里插入图片描述
案例扩展:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭雅琦hh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值