oracel游标循环取值实例

本文深入解析了SQL游标的作用及其在数据库数据检索中的应用,包括使用LOOP循环和FOR...IN...LOOP循环来遍历游标获取数据,并在遍历结束后正确关闭游标以释放资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

游标的作用就是用于临时存储从数据库中提取的数据块

1、loop循环取游标的值

--定义游标
declare
 cursor test_cursor is 
 select * from t_pstu_reginfo where rownum<10;
 tab test_cursor%rowtype;---定义游标变量  
 begin
  open test_cursor;
  loop
    fetch test_cursor into tab;
    exit when test_cursor%notfound;
    dbms_output.put_line(tab.zjhm);
  end loop;

  if test_cursor%isopen then 
  close test_cursor;
  end if;
 end;


2、for.. in ...loop 循环取游标的值

 declare
 cursor test_cursor is 
     select * from t_pstu_reginfo where rownum<10;
 begin
    for tab in test_cursor loop
        dbms_output.put_line(tab.zjhm);
    end loop;

  exception 
  when others then 
     dbms_output.put_line('error');
  if test_cursor%isopen then 
      close test_cursor;
  end if;
 end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值