Solving the "String concatenation result is too long" error in Oracle's LISTAGG function
An alternative is needed when the LISTAGG function encounters a large amount of data and throws a "String concatenation result is too long" error.
Solution of XMLAGG function
In order to achieve similar functionality to LISTAGG, Oracle's XMLAGG function can be used. The XMLAGG function aggregates data into an XML document, allowing concatenated values to exceed the LISTAGG limit. The following syntax demonstrates how to use XMLAGG:
<code class="language-sql">SELECT RTRIM(XMLAGG(XMLELEMENT(E,colname,',').EXTRACT('//text()') ORDER BY colname).GetClobVal(),',') AS LIST FROM tablename;</code>
Advantages of XMLAGG
The XMLAGG function has the following advantages:
Example usage
Consider the following code snippet:
<code class="language-sql">SELECT SESSION_DT, C_IP, CS_USER_AGENT, listagg(WEB_LINK, ' ') WITHIN GROUP(ORDER BY C_IP, CS_USER_AGENT) "WEB_LINKS" FROM webviews GROUP BY C_IP, CS_USER_AGENT, SESSION_DT ORDER BY SESSION_DT</code>
To resolve errors encountered, LISTAGG can be replaced with XMLAGG:
<code class="language-sql">SELECT SESSION_DT, C_IP, CS_USER_AGENT, RTRIM(XMLAGG(XMLELEMENT(E,WEB_LINK,',').EXTRACT('//text()') ORDER BY WEB_LINK).GetClobVal(),',') AS "WEB_LINKS" FROM webviews GROUP BY C_IP, CS_USER_AGENT, SESSION_DT ORDER BY SESSION_DT</code>
The above is the detailed content of How to Overcome Oracle's 'Result of String Concatenation is Too Long' Error When Using LISTAGG?. For more information, please follow other related articles on the PHP Chinese website!