从分隔符分隔的oracle db中的clob中提取值

时间:2017-06-29 08:45:03

标签: sql xml oracle

我有一个Oracle DB CLOB字段,用于存储xml。我需要使用DistributionNbr标记检索所有xml中的所有值我已使用下面的查询并为ref提供示例xml。

select extract(xmltype(column_name),'//*/xmlpath/text()').getStringval() as g 
from table;
<xml>
<ShipConfirm>
<ShipmentID>000000196</ShipmentID>
</ShipConfirm>
<ShipConfirm>
<ShipmentID>000000197</ShipmentID>
</ShipConfirm>
<ShipConfirm>
<ShipmentID>000000198</ShipmentID>
</ShipConfirm>
</xml>

现在我将值作为单个字符串获取:000000196000000197000000198

我需要在下一行中获得逗号分隔值或值。 000000196,000000197,000000198

OR

000000196
000000197 
000000198 

2 个答案:

答案 0 :(得分:0)

使用XMLTABLE

SELECT x.ShipmentID
FROM   your_table t
       CROSS JOIN
       XMLTABLE(
         '//xml/ShipConfirm'
          PASSING XMLTYPE( t.column_name )
          COLUMNS ShipmentID CHAR(9) PATH './ShipmentID'
       ) x;

这将以行的形式返回。

如果您希望它以逗号分隔的值,那么您可以使用:

SELECT LISTAGG( x.ShipmentID, ',' ) WITHIN GROUP ( ORDER BY idx )
         AS ShipmentIDs
FROM   your_table t
       CROSS JOIN
       XMLTABLE(
         '//xml/ShipConfirm'
          PASSING XMLTYPE( t.column_name )
          COLUMNS ShipmentID CHAR(9) PATH './ShipmentID',
                  idx FOR ORDINALITY
       ) x
GROUP BY t.primary_key;

答案 1 :(得分:0)

请避免使用不必要的'//'运算符......并且列模式隐含地相对于行模式

SQL> with MY_TABLE as
  2  (
  3    select 1 as PRIMARY_KEY,
  4           XMLTYPE(
  5  '<xml>
  6    <ShipConfirm>
  7     <ShipmentID>000000196</ShipmentID>
  8    </ShipConfirm>
  9    <ShipConfirm>
 10      <ShipmentID>000000197</ShipmentID>
 11    </ShipConfirm>
 12    <ShipConfirm>
 13      <ShipmentID>000000198</ShipmentID>
 14     </ShipConfirm>
 15  </xml>') as XML_DOC
 16      from DUAL
 17  )
 18  SELECT LISTAGG( x.ShipmentID, ',' ) WITHIN GROUP ( ORDER BY idx )  AS ShipmentIDs
 19    FROM MY_TABLE t,
 20         XMLTABLE(
 21           '/xml/ShipConfirm'
 22            PASSING XML_DOC
 23            COLUMNS
 24              idx FOR ORDINALITY,
 25              ShipmentID CHAR(9) PATH 'ShipmentID'
 26         ) x
 27  GROUP BY t.primary_key
 28  /

SHIPMENTIDS
--------------------------------------------------------------------------------
000000196,000000197,000000198

SQL>
SQL>