对于单个记录,试试这个:
declare
type fulfillment_set_id_tbl is table of number;
type line_rec is record
( line_id number
, fulfillment_set_id fulfillment_set_id_tbl );
r line_rec;
begin
r.line_id := 1;
r.fulfillment_set_id := fulfillment_set_id_tbl(10,11,12);
end;
对于行记录表:
declare
type fulfillment_set_id_tbl is table of number;
type line_rec is record
( line_id number
, fulfillment_set_id fulfillment_set_id_tbl );
type line_rec_tbl is table of line_rec;
r line_rec;
line_recs line_rec_tbl := new line_rec_tbl();
begin
r.line_id := 1;
r.fulfillment_set_id := fulfillment_set_id_tbl(10,11,12);
line_recs.extend;
line_recs(1) := r;
end;
从 Oracle 18c 中,我们获得了记录类型的伪构造函数(称为 'Qualified Expressions'),因此您可以以声明方式填充它们:
declare
type fulfillment_set_id_tbl is table of number;
type line_rec is record
( line_id number
, fulfillment_set_id fulfillment_set_id_tbl );
type line_rec_tbl is table of line_rec;
line_recs line_rec_tbl :=
new line_rec_tbl
( line_rec(1, fulfillment_set_id_tbl(10,11,12))
, line_rec(2, fulfillment_set_id_tbl(13,14,14))
);
begin
dbms_output.put_line(line_recs(2).fulfillment_set_id(3));
end;
输出:
14
如果您要在 SQL 中将类型创建为独立对象,那么您可以更加灵活地使用自定义构造函数,并能够在查询中使用它们。
顺便说一句,问题标题是指集合的集合,但这里的主要问题似乎是 PL/SQL 记录。希望这已经回答了这两个问题,但如果还没有,请告诉我。
要实现cmets中提到的bulk collect,需要在SQL中定义一个集合类型,例如:
create or replace type number_tt as table of number;
那么它应该可以工作了:
declare
type line_rec is record
( line_id number
, fulfillment_set_id number_tt );
type line_rec_tbl is table of line_rec;
line_recs line_rec_tbl;
begin
with demo (line_id, fulfillment_set_id) as
( select 1, 10 from dual union all
select 1, 11 from dual union all
select 1, 12 from dual union all
select 2, 13 from dual union all
select 2, 14 from dual union all
select 2, 15 from dual )
select line_id, cast(collect(fulfillment_set_id) as number_tt)
bulk collect into line_recs
from demo
group by line_id;
dbms_output.put_line('line_recs contains ' || line_recs.count || ' records');
end;
输出:
line_recs contains 2 records