gpt4 book ai didi

sql-server - 如何从 xml 节点查询值?

转载 作者:行者123 更新时间:2023-12-02 04:47:11 24 4
gpt4 key购买 nike

我有一个包含 XML 列的表:

CREATE TABLE Batches( 
BatchID int,
RawXml xml
)

XML 包含以下项目:

<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>

我想要的是生成一个集合,其中包含:

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

我已经尝试过:

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

但这不起作用。我试过:

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

但这行不通。 XPath 表达式

/GrobReportXmlFileXmlFile/GrobReport/ReportHeader

正确;在任何其他 xml 系统中它返回:

<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>

所以,从我的查询中可以明显看出我希望看到。在阅读了十几个 Stackover 问题和答案后,我距离解决问题还很远。

最佳答案

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);

演示:SQLFiddle

关于sql-server - 如何从 xml 节点查询值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14712864/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com