oracle 存储过程使用游标返回结果集和游标的两种for循环

游标定义和使用需要4个步骤:

1、声明游标     myCur Sys_Refcursor;

2、打开游标     open myCur for select * from students;

3、提取数据    可以使用fetch也可以使用for循环;

4、关闭游标    close myCur;

fetch myCur into name,age;

exit when myCur%NOTFOUND;
    if myCur%found then
      dbms_output.put_line('读取的数据:学生姓名为:' || name || '元, 年龄为: ' ||age);
    end if;
  end loop;

使用for循环:

for myCur_record in myCur loop

dbms_output.put_line('读取的数据:学生姓名为:' || name || '元, 年龄为: ' ||age);
    end loop; 

此时myCur_record 为隐含定义的记录变量,循环的执行次数与游标取得的数据的行数一致。

用于游标判断的4个属性:

1. %ROWCOUNT   整型  获得FETCH语句返回的数据行数    

2. %FOUND  布尔型 最近的FETCH语句返回一行数据则为真,否则为假   

3. %NOTFOUND   布尔型 与%FOUND属性返回值相反   

4.  %ISOPEN 布尔型 游标已经打开时值为真,否则为假 。

使用sys_refcursor游标返回记录集:

create or replace procedure calprice(resCur out Sys_Refcursor) is

  myCur Sys_Refcursor;

begin
   open myCur for
    select * from student;

 resCur := myCur;

此时将结果集保存在resCur游标中,其他函数或存储过程调用的时候再使用fetch或for语句提取记录。

原文地址:https://www.cnblogs.com/sindyhua/p/3016876.html