Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query values from xml nodes?

i have a table that contains an XML column:

CREATE TABLE Batches(     BatchID int,    RawXml xml  ) 

The xml contains items such as:

<GrobReportXmlFileXmlFile>    <GrobReport>        <ReportHeader>           <OrganizationReportReferenceIdentifier>1</OrganizationReportReferenceIdentifier>           <OrganizationNumber>4</OrganizationNumber>        </ReportHeader>   </GrobReport>    <GrobReport>        <ReportHeader>           <OrganizationReportReferenceIdentifier>2</OrganizationReportReferenceIdentifier>           <OrganizationNumber>4</OrganizationNumber>        </ReportHeader>   </GrobReport>    <GrobReport>        <ReportHeader>           <OrganizationReportReferenceIdentifier>3</OrganizationReportReferenceIdentifier>           <OrganizationNumber>4</OrganizationNumber>        </ReportHeader>   </GrobReport>    <GrobReport>        <ReportHeader>           <OrganizationReportReferenceIdentifier>4</OrganizationReportReferenceIdentifier>           <OrganizationNumber>4</OrganizationNumber>        </ReportHeader>   </GrobReport> 

What i want is to generate a set, that contains:

OrganizationReportReferenceNumber  OrganizationNumber =================================  ================== 1                                  4 2                                  4 3                                  4 4                                  4 

i've tried:

SELECT      foo.value('/ReportHeader/OrganizationReportReferenceIdentifier') AS ReportIdentifierNumber,     foo.value('/ReportHeader/OrganizationNumber') AS OrginazationNumber FROM CDRBatches.RawXML.query('/GrobReportXmlFileXmlFile/GrobReport/ReportHeader') foo 

but that doesn't work. i've tried:

SELECT      foo.value('/ReportHeader/OrganizationReportReferenceIdentifier') AS ReportIdentifierNumber,     foo.value('/ReportHeader/OrganizationNumber') AS OrginazationNumber FROM RawXML.nodes('/GrobReportXmlFileXmlFile/GrobReport/ReportHeader') bar(foo) 

But that doesn't work. The XPath expression

/GrobReportXmlFileXmlFile/GrobReport/ReportHeader 

is correct; in any other xml system it returns:

<ReportHeader>     <OrganizationReportReferenceIdentifier>1</OrganizationReportReferenceIdentifier>     <OrganizationNumber>4</OrganizationNumber> </ReportHeader> <ReportHeader>     <OrganizationReportReferenceIdentifier>2</OrganizationReportReferenceIdentifier>     <OrganizationNumber>4</OrganizationNumber> </ReportHeader> <ReportHeader>     <OrganizationReportReferenceIdentifier>3</OrganizationReportReferenceIdentifier>     <OrganizationNumber>4</OrganizationNumber> </ReportHeader> <ReportHeader>     <OrganizationReportReferenceIdentifier>4</OrganizationReportReferenceIdentifier>     <OrganizationNumber>4</OrganizationNumber> </ReportHeader> 

So, it's obvious from my queries that i'd like to see. After reading a dozen Stackover questions and answers, i'm no closer to solving the problem.

like image 855
Ian Boyd Avatar asked Feb 05 '13 17:02

Ian Boyd


People also ask

How do I select a specific XML node in SQL Server?

You should use the query() Method if you want to get a part of your XML. If you want the value from a specific node you should use value() Method. Update: If you want to shred your XML to multiple rows you use nodes() Method.

How do I get data from XML format in SQL Server?

SQL Server lets you retrieve data as XML by supporting the FOR XML clause, which can be included as part of your query. You can use the FOR XML clause in the main (outer) query as well as in subqueries. The clause supports numerous options that let you define the format of the XML data.

What is XML node in SQL Server?

The nodes() method is useful when you want to shred an xml data type instance into relational data. It allows you to identify nodes that will be mapped into a new row.


2 Answers

SELECT  b.BatchID,         x.XmlCol.value('(ReportHeader/OrganizationReportReferenceIdentifier)[1]','VARCHAR(100)') AS OrganizationReportReferenceIdentifier,         x.XmlCol.value('(ReportHeader/OrganizationNumber)[1]','VARCHAR(100)') AS OrganizationNumber FROM    Batches b CROSS APPLY b.RawXml.nodes('/CasinoDisbursementReportXmlFile/CasinoDisbursementReport') x(XmlCol); 

Demo: SQLFiddle

like image 104
Bogdan Sahlean Avatar answered Sep 21 '22 12:09

Bogdan Sahlean


This works, been tested...

SELECT  n.c.value('OrganizationReportReferenceIdentifier[1]','varchar(128)') AS 'OrganizationReportReferenceNumber',           n.c.value('(OrganizationNumber)[1]','varchar(128)') AS 'OrganizationNumber' FROM    Batches t Cross   Apply RawXML.nodes('/GrobXmlFile/Grob/ReportHeader') n(c)   
like image 30
Eric J. Price Avatar answered Sep 19 '22 12:09

Eric J. Price