【问题标题】:How to open a sys_refcursor with values from a normal cursor?如何使用来自普通游标的值打开 sys_refcursor?
【发布时间】:2020-11-04 14:14:57
【问题描述】:

我可以用普通光标的值打开sys_refcursor吗?

create or replace procedure test(C1 out sys_refcursor)
  Lv_c1 as
    Select * from table;
Begin
  Open C1 for select * from lv_c1;
End;

【问题讨论】:

  • 没有。显式游标和引用游标(游标变量)是两个不同但相关的结构。两者都是“正常的”。
  • 为什么需要它?你想达到什么目的?
  • 如果提供的答案对您没有帮助,请发表评论,否则请投票/接受:)

标签: oracle plsql cursor


【解决方案1】:

不,你不能。 “Normal”游标是PL/SQL变量,不能用于SQL查询。

但是可以为游标变量的结果集打开游标:

create or replace package pack as 
    cursor cur is 
        select rownum attr_1 from dual connect by level<=3;
    type rset is table of cur%rowtype;     
    procedure getCursor (rc out sys_refcursor);
end;
/
create or replace package body pack as 
    procedure getCursor (rc out sys_refcursor) is
        rs rset; 
    begin
        open cur;
        fetch cur bulk collect into rs;
        close cur;    
        open rc for select * from table (rs);
    end;
end;
/

执行和结果:

var rc refcursor
exec pack.getCursor (:rc)  

ATTR_1
--------
row1
row2
row3

【讨论】:

  • 我的游标有 15 列,当我得到结果时,它显示 ATTR_1 到 ATTR_15,而不是游标中的原始列名。有没有办法保留列名而不是再次为每一列起别名。
  • 在 18c、19c 中是 known issue。作为解决方法,您必须使用表格或视图,例如 my_view%rowtype;
  • @RajA QA on so.
猜你喜欢
  • 2012-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-14
  • 1970-01-01
  • 1970-01-01
  • 2018-09-10
  • 1970-01-01
相关资源
最近更新 更多