ORACLE/PLSQL 游标的简单使用

游标的属性 %found,%notfound,%isopen,%rowcount。
    %found:若前面的fetch语句返回一行数据,则%found返回true,如果对未打开的游标使用则报ORA-             1001异常。
    %notfound,与%found行为相反。
    %isopen,判断游标是否打开。
    %rowcount:当前游标的指针位移量,到目前位置游标所检索的数据行的个数,若未打开就引用,返回ORA-1001。
1.方法一:
Declare
  Cursor my_cursor is select r.person_id,r.emp_number,r.emp_name from hr_user r where rownum between 1 and 10;
  My_rec my_cursor%rowtype;
Begin
  Open my_cursor; --如果这行错误可以注销
  loop
  Fetch my_cursor into My_rec ;
   DBMS_OUTPUT.PUT_LINE('员工号:' || My_rec.Person_Id ||  ',员工编号:' || My_rec.Emp_Number || ',员工姓名:' || My_rec.Emp_Name );
  Exit when my_cursor%notfound;
  End loop;
  Close my_cursor;
END;
2.方法二:
Declare
  Cursor my_cursor is select r.person_id,r.emp_number,r.emp_name from hr_user r where rownum between 1 and 10;
Begin
  For My_rec in My_cursor loop
    DBMS_OUTPUT.PUT_LINE('员工号:' || My_rec.Person_Id ||  ',员工编号:' || My_rec.Emp_Number || ',员工姓名:' || My_rec.Emp_Name );
  end loop;
END;
3.方法三:
Declare
  Cursor My_cursor is select r.person_id,r.emp_number,r.emp_name from hr_user r where rownum between 1 and 10;
  My_rec my_cursor%rowtype;
Begin
   Open my_cursor;
   Fetch My_cursor into My_rec;
   While (my_cursor%found) loop
    DBMS_OUTPUT.PUT_LINE('员工号:' || My_rec.Person_Id ||  ',员工编号:' || My_rec.Emp_Number || ',员工姓名:' || My_rec.Emp_Name );
   Fetch My_cursor into My_rec;
   End Loop;
END;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值