您的位置:首页 > 数据库 > Oracle

oracle 在一个存储过程中调用另一个返回游标的存储过程

2011-12-14 16:20 489 查看
        实际项目当中经常需要在一个存储过程中调用另一个存储过程返回的游标,本文列举了两种情况讲述具体的操作方法。

第一种情况:返回的游标是某个具体的表或视图的数据

create or replace procedure p_testa(presult out sys_refcursor) as
begin
open presult for
select * from users;
end p_testa;
其中 users 就是数据库中一个表(或视图)。在调用的时候只要声明一个该表的rowtype类型就可以了:
create or replace procedure p_testb as
temp_cur sys_refcursor;
r        users%rowtype;
begin
p_testa(temp_cur);
loop
fetch temp_cur
into r;
exit when temp_cur%notfound;
dbms_output.put_line(r.name);
end loop;
end p_testb;


第二种情况:我们返回的不是表的所有的列,或许只是其中一列或两列 

create or replace procedure p_testa(presult out sys_refcursor) as
begin
open presult for
select id, name from users;
end p_testa;
这里我们只返回了 users 表的 id, name 这两个列,那么调用的时候也必须做相应的修改: 
create or replace procedure p_testb as
temp_cur sys_refcursor;
cursor cur_1 is
select id, name from users where rownum = 1;
r cur_1%rowtype;
begin
p_testa(temp_cur);
loop
fetch temp_cur
into r;
exit when temp_cur%notfound;
dbms_output.put_line(r.id);
end loop;
end p_testb;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  存储 oracle 数据库