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

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

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

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;


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值