Listagg函数和ORA-01489:字符串连接的结果太长

当我运行以下查询时:

Select tm.product_id, listagg(tm.book_id || '(' || tm.score || ')',',') within group (order by tm.product_id) as matches from tl_product_match tm where tm.book_id is not null group by tm.product_id 

Oracle返回以下错误:

  ORA-01489: result of string concatenation is too long 

我知道失败的原因是listagg函数试图连接大于4000个字符的值,这些值不受支持。

我已经看到了这里描述的替代示例 – http://www.oracle-base.com/articles/misc/string-aggregation-techniques.php但它们都需要使用函数或过程。

是否有一个纯SQL的解决方案,而无需调用函数或存储过程,并且能够使用标准JDBC读取值?

我遇到的另一个困难是我见过的大多数字符串聚合示例都显示了如何按原样读取值的示例。 在我的例子中我首先修改了值(即我正在聚合两列)。

您可以使用xml函数来执行返回CLOB的操作。 JDBC应该没问题。

 select tm.product_id, rtrim(extract(xmlagg(xmlelement(e, tm.book_id || '(' || tm.score || '),')), '/E/text()').getclobval(), ',') from tl_product_match tm where tm.book_id is not null group by tm.product_id; 

例如: http : //sqlfiddle.com/#!4 / 083a2 / 1

为什么不使用嵌套表?

 set echo on; set display on; set linesize 200; drop table testA; create table testA ( col1 number, col2 varchar2(50) ); drop table testB; create table testB ( col1 number, col2 varchar2(50) ); create or replace type t_vchar_tab as table of varchar2(50); insert into testA values (1,'A'); insert into testA values (2,'B'); insert into testB values (1,'X'); insert into testB values (1,'Y'); insert into testB values (1,'Z'); commit; -- select all related testB.col2 values in a nested table for each testA.col1 value select a.col1, cast(multiset(select b.col2 from testB b where b.col1 = a.col1 order by b.col2) as t_vchar_tab) as testB_vals from testA a; -- test size > 4000 insert into testB select 2 as col1, substr((object_name || object_type), 1, 50) as col2 from all_objects; commit; -- select all related testB.col2 values in a nested table for each testA.col1 value select a.col1, cast(multiset(select b.col2 from testB b where b.col1 = a.col1 order by b.col2) as t_vchar_tab) as testB_vals from testA a; 

我不是Java专家,但这已经有一段时间了,我确信java可以从嵌套表中提取值。 并且,无需在另一端标记一些分隔的字符串。

我已经看到了这里描述的替代示例 – http://www.oracle-base.com/articles/misc/string-aggregation-techniques.php但它们都需要使用函数或过程。

不,他们没有。 向下滚动,您将看到几个不需要pl / sql的选项。