Oracle - ORA-01489:字符串连接的结果太长 [重复]

Posted

技术标签:

【中文标题】Oracle - ORA-01489:字符串连接的结果太长 [重复]【英文标题】:Oracle - ORA-01489: result of string concatenation is too long [duplicate] 【发布时间】:2015-06-28 20:09:46 【问题描述】:

创建了一个视图,其中一个字段(共 6 个)是源表中两个字段的串联。创建了第二个视图,该视图使用 listagg 潜在地组合连接的结果。使用这两个步骤操作正确返回源表中两个字段的 listagg 连接。

当我尝试创建一个同时生成连接和列表的视图时,我收到 oracle 错误 ora-01489 字符串连接的结果太长。

作为测试,我选择了 max(length(concatenated field) 并返回 837。因此,所述错误似乎是错误的。

所以它必须在语法中。我尝试了 rtrim、trim 甚至 substr,但无法让 view 语句同时编译 listagg(concatenation) 和返回数据。

我能够开发正确返回数据的两个视图表明我已经掌握了基本语法,但是将串联与 listagg 函数相结合的任务我还没有弄清楚。

在另一种情况下,我已经能够在一个语句中将连接与 listagg 函数结合起来:

    LISTAGG (STATEMENTS || ' - ' || BIRTH_DATE, ';  ')
                   WITHIN GROUP (ORDER BY STATEMENTS || ' - ' || BIRTH_DATE)
                   AS GROWING_UP

此语法正确返回所需的数据............

但在上述情况下,类似的语法会产生 Ora-01489 错误。

知道有时 Oracle 错误可能会产生误导,我想知道是否有任何机上专家知道除了 Oracle 可能抛出此错误之外的任何原因?

LISTAGG ((NUMBER || '-' || text), ',') WITHIN GROUP (ORDER BY (NUMBER || '-' || text))
AS
  restrictions FROM source

返回错误

【问题讨论】:

你为什么不测试 SUM(length(concatenated field)) ? :) 总和与最大值不同;单个串联字段的最长长度是 837,但这并不能告诉您其中几个单独的串联字段的 listagg 可能有多长。我认为这是 OP 遇到的问题。对于重现此问题的 SQL Fiddle 或至少产生错误的代码(不仅仅是有效的代码)很有用。 max 正在读取 listagg(concatenated) 字段,因此应该与任何一个串联 listagg(ed) 字段一样长。 编辑了原帖 您的措辞具有误导性,“listagg to ...组合连接的结果”听起来您的最大值是用于内部位,而不是组合字符串。你知道你的意思,但我们不一定。您仍然没有显示完整的代码,两个有效的视图和无效的部分。根据您所说的,我猜您在组合版本中的分组错误。但是,如果没有所有信息,就无法真正为您提供帮助。 【参考方案1】:

您超出了 4000 字节的 SQL 限制,这也适用于 LISTAGG

SQL> SELECT listagg(text, ',') WITHIN GROUP (
  2  ORDER BY NULL)
  3  FROM
  4    (SELECT to_char(to_date(level,'j'), 'jsp') text FROM dual CONNECT BY LEVEL < 250
  5    )
  6  /
