2012-05-02 248 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

相關問題