2016-09-21 11 views
1

ランニング次のクエリ(複数行)でインライン出力PostgreSQLで

select REPLACE(
      REPLACE(
      REPLACE(
       REPLACE(
        query_to_xml(
         'select 1 "col1",2 "col2",3 "col3" 
         union all 
         select 11 "col1",22 "col2",33 "col3" 
         union all 
         select 111 "col1",222 "col2",333 "col3"', 
         true, 
         false, 
         '' 
        )::text , 
        '<row>', 
        '<Leaf>' 
       ), 
       '</row>', 
       '</Leaf>' 
      ), 
      '< table xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >', 
      '<Tree>' 
     ), 
      '</table>', 
      '</Tree>' 
     ) 

結果(すなわち、単一の行のステートメント)に必要な

<Tree> 

<Leaf> 

<col1>1</col1> 

<col2>2</col2> 

<col3>3</col3> 

</Leaf> 

<Leaf> 

<col1>11</col1> 

<col2>22</col2> 

<col3>33</col3> 

</Leaf> 

<Leaf> 

<col1>111</col1> 

<col2>222</col2> 

<col3>333</col3> 

</Leaf> 

</Tree> 

<Tree> <Leaf> <col1>1</col1> <col2>2</col2> <col3>3</col3> </Leaf> <Leaf> <col1>11</col1> <col2>22</col2> <col3>33</col3> </Leaf> <Leaf> <col1>111</col1> <col2>222</col2> <col3>333</col3> </Leaf> </Tree> 

答えて

1

ます結果から改行文字を削除する必要があります。新しい行を削除するには、regexp_replace(column_or_result, E'[\\n\\r]+', ' ', 'g')を使用できます。

あなたのクエリは次のようになります。

( が '1 "COL1"、2 "COL2"、3 "col3という" 労働組合すべてを選択選択します((query_to_xmlを(REPLACE REPLACE(REPLACE(REPLACE

選択REGEXP_REPLACE 11 "col1"、22 "col2"、33 "col3"共用体すべてselect 111 "col1"、222 "col2"、333 "col3" '、true、false、' '):: text、' < row ' '<リーフ>')、 '< /行>'、 '< /リーフ>')、 '<テーブルxmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance">' '<ツリー>')、 '</table>'、 '</Tree>')、 E '[\ n \ r] +'、 ''、 ' g ')

+0

多くのthnks、それは本当に速く、きれいで適切でした。 – Yusuf

関連する問題