如何从Oracle查询中的XML字段中选择值

时间:2019-05-14 04:44:51

标签: sql xml oracle

我有一个表列中的XML数据,其示例XML格式如下所述:

<?xml version="1.0" encoding="Big5"?>
<SN Name="Group Medical Member Setup">
  <DO Name="datPrlMmr" Label="" Table="COMPRLMMR">
    <RECORD>
      <ACTION Name="M">
    <F Name="MMRCNYOFRSN" Label="" OldValue="HKG" NewValue="AUS" />
    <F Name="MDFBY" Label=" " OldValue="fc" NewValue="admin" />
    <F Name="MDFTMSTP" Label=" " OldValue="Thu Feb 19 11:14:37 HKT 2004" NewValue="Tue May 07 14:24:09 HKT 2019" />
      </ACTION>
    </RECORD>
  </DO>
  <DO Name="doAdmGMPlyMmrDtl" Label="" Table="ADMGMPLYMMRDTL">
    <RECORD>
      <ACTION Name="M">
    <F Name="MDFTMSTP" Label="Modified TimeStamp" OldValue="Mon May 06 03:12:20 SGT 2019" NewValue="Mon May 06 03:15:00 SGT 2019" />
    <F Name="RCDTMSTP" Label="Record TimeStamp" OldValue="Mon May 06 03:12:20 SGT 2019" NewValue="Mon May 06 03:15:00 SGT 2019" />
      </ACTION>
    </RECORD>
  </DO>
</SN>

我想为字段 Name =“ MMRCNYOFRSN” 提取与' NewValue '标记名相对应的值。 这将仅在xml中出现一次。请通过SQL查询oracle提取 NewValue(AUS)的适当方法来帮助我。

2 个答案:

答案 0 :(得分:0)

with s as 
(select xmltype('<?xml version="1.0" encoding="Big5"?>
<SN Name="Group Medical Member Setup">
  <DO Name="datPrlMmr" Label="" Table="COMPRLMMR">
    <RECORD>
      <ACTION Name="M">
    <F Name="MMRCNYOFRSN" Label="" OldValue="HKG" NewValue="AUS" />
    <F Name="MDFBY" Label=" " OldValue="fc" NewValue="admin" />
    <F Name="MDFTMSTP" Label=" " OldValue="Thu Feb 19 11:14:37 HKT 2004" NewValue="Tue May 07 14:24:09 HKT 2019" />
      </ACTION>
    </RECORD>
  </DO>
  <DO Name="doAdmGMPlyMmrDtl" Label="" Table="ADMGMPLYMMRDTL">
    <RECORD>
      <ACTION Name="M">
    <F Name="MDFTMSTP" Label="Modified TimeStamp" OldValue="Mon May 06 03:12:20 SGT 2019" NewValue="Mon May 06 03:15:00 SGT 2019" />
    <F Name="RCDTMSTP" Label="Record TimeStamp" OldValue="Mon May 06 03:12:20 SGT 2019" NewValue="Mon May 06 03:15:00 SGT 2019" />
      </ACTION>
    </RECORD>
  </DO>
</SN>') x from dual)
select t.*
from s,
xmltable(
'/SN/DO/RECORD/ACTION/F[@Name="MMRCNYOFRSN"]'
passing s.x
columns
name     varchar2(100) path '@Name',
oldvalue varchar2(100) path '@OldValue',
newvalue varchar2(100) path '@NewValue'
)(+) t;

NAME            OLDVALUE        NEWVALUE       
--------------- --------------- ---------------
MMRCNYOFRSN     HKG             AUS 

答案 1 :(得分:0)

由于只需要一个(属性)值,因此您也可以使用XMLQuery

-- CTE for your sample data
with your_table (your_column) as (
  select xmltype('<?xml version="1.0" encoding="Big5"?>
<SN Name="Group Medical Member Setup">
  <DO Name="datPrlMmr" Label="" Table="COMPRLMMR">
    <RECORD>
      <ACTION Name="M">
    <F Name="MMRCNYOFRSN" Label="" OldValue="HKG" NewValue="AUS" />
    <F Name="MDFBY" Label=" " OldValue="fc" NewValue="admin" />
    <F Name="MDFTMSTP" Label=" " OldValue="Thu Feb 19 11:14:37 HKT 2004" NewValue="Tue May 07 14:24:09 HKT 2019" />
      </ACTION>
    </RECORD>
  </DO>
  <DO Name="doAdmGMPlyMmrDtl" Label="" Table="ADMGMPLYMMRDTL">
    <RECORD>
      <ACTION Name="M">
    <F Name="MDFTMSTP" Label="Modified TimeStamp" OldValue="Mon May 06 03:12:20 SGT 2019" NewValue="Mon May 06 03:15:00 SGT 2019" />
    <F Name="RCDTMSTP" Label="Record TimeStamp" OldValue="Mon May 06 03:12:20 SGT 2019" NewValue="Mon May 06 03:15:00 SGT 2019" />
      </ACTION>
    </RECORD>
  </DO>
</SN>')
  from dual
)
-- actual query
select xmlquery('/SN/DO/RECORD/ACTION/F[@Name="MMRCNYOFRSN"]/@NewValue'
  passing t.your_column
  returning content) as MMRCNYOFRSN
from your_table t;

MMRCNYOFRSN                                                                     
--------------------------------------------------------------------------------
AUS