Oracle中返回结果集的存储过程分享
程序员文章站
2023-12-05 08:10:46
oracle不像sqlserver那样在存储过程中用select就可以返回结果集,而是通过out型的参数进行结果集返回的。实际上是利用ref cursor 复制代码 代码如...
oracle不像sqlserver那样在存储过程中用select就可以返回结果集,而是通过out型的参数进行结果集返回的。实际上是利用ref cursor
--procedure返回记录集:
----------------------声明一个package--------------
create or replace package pkg_test
as
typemyrctypeis ref cursor;
procedure get_r(p_id number,p_rc out myrctype); --package中声明名为get 的procedure(只有接口没内容)
end pkg_test;
-----------------声明package body,即上面package中的内容,包括procedure get---------------------
create or replace package body pkg_test
as
procedure get_r(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'; --w_id是个参数,
--以下 p_rc是个ref cursor游标类型,而且是out型参数,即可返回一个记录集了。using p_id就是替换上面sql中:w_id值拉:)
open p_rc for sqlstr using p_id;
end if;
end get;
end pkg_test;
--function返回记录集的例子,原理和上面相同,而是用function的return值来返回记录集。
函数返回记录集:
建立带ref cursor定义的包和包体及函数:
create or replace
package pkg_test as
type myrctype is ref cursor;
function get_r(intid number) return myrctype;
end pkg_test;
/
create or replace
package body pkg_test as
--函数体
function get_r(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;
复制代码 代码如下:
--procedure返回记录集:
----------------------声明一个package--------------
create or replace package pkg_test
as
typemyrctypeis ref cursor;
procedure get_r(p_id number,p_rc out myrctype); --package中声明名为get 的procedure(只有接口没内容)
end pkg_test;
-----------------声明package body,即上面package中的内容,包括procedure get---------------------
create or replace package body pkg_test
as
procedure get_r(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'; --w_id是个参数,
--以下 p_rc是个ref cursor游标类型,而且是out型参数,即可返回一个记录集了。using p_id就是替换上面sql中:w_id值拉:)
open p_rc for sqlstr using p_id;
end if;
end get;
end pkg_test;
--function返回记录集的例子,原理和上面相同,而是用function的return值来返回记录集。
函数返回记录集:
建立带ref cursor定义的包和包体及函数:
复制代码 代码如下:
create or replace
package pkg_test as
type myrctype is ref cursor;
function get_r(intid number) return myrctype;
end pkg_test;
/
create or replace
package body pkg_test as
--函数体
function get_r(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;
上一篇: Sqlserver 表类型和表变量介绍