我有一个plsql记录类型mrec,而mreclist是该记录的集合。我想知道是否可以通过一个语句将每条记录添加到mreclist中。或者是否有另一种有效的方法来做同样的事情。
declare
type mrec is record ( a varchar2(10),b varchar2(20));
type mreclist is table of mrec;
r mrec;
rlist mreclist;
begin
rlist:=mreclist();
--insert value
select 'dummy1','dummy2' into r.a,r.b from dual;
--how to copy the above value into the mreclist with one single statement instead of the following statements.
rlist.Extend(1);
rlist(1).a:=r.a;
rlist(1).b:=r.b;
select 'dummy3','dummy4' into r.a,r.b from dual;
rlist.Extend(1);
rlist(2).a:=r.a;
rlist(2).b:=r.b;
end;
答案 0 :(得分:1)
您是否正在寻找BULK COLLECT
和MULTISET
operators的组合?
类似的东西:
declare
type mrec is record ( a varchar2(10),b varchar2(20));
type mreclist is table of mrec;
r mrec;
rlist mreclist;
tlist mreclist; -- <-- we need two collections here
begin
select 'dummy1','dummy2' bulk collect into rlist from dual;
-- collect initial data into the collection `rlist`
select 'dummy3','dummy4' bulk collect into tlist from dual;
-- collect next data into the other collection `tlist`
rlist := rlist multiset union all tlist;
-- merge `rlist` and `tlist`. Replace the collection `rlist` by the result.
我不会多说这些代码的效率。它可能非常依赖于您的具体用例。但请注意,在某些时候,您同时将rlist
和结合的结果保存在内存中。对于大型馆藏来说,这可能是令人望而却步的。
对于非常基本的用例,您可能只需要BULK COLLECT
和一个简单的UNION ALL
:
select * bulk collect into rlist from (
select 'dummy1','dummy2' from dual
union all select 'dummy3','dummy4'from dual
);
最后,我建议您查看"Taking Up Collections" by Steven Feuerstein以了解多集操作的结果顺序。只是引用几句话:
Oracle文档指出嵌套表和变量数组之间的区别在于嵌套表列中存储的数据不保留其顺序,而该顺序保留在变量数组中。在Oracle数据库10g之前,这种区别在PL / SQL世界中没有多大意义。现在,使用set运算符,multiset或嵌套表的特殊特性可以非常清楚地显示出来。