在 XML 中使用 2 CROSS APPLY
Posted
技术标签:
【中文标题】在 XML 中使用 2 CROSS APPLY【英文标题】:Using 2 CROSS APPLY in XML 【发布时间】:2020-01-21 09:00:48 【问题描述】:我正在使用下面的 XML 来获取 ClauseCode
,但它没有为下面的查询返回任何数据。
<Policy>
<Plans>
<Plan>
<ProductEndorsementClauses>
<ClauseCode>3</ClauseCode>
</ProductEndorsementClauses>
</Plan>
<Plan>
<ProductEndorsementClauses>
<ClauseCode>1</ClauseCode>
<ClauseCode>2</ClauseCode>
</ProductEndorsementClauses>
</Plan>
<Plan>
<ProductEndorsementClauses>
<ClauseCode>5</ClauseCode>
<ClauseCode>4</ClauseCode>
</ProductEndorsementClauses>
</Plan>
</Plans>
</Policy>
这是我的查询:
select proposaid,
,Col1.value('(/*/ProductEndorsementClauses/ClauseCode)[1]','nvarchar(max)')
from Policy p
CROSS APPLY data.nodes('/*/Plans/Plan') AS Tbl(Col)
CROSS APPLY Tbl.Col.nodes('/ProductEndorsementClauses/ClauseCode') AS TblPec(Col1)
where Col1.value('(ProductEndorsementClauses/ClauseCode)[1]', 'nvarchar(max)') in ('1','3')
【问题讨论】:
我假设原因不是因为您实际上拥有proposaid,,Col1.value
(双逗号)?
似乎nodes('/ProductEndorsementClauses/ClauseCode')
应该是nodes('ProductEndorsementClauses/ClauseCode')
(在您遇到的其他印刷错误中)。
我还建议在使用value
时在末尾添加text()
;由于 SQL Server 处理 XML 的方式,它更快。 IE。 MY.MC.value('(MyNode/text())[1]','int')
.
【参考方案1】:
灵感:
declare @x xml = N'
<Policy>
<Plans>
<Plan>
<ProductEndorsementClauses>
<ClauseCode>3</ClauseCode>
</ProductEndorsementClauses>
</Plan>
<Plan>
<ProductEndorsementClauses>
<ClauseCode>1</ClauseCode>
<ClauseCode>2</ClauseCode>
</ProductEndorsementClauses>
</Plan>
<Plan>
<ProductEndorsementClauses>
<ClauseCode>5</ClauseCode>
<ClauseCode>4</ClauseCode>
</ProductEndorsementClauses>
</Plan>
</Plans>
</Policy>'
select @x
--all clause code values
select t.col.value('.[1]', 'nvarchar(10)')
from @x.nodes('Policy/Plans/Plan/ProductEndorsementClauses/ClauseCode') as t(col);
--only clause code values that have at least a 1, 3 clause code in their parent ProductEndorsementClauses
select t.col.value('.[1]', 'nvarchar(10)')
from @x.nodes('Policy/Plans/Plan/ProductEndorsementClauses[(./ClauseCode/text() = "1") or (./ClauseCode/text() = "3")]/ClauseCode') as t(col);
--result only when exists at least a 1 or 3
select t.col.value('.[1]', 'nvarchar(max)')
from @x.nodes('Policy/Plans/Plan/ProductEndorsementClauses/ClauseCode') as t(col)
where @x.exist('Policy/Plans/Plan/ProductEndorsementClauses/ClauseCode[text() = "1" or text() = "3"]') = 1;
declare @Policy table
(
proposaid int,
data xml
);
insert into @Policy(proposaid, data)
values(1, N'
<Policy>
<Plans>
<Plan>
<ProductEndorsementClauses>
<ClauseCode>3</ClauseCode>
</ProductEndorsementClauses>
</Plan>
<Plan>
<ProductEndorsementClauses>
<ClauseCode>1</ClauseCode>
<ClauseCode>2</ClauseCode>
</ProductEndorsementClauses>
</Plan>
<Plan>
<ProductEndorsementClauses>
<ClauseCode>5</ClauseCode>
<ClauseCode>4</ClauseCode>
</ProductEndorsementClauses>
</Plan>
</Plans>
</Policy>'),
(2, N'
<Policy>
<Plans>
<Plan>
<ProductEndorsementClauses>
<ClauseCode>13</ClauseCode>
</ProductEndorsementClauses>
</Plan>
<Plan>
<ProductEndorsementClauses>
<ClauseCode>11</ClauseCode>
<ClauseCode>12</ClauseCode>
</ProductEndorsementClauses>
</Plan>
<Plan>
<ProductEndorsementClauses>
<ClauseCode>15</ClauseCode>
<ClauseCode>14</ClauseCode>
</ProductEndorsementClauses>
</Plan>
</Plans>
</Policy>');
select *
from @Policy;
select p.proposaid,
t.col.value('.[1]', 'nvarchar(max)')
from @Policy as p
cross apply p.data.nodes('Policy/Plans/Plan/ProductEndorsementClauses/ClauseCode') as t(col)
where p.data.exist('Policy/Plans/Plan/ProductEndorsementClauses/ClauseCode[text() = "1" or text() = "3"]') = 1;
【讨论】:
以上是关于在 XML 中使用 2 CROSS APPLY的主要内容,如果未能解决你的问题,请参考以下文章
Hive 上的 CROSS APPLY SQL Server 查询
SQLServer中的cross apply和FOR XML PATH
SQL Server:仅使用一次 Join/Cross Apply 中的记录
如何在 BigQuery 中使用 SPLIT 和 CROSS APPLY 函数