我该如何从%ROWTYPE表中选择要反射的数据?

问题描述

declare
    cursor cur0 is Select no,date from data123;
    TYPE TempTabTyp0 is table of cur0%ROWTYPE index by pls_integer;
    temp_tab0 TempTabTyp0; 
    lrc sys_refcursor; 
BEGIN
    open cur0;
    fetch cur0 bulk collect into temp_tab0;
    close cur0;
    for rec in 1 .. temp_tab0.count loop 
    temp_tab0(rec).no := 'new value' || temp_tab0(rec).no;
    end loop;  

    open lrc for select * from table(temp_tab0);
END;

如何将数据从temp_tab0发送到sys_refcursor(或另一个游标)?

解决方法

只需将您的代码包装在一个包中。像这样:

create table data123 (no,dt) as
    select cast ('abc' as varchar2 (30)),date'2020-09-20' from dual
/
create or replace package pack1 as
    cursor cur0 is select no,dt from data123;
    type TempTabTyp0 is table of cur0%rowtype index by pls_integer;
    procedure getcur (rc out sys_refcursor);
end;
/
create or replace package body pack1 as
    procedure getcur (rc out sys_refcursor) is 
        temp_tab0 TempTabTyp0;
    begin
        open cur0;
        fetch cur0 bulk collect into temp_tab0;
        close cur0;
        for i in 1..temp_tab0.count loop 
            temp_tab0(i).no := 'new value '||temp_tab0(i).no;
        end loop;
        open rc for select * from table (temp_tab0);
    end;
end;
/

执行和结果:

var rc refcursor
exec pack1.getcur (:rc)

ATTR_1                         ATTR_2             
------------------------------ -------------------
new value abc                  2020-09-20 00:00:00

不正确的输出标题的PS,请参见this topic

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...