简体   繁体   中英

SQL Querying inside XML column

Am trying to Query inside an SQL table which has XML Column . Table name: 'Purchase' Column name: 'XML_COL'

Please find below xml data for column name 'XML_COL' under purchase table:

<ns1:Request xmlns:ns1="http://www.sample.com/hic/event/request"
xmlns:ns2="http://www.sample.com/hic/eventpayload/request">
<ns1:createeventRequest>
<ns1:eventPayLoad>
<ns2:eventPayLoad>
<Id>123456</Id>
</ns2:eventPayLoad>
</ns1:eventPayLoad>
</ns1:createeventRequest>
</ns1:Request>

I have written below query :

`select * from  purchase,
XMLTABLE ('$d/Request/createeventRequest/eventPayLoad/eventPayLoad' PASSING  XML_COL  as  "d" 
COLUMNS 
Id  varchar(20)  PATH 'Id')  as a  where(a.Id like '1234%');`

But this is returning me an empty column with no data. But my requirement is it should fetch all the data for this particular Id. Please help if any one faced this kind of issue. Do we need to include namespaces as well while querying?? or am I missing any thing?

I think the expression PATH 'Id' is bit to simple...

I'm not familiar with MySQL's abilities to query XML... The Path Id would try to find an element "Id" from the current node (which is the root node in the first action). But there is no "Id"... You must either specify the full path, starting with a single / to start at the root node, or let the engine try a deep search, starting with two //

These paths should work:

SELECT ExtractValue( 
'<ns1:Request xmlns:ns1="http://www.sample.com/hic/event/request" xmlns:ns2="http://www.sample.com/hic/eventpayload/request">
  <ns1:createeventRequest>
    <ns1:eventPayLoad>
      <ns2:eventPayLoad>
        <Id>123456</Id>
      </ns2:eventPayLoad>
    </ns1:eventPayLoad>
  </ns1:createeventRequest>
</ns1:Request>',

'/ns1:Request[1]/ns1:createeventRequest[1]/ns1:eventPayLoad[1]/ns2:eventPayLoad[1]/Id[1]' ) AS result;

If there is only one element with a value (in your case "Id") you might use the simple deep search like this:

SELECT ExtractValue( 
'<ns1:Request xmlns:ns1="http://www.sample.com/hic/event/request" xmlns:ns2="http://www.sample.com/hic/eventpayload/request">
  <ns1:createeventRequest>
    <ns1:eventPayLoad>
      <ns2:eventPayLoad>
        <Id>123456</Id>
      </ns2:eventPayLoad>
    </ns1:eventPayLoad>
  </ns1:createeventRequest>
</ns1:Request>',

'//Id[1]' ) AS result;

But - in general - it is good advise to be as specific as possible...

Just cracked the query...When name spaces are being used in an XML, instead of the entire path, I found it's better to use '/*//' which traverses through the required element tag through XML.

Final Query:

select * from purchase, XMLTABLE('$d' PASSING XML_COL as "d" COLUMNS Id varchar(20) PATH '/*//Id') as a where(a.Id like '1234%') with ur

Using 'with ur' helps to read the data that has not been committed in the database.

Please post comments if it is helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM