我在oracle中执行以下查询,出现以下错误
ORA-19011:字符串缓冲区太小
select Rtrim(Xmlagg (Xmlelement (e, wonum || ',')).extract ( '//text()' ), ',')
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' )
我从未使用过Xmlagg / Xmlelement因此我不确定是什么问题。执行内部查询时,输出将如下所示
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'
wonum parent
P1234
5678 P1234
9999 5678
8888 9999
依旧......
答案 0 :(得分:2)
聚合不是问题;当你试图修剪你留下的尾随逗号时,错误即将来临。
您正在将XMLAgg结果(XMLType对象)隐式转换为varchar2;当它的长度超过4000个字符时,您将收到此错误,因为这是SQL中varchar2值的最大长度(至少在Oracle 12c之前)。
在使用rtrim()
调用getclobval()
之前,您需要明确地将值作为CLOB获取:
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' );
您还可以定义your own aggregate function,它可以返回CLOB并处理超过4000个字符;然后可以更像listagg()
调用,而不需要XML解决方法。