如何从DB2 Clob中检索数据?

时间:2017-05-17 06:39:55

标签: xml db2

我有一个带XML的列 - XMLMESSAGE

CLOB中的XML示例:

<xml>
 <attribute>name</attribute>
 <value>this is my xml</value>
 <logLocation>/user/apps/myLogFolder</logLocation>
 <engines>
    <engine>
        <engineName>Engine1</engineName>
        <engineLocation>$HOME/apps/engines</engineLocation>
        <engineVersion>3.1416</engineVersion>
    </engine>
    <engine>
        <engineName>Engine2</engineName>
        <engineLocation>$HOME/apps/engines/backup</engineLocation>
        <engineVersion>3b</engineVersion>
    </engine>
</engines>
<connections>
    <connection>
    <jdbc>jdbc:db2://127.0.0.1:50000/localdb</jdbc>
    <user>dbuser</user>
    <password>{}</password>
    </connection>
</connections>

我想检索值@ XPATH: / XML /发动机/发动机/引擎名称

之前我在Oracle中使用XMLEXTRACT,并且想知道DB2是否具有类似的功能。 我对这个主题的答案很少,但无法得到解决方案。

1 个答案:

答案 0 :(得分:0)

您好我尝试重新创建您的问题,我为xml节点添加了结束标记

CREATE table work.dd(int id, doc clob);

insert into work.DD VALUES (1,'<xml>
 <attribute>name</attribute>
 <value>this is my xml</value>
 <logLocation>/user/apps/myLogFolder</logLocation>
 <engines>
   <engine>
    <engineName>Engine1</engineName>
    <engineLocation>$HOME/apps/engines</engineLocation>
    <engineVersion>3.1416</engineVersion>
</engine>
<engine>
    <engineName>Engine2</engineName>
    <engineLocation>$HOME/apps/engines/backup</engineLocation>
    <engineVersion>3b</engineVersion>
</engine>
</engines>
  <connections>
     <connection>
     <jdbc>jdbc:db2://127.0.0.1:50000/localdb</jdbc>
     <user>dbuser</user>
    <password>{}</password>
    </connection>
</connections>
</xml>') 

那么你的查询应该是这样的

select * from work.dd,
XMLTABLE(
    '$d/xml/engines/engine' PASSING XMLPARSE(DOCUMENT doc) AS "d"
    COLUMNS
    engineName varCHAR(100) PATH 'engineName'
)

我在lUW 10.1中测试了它