sys_refcursor的用法

本文介绍如何通过使用sys_refcursor解决Oracle PL/SQL过程中返回动态SQL游标的问题。通过创建两个存储过程,一个用于返回游标,另一个用于消费此游标,解决了weakcursor不可返回的限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现目的:在开发中我需要在Procedure A中打开一个动态SQL的游标,并返回此cursor;然后用Procedure B去call A存储过程,再对返回的cursor进行操作;

遇到的问题:因为用游标打开动态SQL时,此游标就不可以定义为strong cursor,而只能使用weak cursor;同时在定义返回参数时不能将其定义为weak cursor,从而产生了问题,总是提示PL/SQL-00455错误

解决问题:使用sys_refcursor定义返回参数

示例:

create or replace procedure return_cursor(order_no_in number,out_cursor sys_refcursor)
is
  v_sql varchar2(1000);
begin
  v_sql :='select price from order where order_no='order_no_in;
  open out_cursor for v_sql;
end;

create or replace procedure call_cursor(order_no_in number)
is
v_order_no order.price%type;
v_cursor  sys_refcursor;
begin
 return_cursor(order_no_in,v_cursor);
  loop
   fetch v_cursor into v_order_no;
   exit when v_cursor%notfound;
     insert into order_bak values (v_order_no.price);
     commit;
  end loop;
 close v_cursor;
end;

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/13129975/viewspace-614101/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/13129975/viewspace-614101/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值