The aggregation isn't the problem; the error is coming when you try to trim off the trailing comma you're left with.
You are getting an implicit conversion of your XMLAgg result, which is an XMLType object, to varchar2; and when its length exceeds 4000 characters you will get this error as that is the maximum length of a varchar2 value in SQL (at least, until Oracle 12c).
You need to explicitly get the value as a CLOB before calling rtrim()
, using getclobval()
:
select Rtrim(
(Xmlagg(Xmlelement(e,wonum||',')).extract('//text()')).getclobval(),
',') as wolist
from ( select w.wonum from workorder w
connect by prior w.wonum = w.parent and prior w.siteid = siteid
start with w.siteid = 'ABCD' and w.wonum = 'P1234' );
You could also define your own aggregate function that can return a CLOB and handle more than 4000 characters; that could then be called more like listagg()
, without the XML workaround.