如何将值附加到oracle类型

Pat*_*ick 2 oracle plsql user-defined-types

如何将3或4个不同的值附加(插入)到oracle类型,然后再打开它以获取游标.

例如(伪):

insert into mytype select 1 from dual;
insert into mytype select 3 from dual;
insert into mytype select 5 from dual;

open cursor_1 for select * from table(mytype);
Run Code Online (Sandbox Code Playgroud)

这是可以在pl/sql中做到的吗?

我知道这是微不足道的,可以组合成一个查询,但我真正的需要是有不同的查询并继续将结果附加到mytype.

Dav*_*sta 5

假设你的意思是你有一个自定义的SQL类型(可能是嵌套的表类型),以及那种类型的PL/SQL变量:我不相信你可以INSERT进入它,我认为你不能SELECT以某种方式进入它将附加到该集合.

您可以选择标量变量,然后在程序上将其附加到集合中.

SQL> create type mytype as table of integer;
  2  /

Type created.

SQL> set serveroutput on
SQL> l
  1  declare
  2    mytable  mytype := mytype();
  3    cursor_1 sys_refcursor;
  4    x  integer;
  5    procedure append_to_table( t IN OUT mytype, y IN INTEGER)
  6      is
  7      begin
  8        t.extend();
  9        t(t.COUNT) := y;
 10      end append_to_table;
 11  begin
 12    select 1 into x from dual;
 13    append_to_table( mytable, x );
 14    select 3 into x from dual;
 15    append_to_table( mytable, x );
 16    select 5 into x from dual;
 17    append_to_table( mytable, x );
 18    open cursor_1 for select * from table(cast(mytable as mytype));
 19    fetch cursor_1 into x;
 20    dbms_output.put_line(x);
 21    fetch cursor_1 into x;
 22    dbms_output.put_line(x);
 23    fetch cursor_1 into x;
 24    dbms_output.put_line(x);
 25    close cursor_1;
 26* end;
SQL> /
1
3
5

PL/SQL procedure successfully completed.
Run Code Online (Sandbox Code Playgroud)