oracle - Listagg 函数与 PLSQL 集合

标签 oracle collections plsql oracle11g aggregate-functions

我有以下类型的 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/

相关文章:

java - 如何用Java连接RMAN?

sql - 使用 DISTINCT 关键字会导致此错误 : not a SELECTed expression

java - 在 Java 中将 Vector 转换为 HashSet

java - BoundedFifoBuffer 和 CircularFifoBuffer 的区别?

java - 如何对 Java Hashmap 中的值求和

oracle - 在for循环中调用存储过程

oracle - PLW-07202 : bind type would result in conversion away from column type

"WITH"子句的 SQL 语法错误

Java ResultSet 未正确关闭

regex - 从Oracle Varchar2查找和删除非ASCII字符