如何用 refcursor 返回结果集

可以通过返回 Refcursor 类型的函数,或者out 类型的函数或 procedure 返回结果集。

一、返回refcursor 类型的函数

create or replace function f_get_set(refcursor,refcursor)
returns setof refcursor
as $$
begin
  open $1 for select * from t1;
  return next $1;
  open $2 for select * from t1;
  return next $2;
end;
$$ language plsql;

begin;
  select * from get_set('a'::refcursor,'b'::refcursor);
  fetch all from a;
end

二、通过OUT类型的函数参数

create or replace function func_get_set_out(out cur_a refcursor,out cur_b refcursor)
as $$
begin
  open cur_a for select * from t1;
  open cur_b for select * from t1;
end;
$$ language plpgsql;

declare
  v_ref_cur1 refcursor;
  v_ref_cur2 refcursor;
  v_t1 t1%rowtype;
begin
  select * from get_set_out() into v_ref_cur1,v_ref_cur2;
  loop
    fetch v_ref_cur1 into v_t1;
    exit when v_ref_cur1%NOTFOUND;
    raise notice '%',v_t1.oid;
  end loop;
  loop
    fetch v_ref_cur2 into v_t1;
    exit when v_ref_cur2%NOTFOUND;
    raise notice '%',v_t1.oid;
  end loop;  
end;

三、通过OUT类型的过程参数

create or replace procedure proc_get_set_out(inout cur_a refcursor,inout cur_b refcursor)
as $$
begin
  open cur_a for select * from t1;
  open cur_b for select * from t1;
end;
$$ language plpgsql;

declare
  v_ref_cur1 refcursor;
  v_ref_cur2 refcursor;
  v_t1 t1%rowtype;
begin
  call proc_get_set_out(v_ref_cur1,v_ref_cur2);
  loop
    fetch v_ref_cur1 into v_t1;
    exit when v_ref_cur1%NOTFOUND;
    raise notice '%',v_t1.oid;
  end loop;
  loop
    fetch v_ref_cur2 into v_t1;
    exit when v_ref_cur2%NOTFOUND;
    raise notice '%',v_t1.oid;
  end loop;  
end;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值