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.
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.
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.
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.
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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With