我有以下类型的 PL/SQL 集合
type p_typ_str_tab is table of varchar2(4000) index by pls_integer;
我想使用简单的内联函数(例如LISTAGG
)将值聚合到单个字符串中,而无需编写任何自定义函数或for循环。 LISTAGG
的所有示例均未展示如何使用 PL/SQL 集合。我使用的是 Oracle 11g R2。这可能吗?
最佳答案
为了能够对集合使用LISTAGG
函数,必须将集合声明为嵌套表而不是关联数组,并且必须将其创建为sql类型(架构对象),因为不可能在 select 语句中使用 pl/sql 类型。为此,您可以执行以下操作:
--- create a nested table type
SQL> create or replace type t_tb_type is table of number;
2 /
Type created
--- and use it as follows
SQL> select listagg(column_value, ',') within group(order by column_value) res
2 from table(t_tb_type(1,2,3)) -- or call the function that returns data of
3 / -- t_tb_type type
RES
-------
1,2,3
否则循环
是你唯一的选择。
关于oracle - Listagg 函数与 PLSQL 集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13979024/