2014-02-28 33 views
0

我有一個包含xml格式的列CLOB類型。在Oracle列中獲取字段的xmltype CLOB

我需要一個簡單的查詢,以獲得從該領域的所有不同的類型:

例。該字段包含不同的類型:

First record: 
<Message type="New Address" xmlns="http://euroconsumers.org/ecommerce/2009/01/scope/messages"><Customer ... 

Second record: 
<Message type="Added Email" xmlns="http://euroconsumers.org/ecommerce/2009/01/scope/messages"><Customer ... 

Third record: 
<Message type="New Order" xmlns="http://euroconsumers.org/ecommerce/2009/01/scope/messages"><Customer ... 

我想檢索:

New Address 
Added Email 
New Order 
+0

這可能是有用的:http://psoug.org/reference/xml_functions.html –

回答

1

這適用於你的數據:

select xmlquery('/*/@type' 
    passing xmltype(<clob column>) 
    returning content) 
from <your table>; 

演示:

create table t42 (clob_col clob); 
insert into t42 values ('<Message type="New Address" xmlns="..."><Customer type="x"></Customer></Message>'); 
insert into t42 values ('<Message type="Added Email" xmlns="..."><Customer></Customer></Message>'); 
insert into t42 values ('<Message type="New Order" xmlns="..."><Customer></Customer></Message>'); 

select xmlquery('/*/@type' 
    passing xmltype(t42.clob_col) 
    returning content) 
from t42; 

XMLQUERY('/*/@TYPE'PASSINGXMLTYPE(T42.CLOB_COL)RETURNINGCONTENT) 
---------------------------------------------------------------- 
New Address 
Added Email 
New Order 

或者這:

select xmltype(<clob_column>).extract('/*/@type') 
from <your table>; 

演示:

select xmltype(clob_col).extract('/*/@type') 
from t42; 

XMLTYPE(CLOB_COL).EXTRACT('/*/@TYPE') 
------------------------------------- 
New Address 
Added Email 
New Order 

Read more有關查詢XML。

+0

謝謝!運作良好。 –