XML 列 - 三级层次结构 - 交叉应用
XML Column - three levels hierarchy - with Cross Apply
我之前的问题已解决 。现在我给它增加了一层复杂性——嵌套的父、子、孙数据。
您可以在此处查看和 运行 示例:
https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=df2766c95383d4c8c2d1f55539634341
示例代码,其中 Leg1 可能是出发的行程,Leg2 可能是返回的行程。每条航段可以有一个或多个航班。
DECLARE @xml XML='
<Reservation>
<Name>Neal</Name>
<Leg seq=''1''>
<Flight>12</Flight>
</Leg>
<Leg seq=''2''>
<Flight>34</Flight>
<Flight>56</Flight>
</Leg>
</Reservation>'
select @xml
DECLARE @xmlTable TABLE (
xmlDoc Xml
);
Insert into @xmltable values (@xml)
--Select xmlDoc from @XmlTable
Select xmlDoc.value('(//Name)[1]', 'varchar(30)') as Passenger,
XmlData2.xmlDoc2.query('.') as XmlData2,
XmlData2.xmlDoc2.value('./@seq', 'int') as LegSeq,
XmlData3.xmlDoc3.query('.') as XmlData3,
XmlData3.xmlDoc3.value('.', 'varchar(20)') as Flight
FROM @xmlTable as t
CROSS APPLY
t.xmlDoc.nodes('//Leg') AS XmlData2(xmlDoc2)
CROSS APPLY
t.xmlDoc.nodes('//Flight') AS XmlData3(xmlDoc3)
问题是我仍然需要返回 3 行,但现在我得到了 6 行。
预期结果为:
Neal LegSeq=1 Flight=12
Neal LegSeq=2 Flight=34
Neal LegSeq=2 Flight=56
Microsoft SQL Server 2019 (RTM) - 15.0.2000.5 (X64) 2019 年 9 月 24 日 13:48:23 版权所有 (C) 2019 Microsoft Corporation Developer Edition(64 位)在 Windows 服务器 2019 标准版 10.0(内部版本 17763:)
在第二次申请中,您想要申请来自 XmlData2.xmlDoc2
的节点。按照您编写的方式,它再次从根查找节点,这将应用于 XML.
中的所有 Flight
元素
DECLARE @xml XML='
<Reservation>
<Name>Neal</Name>
<Leg seq=''1''>
<Flight>12</Flight>
</Leg>
<Leg seq=''2''>
<Flight>34</Flight>
<Flight>56</Flight>
</Leg>
</Reservation>'
select @xml
DECLARE @xmlTable TABLE (
xmlDoc Xml
);
Insert into @xmltable values (@xml)
--Select xmlDoc from @XmlTable
Select xmlDoc.value('(//Name)[1]', 'varchar(30)') as Passenger,
XmlData2.xmlDoc2.query('.') as XmlData2,
XmlData2.xmlDoc2.value('./@seq', 'int') as LegSeq,
XmlData3.xmlDoc3.query('.') as XmlData3,
XmlData3.xmlDoc3.value('.', 'varchar(20)') as Flight
FROM @xmlTable as t
CROSS APPLY
t.xmlDoc.nodes('//Leg') AS XmlData2(xmlDoc2)
CROSS APPLY
XmlData2.xmlDoc2.nodes('Flight') AS XmlData3(xmlDoc3);
我之前的问题已解决
您可以在此处查看和 运行 示例: https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=df2766c95383d4c8c2d1f55539634341
示例代码,其中 Leg1 可能是出发的行程,Leg2 可能是返回的行程。每条航段可以有一个或多个航班。
DECLARE @xml XML='
<Reservation>
<Name>Neal</Name>
<Leg seq=''1''>
<Flight>12</Flight>
</Leg>
<Leg seq=''2''>
<Flight>34</Flight>
<Flight>56</Flight>
</Leg>
</Reservation>'
select @xml
DECLARE @xmlTable TABLE (
xmlDoc Xml
);
Insert into @xmltable values (@xml)
--Select xmlDoc from @XmlTable
Select xmlDoc.value('(//Name)[1]', 'varchar(30)') as Passenger,
XmlData2.xmlDoc2.query('.') as XmlData2,
XmlData2.xmlDoc2.value('./@seq', 'int') as LegSeq,
XmlData3.xmlDoc3.query('.') as XmlData3,
XmlData3.xmlDoc3.value('.', 'varchar(20)') as Flight
FROM @xmlTable as t
CROSS APPLY
t.xmlDoc.nodes('//Leg') AS XmlData2(xmlDoc2)
CROSS APPLY
t.xmlDoc.nodes('//Flight') AS XmlData3(xmlDoc3)
问题是我仍然需要返回 3 行,但现在我得到了 6 行。
预期结果为:
Neal LegSeq=1 Flight=12
Neal LegSeq=2 Flight=34
Neal LegSeq=2 Flight=56
Microsoft SQL Server 2019 (RTM) - 15.0.2000.5 (X64) 2019 年 9 月 24 日 13:48:23 版权所有 (C) 2019 Microsoft Corporation Developer Edition(64 位)在 Windows 服务器 2019 标准版 10.0(内部版本 17763:)
在第二次申请中,您想要申请来自 XmlData2.xmlDoc2
的节点。按照您编写的方式,它再次从根查找节点,这将应用于 XML.
Flight
元素
DECLARE @xml XML='
<Reservation>
<Name>Neal</Name>
<Leg seq=''1''>
<Flight>12</Flight>
</Leg>
<Leg seq=''2''>
<Flight>34</Flight>
<Flight>56</Flight>
</Leg>
</Reservation>'
select @xml
DECLARE @xmlTable TABLE (
xmlDoc Xml
);
Insert into @xmltable values (@xml)
--Select xmlDoc from @XmlTable
Select xmlDoc.value('(//Name)[1]', 'varchar(30)') as Passenger,
XmlData2.xmlDoc2.query('.') as XmlData2,
XmlData2.xmlDoc2.value('./@seq', 'int') as LegSeq,
XmlData3.xmlDoc3.query('.') as XmlData3,
XmlData3.xmlDoc3.value('.', 'varchar(20)') as Flight
FROM @xmlTable as t
CROSS APPLY
t.xmlDoc.nodes('//Leg') AS XmlData2(xmlDoc2)
CROSS APPLY
XmlData2.xmlDoc2.nodes('Flight') AS XmlData3(xmlDoc3);