2012-05-02 242 views
2

我正在使用SYS_CONNECT_BY_PATH做字符串聚合。查询的一般形状是这样的:SYS_CONNECT_BY_PATH导致“ORA-01489:字符串连接的结果太长”

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

关于甲骨文字符串聚合技术。我怀疑唯一能在我的情况下工作的是XML方法和Model方法。我无法让模型方法正常工作,所以我只是使用XML方法。

+1

这将会很艰难。 [这是关于主题的有趣的论坛帖子](https://forums.oracle.com/forums/thread.jspa?threadID=963324)。引用一个答案:“*您可以使用SYS_CONNECT_BY_PATH在子查询中形成部分字符串,然后将这些部分连接到主查询中的CLOB *。祝你好运! –

回答

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 
); 
+0

这似乎在工作,但我现在有两个查询:一个用于报告数据,一个用于xmlagg的结果。这可能是没有别的办法的...... – FrustratedWithFormsDesigner

0

对不起我的答案依赖于创建字符串总包,但可能在长期内 您可以使用,而不是在下面的链接

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

中,在AskTom提到SYS_CONNECT_BY_PATH 的Stragg包是有用的包有一个声明和一些处理长的逻辑,您可以根据您的需求更改以处理CLOB

+0

虽然我正在针对生产服务器运行此查询以生成生产数据报告,但这可能会起作用。因为它是生产服务器,所以为了方便报告,我不能编译新的软件包。 – FrustratedWithFormsDesigner

相关问题