How to tweak LISTAGG to support more than 4000 character in select query?

馋奶兔 提交于 2019-11-28 00:18:51

if you cant create types (you can't just use sql*plus to create on as a one off?), but you're OK with COLLECT, then use a built-in array. There's several knocking around in the RDBMS. run this query:

select owner, type_name, coll_type, elem_type_name, upper_bound, length 
 from all_coll_types
 where elem_type_name = 'VARCHAR2';

e.g. on my db, I can use sys.DBMSOUTPUT_LINESARRAY which is a varray of considerable size.

select department, 
       cast(collect(name) as sys.DBMSOUTPUT_LINESARRAY) 
  from emp 
 group by department;
Ankur Bhutani

You should add GetClobVal and also need to rtrim as it will return delimiter in the end of the results.

SELECT RTRIM(XMLAGG(XMLELEMENT(E,colname,',').EXTRACT('//text()') 
  ORDER BY colname).GetClobVal(),',') from tablename;

A derivative of @anuu_online but handle unescaping the XML in the result.

dbms_xmlgen.convert(xmlagg(xmlelement(E, name||',')).extract('//text()').getclobval(),1)

I end up in another approach using the XMLAGG function which doesn't have the hard limit of 4000.

select department,
XMLAGG(XMLELEMENT(E,name||',')).EXTRACT('//text()')  
from emp 
group by department;

For IBM DB2, Casting the result to a varchar(10000) will give more than 4000.

select column1, listagg(CAST(column2 AS VARCHAR(10000)), x'0A') AS "Concat column"...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!