SQL 服务器使用 XMLQUERY 解析 XML 个节点的数组
SQL Server parse array of XML Nodes using XMLQUERY
我正在尝试解析一个 XML 节点数组,它是一个转换为 XML 的 nvarchar(max) 列,我使用 google 进行了搜索并搜索了 Whosebug ,但我没有找到任何可以帮助我解决这个问题的例子。
我有一个 XML 值存储为 nvarchar(max),格式如下:
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfCmnStatusRuleOverride xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<cmnStatusRuleOverride xsi:type="cmnCoveredByOverride">
<Start>2020-05-28T17:00:00</Start>
<End>2020-05-29T07:00:00</End>
<SuspendExpiration>0001-01-01T00:00:00</SuspendExpiration>
<subID>103</subID>
<listID>4016</listID>
<Description>Jane Doe</Description>
</cmnStatusRuleOverride>
<cmnStatusRuleOverride xsi:type="cmnCoveredByOverride">
<Start>2020-05-26T17:00:00</Start>
<End>2020-05-28T07:00:00</End>
<SuspendExpiration>0001-01-01T00:00:00</SuspendExpiration>
<subID>103</subID>
<listID>4014</listID>
<Description>Joe Blow</Description>
</cmnStatusRuleOverride>
<cmnStatusRuleOverride xsi:type="cmnCoveredByOverride">
<Start>2020-05-25T17:00:00</Start>
<End>2020-05-26T07:00:00</End>
<SuspendExpiration>0001-01-01T00:00:00</SuspendExpiration>
<subID>103</subID>
<listID>4016</listID>
<Description>Jane Doe</Description>
</cmnStatusRuleOverride>
</ArrayOfCmnStatusRuleOverride>
我正在使用以下 SQL 来解析内容,这接近我需要的内容,但是交叉应用给我带来了问题
declare @Status xml, @Override nvarchar(100),@ORStart nvarchar(50), @OREnd nvarchar(50)
set @Status =
(select b.Overrides from Database.dbo.Descriptions as a
inner join Database.dbo.Listings as b on a.listID = b.listId
inner join Database.dbo.Clients as c on b.subId = c.subId and cast(c.ClientNumber as nvarchar) = '2195956693'
--cross apply @Status.nodes('/ArrayOfCmnStatusRuleOverride/cmnStatusRuleOverride/Description') as T(Loc)
where a.Description = 'Jack Sprat')
select T.OVR.query('.') 'Override'--, T1.STT.query('.') 'Start'--, T2.ENND.query('.') 'End'
from Database.dbo.Descriptions as a
inner join Database.dbo.Listings as b on a.listID = b.listId
inner join Database.dbo.Clients as c on b.subId = c.subId and cast(c.ClientNumber as nvarchar) = '9876543210'
cross apply @Status.nodes('/ArrayOfCmnStatusRuleOverride/cmnStatusRuleOverride/Description') as T(OVR)
--cross apply T.OVR.nodes('/ArrayOfCmnStatusRuleOverride/cmnStatusRuleOverride/Start') as T1(STT)
--cross apply T.OVR.nodes('/ArrayOfCmnStatusRuleOverride/cmnStatusRuleOverride/End') as T2(ENND)
where a.Description = 'Jack Sprat'
第二个和第三个 xml 查询被注释掉后,我得到了 3 个结果,正如我所期望的那样。
- 李四
- 乔布洛
- 李四
如果我取消注释第二个查询,那么我会得到如下 9 个结果
Override Start
- 无名氏 2020-05-28T17:00:00
- 无名氏 2020-05-26T17:00:00
- 无名氏 2020-05-25T17:00:00
- 乔打击 2020-05-28T17:00:00
- 乔打击 2020-05-26T17:00:00
- 乔打击 2020-05-25T17:00:00
- 无名氏 2020-05-28T17:00:00
- 无名氏 2020-05-26T17:00:00
- 无名氏 2020-05-25T17:00:00
当然,如果我取消注释第三个查询,我会得到 27 行。关于我做错了什么的任何提示?从昨天早上开始,我就一直在研究这个问题,试图弄清楚这个问题,虽然如此接近,但又如此遥远。
想通了。
select T.OVR.query('./Description') 'Override',
T.OVR.query('./Start') 'Start',
T.OVR.query('./End') 'End'
from Database.dbo.Descriptions as a
inner join Database.dbo.Listings as b on a.listID = b.listId
inner join Database.dbo.Clients as c on b.subId = c.subId and cast(c.ClientNumber as nvarchar) = '9876543210'
cross apply @Status.nodes('/ArrayOfCmnStatusRuleOverride/cmnStatusRuleOverride') as T(OVR)
where a.Description = 'Jack Sprat'
我正在尝试解析一个 XML 节点数组,它是一个转换为 XML 的 nvarchar(max) 列,我使用 google 进行了搜索并搜索了 Whosebug ,但我没有找到任何可以帮助我解决这个问题的例子。
我有一个 XML 值存储为 nvarchar(max),格式如下:
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfCmnStatusRuleOverride xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<cmnStatusRuleOverride xsi:type="cmnCoveredByOverride">
<Start>2020-05-28T17:00:00</Start>
<End>2020-05-29T07:00:00</End>
<SuspendExpiration>0001-01-01T00:00:00</SuspendExpiration>
<subID>103</subID>
<listID>4016</listID>
<Description>Jane Doe</Description>
</cmnStatusRuleOverride>
<cmnStatusRuleOverride xsi:type="cmnCoveredByOverride">
<Start>2020-05-26T17:00:00</Start>
<End>2020-05-28T07:00:00</End>
<SuspendExpiration>0001-01-01T00:00:00</SuspendExpiration>
<subID>103</subID>
<listID>4014</listID>
<Description>Joe Blow</Description>
</cmnStatusRuleOverride>
<cmnStatusRuleOverride xsi:type="cmnCoveredByOverride">
<Start>2020-05-25T17:00:00</Start>
<End>2020-05-26T07:00:00</End>
<SuspendExpiration>0001-01-01T00:00:00</SuspendExpiration>
<subID>103</subID>
<listID>4016</listID>
<Description>Jane Doe</Description>
</cmnStatusRuleOverride>
</ArrayOfCmnStatusRuleOverride>
我正在使用以下 SQL 来解析内容,这接近我需要的内容,但是交叉应用给我带来了问题
declare @Status xml, @Override nvarchar(100),@ORStart nvarchar(50), @OREnd nvarchar(50)
set @Status =
(select b.Overrides from Database.dbo.Descriptions as a
inner join Database.dbo.Listings as b on a.listID = b.listId
inner join Database.dbo.Clients as c on b.subId = c.subId and cast(c.ClientNumber as nvarchar) = '2195956693'
--cross apply @Status.nodes('/ArrayOfCmnStatusRuleOverride/cmnStatusRuleOverride/Description') as T(Loc)
where a.Description = 'Jack Sprat')
select T.OVR.query('.') 'Override'--, T1.STT.query('.') 'Start'--, T2.ENND.query('.') 'End'
from Database.dbo.Descriptions as a
inner join Database.dbo.Listings as b on a.listID = b.listId
inner join Database.dbo.Clients as c on b.subId = c.subId and cast(c.ClientNumber as nvarchar) = '9876543210'
cross apply @Status.nodes('/ArrayOfCmnStatusRuleOverride/cmnStatusRuleOverride/Description') as T(OVR)
--cross apply T.OVR.nodes('/ArrayOfCmnStatusRuleOverride/cmnStatusRuleOverride/Start') as T1(STT)
--cross apply T.OVR.nodes('/ArrayOfCmnStatusRuleOverride/cmnStatusRuleOverride/End') as T2(ENND)
where a.Description = 'Jack Sprat'
第二个和第三个 xml 查询被注释掉后,我得到了 3 个结果,正如我所期望的那样。
- 李四
- 乔布洛
- 李四
如果我取消注释第二个查询,那么我会得到如下 9 个结果
Override Start
- 无名氏 2020-05-28T17:00:00
- 无名氏 2020-05-26T17:00:00
- 无名氏 2020-05-25T17:00:00
- 乔打击 2020-05-28T17:00:00
- 乔打击 2020-05-26T17:00:00
- 乔打击 2020-05-25T17:00:00
- 无名氏 2020-05-28T17:00:00
- 无名氏 2020-05-26T17:00:00
- 无名氏 2020-05-25T17:00:00
当然,如果我取消注释第三个查询,我会得到 27 行。关于我做错了什么的任何提示?从昨天早上开始,我就一直在研究这个问题,试图弄清楚这个问题,虽然如此接近,但又如此遥远。
想通了。
select T.OVR.query('./Description') 'Override',
T.OVR.query('./Start') 'Start',
T.OVR.query('./End') 'End'
from Database.dbo.Descriptions as a
inner join Database.dbo.Listings as b on a.listID = b.listId
inner join Database.dbo.Clients as c on b.subId = c.subId and cast(c.ClientNumber as nvarchar) = '9876543210'
cross apply @Status.nodes('/ArrayOfCmnStatusRuleOverride/cmnStatusRuleOverride') as T(OVR)
where a.Description = 'Jack Sprat'