SELECT listagg(text, ',') WITHIN GROUP (
*
ERROR at line 1:
ORA-01489: result of string concatenation is too long

作为一种解决方法,您可以使用 XMLAGG

例如,

SQL> SET LONG 2000000
SQL> SET pagesize 50000
SQL> SELECT rtrim(xmlagg(XMLELEMENT(e,text,',').EXTRACT('//text()')
  2                     ).GetClobVal(),',') very_long_text
  3  FROM
  4    (SELECT to_char(to_date(level,'j'), 'jsp') text FROM dual CONNECT BY LEVEL < 250
  5    )
  6  /

VERY_LONG_TEXT
--------------------------------------------------------------------------------
one,two,three,four,five,six,seven,eight,nine,ten,eleven,twelve,thirteen,fourteen
,fifteen,sixteen,seventeen,eighteen,nineteen,twenty,twenty-one,twenty-two,twenty
-three,twenty-four,twenty-five,twenty-six,twenty-seven,twenty-eight,twenty-nine,
thirty,thirty-one,thirty-two,thirty-three,thirty-four,thirty-five,thirty-six,thi
rty-seven,thirty-eight,thirty-nine,forty,forty-one,forty-two,forty-three,forty-f
our,forty-five,forty-six,forty-seven,forty-eight,forty-nine,fifty,fifty-one,fift
y-two,fifty-three,fifty-four,fifty-five,fifty-six,fifty-seven,fifty-eight,fifty-
nine,sixty,sixty-one,sixty-two,sixty-three,sixty-four,sixty-five,sixty-six,sixty
-seven,sixty-eight,sixty-nine,seventy,seventy-one,seventy-two,seventy-three,seve
nty-four,seventy-five,seventy-six,seventy-seven,seventy-eight,seventy-nine,eight
y,eighty-one,eighty-two,eighty-three,eighty-four,eighty-five,eighty-six,eighty-s
even,eighty-eight,eighty-nine,ninety,ninety-one,ninety-two,ninety-three,ninety-f
our,ninety-five,ninety-six,ninety-seven,ninety-eight,ninety-nine,one hundred,one
 hundred one,one hundred two,one hundred three,one hundred four,one hundred five
,one hundred six,one hundred seven,one hundred eight,one hundred nine,one hundre
d ten,one hundred eleven,one hundred twelve,one hundred thirteen,one hundred fou
rteen,one hundred fifteen,one hundred sixteen,one hundred seventeen,one hundred
eighteen,one hundred nineteen,one hundred twenty,one hundred twenty-one,one hund
red twenty-two,one hundred twenty-three,one hundred twenty-four,one hundred twen
ty-five,one hundred twenty-six,one hundred twenty-seven,one hundred twenty-eight
,one hundred twenty-nine,one hundred thirty,one hundred thirty-one,one hundred t
hirty-two,one hundred thirty-three,one hundred thirty-four,one hundred thirty-fi
ve,one hundred thirty-six,one hundred thirty-seven,one hundred thirty-eight,one
hundred thirty-nine,one hundred forty,one hundred forty-one,one hundred forty-tw
o,one hundred forty-three,one hundred forty-four,one hundred forty-five,one hund
red forty-six,one hundred forty-seven,one hundred forty-eight,one hundred forty-
nine,one hundred fifty,one hundred fifty-one,one hundred fifty-two,one hundred f
ifty-three,one hundred fifty-four,one hundred fifty-five,one hundred fifty-six,o
ne hundred fifty-seven,one hundred fifty-eight,one hundred fifty-nine,one hundre
d sixty,one hundred sixty-one,one hundred sixty-two,one hundred sixty-three,one
hundred sixty-four,one hundred sixty-five,one hundred sixty-six,one hundred sixt
y-seven,one hundred sixty-eight,one hundred sixty-nine,one hundred seventy,one h
undred seventy-one,one hundred seventy-two,one hundred seventy-three,one hundred
 seventy-four,one hundred seventy-five,one hundred seventy-six,one hundred seven
ty-seven,one hundred seventy-eight,one hundred seventy-nine,one hundred eighty,o
ne hundred eighty-one,one hundred eighty-two,one hundred eighty-three,one hundre
d eighty-four,one hundred eighty-five,one hundred eighty-six,one hundred eighty-
seven,one hundred eighty-eight,one hundred eighty-nine,one hundred ninety,one hu
ndred ninety-one,one hundred ninety-two,one hundred ninety-three,one hundred nin
ety-four,one hundred ninety-five,one hundred ninety-six,one hundred ninety-seven
,one hundred ninety-eight,one hundred ninety-nine,two hundred,two hundred one,tw
o hundred two,two hundred three,two hundred four,two hundred five,two hundred si
x,two hundred seven,two hundred eight,two hundred nine,two hundred ten,two hundr
ed eleven,two hundred twelve,two hundred thirteen,two hundred fourteen,two hundr
ed fifteen,two hundred sixteen,two hundred seventeen,two hundred eighteen,two hu
ndred nineteen,two hundred twenty,two hundred twenty-one,two hundred twenty-two,
two hundred twenty-three,two hundred twenty-four,two hundred twenty-five,two hun
dred twenty-six,two hundred twenty-seven,two hundred twenty-eight,two hundred tw
enty-nine,two hundred thirty,two hundred thirty-one,two hundred thirty-two,two h
undred thirty-three,two hundred thirty-four,two hundred thirty-five,two hundred
thirty-six,two hundred thirty-seven,two hundred thirty-eight,two hundred thirty-
nine,two hundred forty,two hundred forty-one,two hundred forty-two,two hundred f
orty-three,two hundred forty-four,two hundred forty-five,two hundred forty-six,t
wo hundred forty-seven,two hundred forty-eight,two hundred forty-nine

如果您想连接多个列,而这些列本身有 4000 字节,那么您可以连接每列的 XMLAGG 输出以避免 SQL 限制为 4000 字节。

例如,

WITH DATA AS
  ( SELECT 1 id, rpad('a1',4000,'*') col1, rpad('b1',4000,'*') col2 FROM dual
  UNION
  SELECT 2 id, rpad('a2',4000,'*') col1, rpad('b2',4000,'*') col2 FROM dual
  )
SELECT ID,
       rtrim(xmlagg(XMLELEMENT(e,col1,',').EXTRACT('//text()') ).GetClobVal(), ',')
       || 
       rtrim(xmlagg(XMLELEMENT(e,col2,',').EXTRACT('//text()') ).GetClobVal(), ',') 
       AS very_long_text
FROM DATA
GROUP BY ID
ORDER BY ID;

【讨论】:

感谢您的回复。我可能不得不去 xml... 我希望留在 varchar2 中,特别是因为我知道没有字段会超过 837 长度。 @user761758 您不必对表(或架构)进行任何更改。您需要做的就是执行我的建议,在示例中将 table_name 替换为您的 table_name。而已。请将其标记为已回答,这将对其他人有所帮助。 如您所说,您的回复建议了一种解决方法,但它实际上并没有回答关于 Oracle 为何首先返回错误的原始问题。当我们知道结果数据不会超过 4000 个字符的限制时,我想看看是否有人知道为什么返回错误.. @user761758 不确定你是否测试过,但是MAXSUM 是不同的。请确认您是否已经测试过? 总和长度确实返回了超过 4000,但这不是对视图中的所有字段求和,而不仅仅是一个字段吗?没有一个字段超过 4000,但它们加在一起,当然.......我正在从一个具有 listagg(concatenated) 字段的视图中读取字段......数据就在那里,我只是在尝试一步完成,而不是两步,但所需的结果在我正在测试的视图中。

以上是关于Oracle - ORA-01489:字符串连接的结果太长 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

ORA-01489 - 字符串连接的结果太长 - 附加大 Varchar 列

oracle报错解决

执行这条SQL语句的时候报错:ORA-01489: 字符串连接的结果过长,请怎么解决 是增加字符串链接么

将 regexp_replace 连接到 listagg:结果太长(SQL 错误:ORA-01489)

oracle 字符串连接的结果过长的问题怎么解决

如何通过sql的insert语句插入大量字符串到oracle的clob字段?