获取 oracle 存储过程执行得到的 数据集

PL/SQL的存储过程和函数均不直接提供返回数据集的方法,但可以通过返回类型对象来实现返回数据集的效果。具体思路为:

(1)创建包头,在其中声明类型和存储过程,类型应该引用游标;

(2)创建包体,定义存储过程,执行查询,将查询得到的结果集以游标句柄的形式返回。

说明:游标分为显示游标和隐式游标,任何查询结果默认都提供隐式游标,当前方案的重点在于使用游标取得查询结果的句柄,由于游标被引用为类型,通过返回类型,即等效于(并不等同)返回数据集。

    具体的案例如下:

--创建包头

create or replace package mypackage1

as

  --声明类型,并引用游标

  type cursorType is ref cursor;

  --声明存储过程,两个输入参数,一个输出参数,输出游标类型数据

  procedure prcGetGlobalAddress

  (

    pos1 integer,                      --分页查询的下限

    pos2  integer,                     --分页查询的上限

    cur in out mypackage1.cursorType     --输出参数,数据类型为引用游标的类型

  );

end mypackage1;

--创建包体

create or replace package body mypackage1                  

as  

    --定义存储过程

  procedure prcGetGlobalAddress

  (

    pos1 integer,

    pos2  integer,

    cur in out mypackage1.cursorType

  )

  as

  begin

    --返回得到分页查询结果集的游标句柄

    open cur for 

      select * 

      from (  select a.*,rownum rn 

              from (  select * 

                      from tblsys_globaladdress) a 

              where rownum<=pos2) 

      where rn > =pos1;

  end prcGetGlobalAddress;

  

end mypackage1;

猜你喜欢

转载自weigang-gao.iteye.com/blog/2075558