oracle的存储过程返回记录集,关键之处是要用游标。关于数据库的游标(cursor)大家肯定都接触不少,我们可以通过open,fetch,close
oracle的存储过程返回记录集,关键之处是要用游标。
关于数据库的游标(cursor)大家肯定都接触不少,我们可以通过open,fetch,close操作控制游标进行各种方便的操作,这方面的例子我就不在重复了。我们现在要介绍的是游标变量(cursor variable)。类似游标,游标变量也是指向一个查询结果集的当前行。不同的是,游标变量能为任何类型相似(type-compatible)的查询打开,而并不是绑定到某一个特定的查询。通过游标变量,你可以在数据库的数据提取中获得更多的方便。
首先是建立表。
create table lihuan.bill_points
(
points_id number(10,0) not null,
customer_id number(10,0) not null,
bill_point_no number(2,0) default 1 not null,
constraint pk_bill_points primary key (points_id)
)
/
其次,,建package。
create or replace package lihuan.yy_pkg_bill_point_no/*取得用户的所有计费电序号*/
is
type t_cursor is ref cursor;
procedure bill_point_no(p_customer_id bill_points.customer_id%type,
re_cursor out t_cursor);
end;
/
再次,建package body。
create or replace package body lihuan.yy_pkg_bill_point_no/*取得用户的所有计费电序号*/
is
procedure bill_point_no(p_customer_id bill_points.customer_id%type,
re_cursor out t_cursor)
is
v_cursor t_cursor;
begin
open v_cursor for
select bill_point_no from bill_points where customer_id =p_customer_id;
re_cursor := v_cursor;
end;
end;
/