SYS_CONNECT_BY_PATH导致“ORA-01489:字符串连接的结果太长”

时间:2012-05-02 15:34:06

标签: sql oracle

我正在使用SYS_CONNECT_BY_PATH进行字符串聚合。查询的一般形状如下:

select /*a bunch of fields unrelated to the problem*/,
--Use SYS_CONNECT_BY_PATH to glue together all the chunks of XML. 
--The XSL header and footer are prepended and appended here.
 , XMLType(to_clob('<?xml version="1.0"?><!-- begining of XSL file -->,'<!-- Next Section -->'))||'</xsl:stylesheet>')) AS XSL
from (
  select /*a bunch of fields unrelated to the problem*/
    case when x = 1 then to_clob('
    /*a bunch of XSL*/
     <xsl:text>'||subq.new_c_value||'</xsl:text>
    /*a whole bunch more xsl*/')
    else
     to_clob('/*a bunch of different XSL*/             
     <xsl:text>'||subq.new_f_value||'</xsl:text>
    /*a whole bunch more xsl*/')
    end as xsl,
  --curr and prev are to help with using sys_connect_by_path do string aggregation.
    rownum AS curr,
    rownum -1 AS prev
  from (Select /* details of subq not relevant */ ) as subq
)
CONNECT BY prev = PRIOR curr 
START WITH curr = 1;

基本上,我正在运行查询来生成用于纠正XML文件的XSL。我正在使用sys_connect_by_path将字符串组合成一个单独的块,这比复制和粘贴许多行中的许多值更容易。我不能使用任何自定义字符串聚合函数,因为此查询在生产数据库上运行,我不能只是去创建我想要的函数。

问题是运行我的查询将返回:

ORA-01489: result of string concatenation is too long
01489. 00000 -  "result of string concatenation is too long"
*Cause:    String concatenation result is more than the maximum size.
*Action:   Make sure that the result is less than the maximum size.

...在数据太多的情况下。正如您所看到的,我一直在将to_clob()函数应用到我认为可能有用的任何地方,但它似乎没有太大的区别。除了诉诸PL / SQL之外,还有其他方法可以解决这个问题吗?我更喜欢将其保留为查询,因为此查询的结果将导出到报告模板,该模板与XSL并排显示许多有用的信息。能够在一个步骤而不是几个步骤中完成所有这些工作将会很棒。

(Oracle 10g)


最终,我找到了这个页面:

http://www.sqlsnippets.com/en/topic-11787.html

关于Oracle中的字符串聚合技术。我怀疑在我的情况下唯一可行的是XML方法和模型方法。我无法使模型方法正常工作,所以我只使用XML方法。

2 个答案:

答案 0 :(得分:1)

select
    xmlroot
    (
        xmlelement
        (
            "xsl:stylesheet"
            ,XMLAttributes
            (
                '1.0' as version
                ,'http://www.w3.org/1999/XSL/Transform' as "xmlns:xsl"
                ,'http://test' as "xmlns:ns0"
            )
            ,(
                xmlagg(xmlelement("xsl:text", val))
            )
        )
        ,version '1.0'
    )
from
(
    --Test >4000 characters
    select 1 id, cast(rpad('a',4000,'a') as varchar2(4000)) val from dual union all
    select 1 id, cast(rpad('b',4000,'b') as varchar2(4000)) val from dual union all
    select 1 id, cast(rpad('c',4000,'c') as varchar2(4000)) val from dual union all
    select 1 id, cast(rpad('d',4000,'d') as varchar2(4000)) val from dual
);

答案 1 :(得分:0)

很抱歉,我的回答依赖于创建字符串聚合包,但从长远来看可能很有用 您可以使用Stragg Package而不是sys_connect_by_path AskTom在下面的链接中提到

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:2196162600402

在该软件包中有一个声明和一些处理long的逻辑,您可以根据需要更改处理CLOB