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.
x(XmlCol)
doing? i assumex
is an arbitrary identifier, and i assumeXmlCol
is an arbitrary identifier. What is happening in this query? And, i assume thatCROSS APPLY
is a synonym forFULL OUTER JOIN ON 1=1
, so the table is joining to itself. Is it unable to query the values without the extra join? – Yucca