oracle游标的使用

--当select语句从数据库中返回的记录多余一条时,就可以使用游标(cursor)。游标可以理解为一次访问一个的一组记录。select语句将列提取到游标中,然后根据游标取得记录。使用游标时需要遵从以下的5个步骤:
(1)声明一些变量,用于保存select语句返回列值
(2)声明游标,并制定select语句
(3)打开游标
(4)从游标获取记录

(5)关闭游标

<span style="font-size:18px;">-- 游标的使用
set serveroutput on;
declare
  --声明变量
  sname varchar2( 20);
  --声明游标
  cursor student_cursor is select sn from s; 
  begin
      --打开游标
      open student_cursor;
      --让游标指针往下移动
      fetch student_cursor into sname ;
      --判断游标指针是否指向某行记录
      while student_cursor%found 
        --遍历
        loop
          dbms_output.put_line ('学生姓名' ||sname );
          fetch student_cursor into sname;
        end loop;
     close student_cursor;
end;

--游标的使用游标,for循环是在pl/sql块中使用游标最简单的方式,它简化了对游标的处理。
--当使用游标for循环时,oracle会隐含的打开游标,提取游标数据并关闭游标。
set serveroutput on;
declare
  --声明游标
  cursor student_cursor is select sn, dept from s;  
  begin
      --打开游标
      --open student_cursor;
      --让游标指针往下移动
      for st in student_cursor     
        loop
          --fetch student_cursor into st;
          dbms_output.put_line ('学生姓名' ||st.sn ||'学生系别'||st.dept);
        end loop;
      close student_cursor;
  end;
</span>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值