我想搜索xml节点名称而不是值。例如
我有一张桌子
CREATE TABLE Batches(
BatchID int,
RawXml xml
和xml列数据
<CasinoDisbursementReportXmlFile>
<CasinoDisbursementReport>
<ReportHeader>
<OrganizationReportReferenceIdentifier>C234-864</OrganizationReportReferenceIdentifier>
<OrganizationNumber>0002141</OrganizationNumber>
</ReportHeader>
</CasinoDisbursementReport>
<CasinoDisbursementReport>
<ReportHeader>
<OrganizationReportReferenceIdentifier>C234-865</OrganizationReportReferenceIdentifier>
<OrganizationNumber>0002141</OrganizationNumber>
</ReportHeader>
</CasinoDisbursementReport>
<CasinoDisbursementReport>
<ReportHeader>
<OrganizationReportReferenceIdentifier>C234-866</OrganizationReportReferenceIdentifier>
<OrganizationNumber>0002141</OrganizationNumber>
</ReportHeader>
</CasinoDisbursementReport>
<CasinoDisbursementReport>
<ReportHeader>
<OrganizationReportReferenceIdentifier>C234-867</OrganizationReportReferenceIdentifier>
<OrganizationNumber>0002141</OrganizationNumber>
</ReportHeader>
</CasinoDisbursementReport>
我想只获取xml列中存在OrganizationNumber节点的记录。
答案 0 :(得分:1)
我认为你正在寻找与此类似的东西。
select Int, Rawxml from Batches
where CAST(Rawxml AS VARCHAR(MAX)) like '%OrganizationNumber%';
编辑:我更正了数据类型XML
答案 1 :(得分:0)
尝试这样的事情
select
report.x.value('OrganizationReportReferenceIdentifier[1]', 'varchar(max)') as OrganizationReportReferenceIdentifier
from [Batches] b
cross apply b.RawXml.nodes('/CasinoDisbursementReportXmlFile/CasinoDisbursementReport/ReportHeader') report(x)
where report.x.exist('OrganizationNumber') = 1
我测试了以下脚本(如下),它在SQL Server 2012中运行良好。
请注意,我已删除了下面测试数据中的一个OrganizationNumber
元素。另请注意,您缺少结束标记</CasinoDisbursementReportXmlFile>
,我还将其添加到下面的测试数据中。
CREATE TABLE Batches(
BatchID int,
RawXml xml )
INSERT INTO [dbo].[Batches]
([BatchID]
,[RawXml])
VALUES
(1
,'<CasinoDisbursementReportXmlFile>
<CasinoDisbursementReport>
<ReportHeader>
<OrganizationReportReferenceIdentifier>C234-864</OrganizationReportReferenceIdentifier>
</ReportHeader>
</CasinoDisbursementReport>
<CasinoDisbursementReport>
<ReportHeader>
<OrganizationReportReferenceIdentifier>C234-865</OrganizationReportReferenceIdentifier>
<OrganizationNumber>0002141</OrganizationNumber>
</ReportHeader>
</CasinoDisbursementReport>
<CasinoDisbursementReport>
<ReportHeader>
<OrganizationReportReferenceIdentifier>C234-866</OrganizationReportReferenceIdentifier>
<OrganizationNumber>0002141</OrganizationNumber>
</ReportHeader>
</CasinoDisbursementReport>
<CasinoDisbursementReport>
<ReportHeader>
<OrganizationReportReferenceIdentifier>C234-867</OrganizationReportReferenceIdentifier>
<OrganizationNumber>0002141</OrganizationNumber>
</ReportHeader>
</CasinoDisbursementReport></CasinoDisbursementReportXmlFile>')
select
report.x.value('OrganizationReportReferenceIdentifier[1]', 'varchar(max)') as OrganizationReportReferenceIdentifier
from [Batches] b
cross apply b.RawXml.nodes('/CasinoDisbursementReportXmlFile/CasinoDisbursementReport/ReportHeader') report(x)
where report.x.exist('OrganizationNumber') = 1