过程返回记录集:
create
or
replace
package
pkg_test
as
type
myrctype
is
ref
cursor;
procedure
get
(p_id
number,
p_rc
out
myrctype);
end
pkg_test;
/
create
or
replace
package
body
pkg_test
as
procedure
get
(p_id
number,
p_rc
out
myrctype)
is
sqlstr
varchar2
(500);
begin
if
p_id
=
0
then
open
p_rc
for
select
id,
name,
sex,
address,
postcode,
birthday
from
student;
else
sqlstr
:=
'select
id,name,sex,address,postcode,birthday
from
student
where
id=:w_id';
open
p_rc
for
sqlstr
using
p_id;
end
if;
end
get;
end
pkg_test;
/
函数返回记录集:
建立带ref
cursor定义的包和包体及函数:
create
or
replace
package
pkg_test
as
/*
定义ref
cursor类型
不加return类型,为弱类型,允许动态sql查询,
否则为强类型,无法使用动态sql查询;
*/
type
myrctype
is
ref
cursor;
--函数申明
function
get(intid
number)
return
myrctype;
end
pkg_test;
/
create
or
replace
package
body
pkg_test
as
--函数体
function
get(intid
number)
return
myrctype
is
rc
myrctype;
--定义ref
cursor变量
sqlstr
varchar2(500);
begin
if
intid=0
then
--静态测试,直接用select语句直接返回结果
open
rc
for
select
id,name,sex,address,postcode,birthday
from
student;
else
--动态sql赋值,用:w_id来申明该变量从外部获得
sqlstr
:=
'select
id,name,sex,address,postcode,birthday
from
student
where
id=:w_id';
--动态测试,用sqlstr字符串返回结果,用using关键词传递参数
open
rc
for
sqlstr
using
intid;
end
if;
return
rc;
end
get;
end
pkg_test;
/
如果你是使用PL/sql工具,在command
窗口下执行set
serveroutput
on
然后exec
sp;
可以看到了
或者在sqlplus
中执行上面的代码