SQL Server - 用于 XML 的 XQuery [英] SQL Server - XQuery for XML
问题描述
与其他帖子类似,我需要根据 Xml 列上的应用条件从表中检索任何行,例如,假设您有一个像这样的 xml 列:
Just similar other post, I need to retrieve any rows from table applying criteria on Xml column, for instance, supposing you have an xml column like this:
<DynamicProfile xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/WinTest">
<AllData xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<d2p1:KeyValueOfstringstring>
<d2p1:Key>One</d2p1:Key>
<d2p1:Value>1</d2p1:Value>
</d2p1:KeyValueOfstringstring>
<d2p1:KeyValueOfstringstring>
<d2p1:Key>Two</d2p1:Key>
<d2p1:Value>2</d2p1:Value>
</d2p1:KeyValueOfstringstring>
</AllData>
</DynamicProfile>
我的查询将能够返回节点值 <d2p1:Key> 的所有行.= 'some key value'
AND 节点值 <d2p1Value = 'some value value'
.
My query would be able to return all rows where node value <d2p1:Key> = 'some key value'
AND node value <d2p1Value = 'some value value'
.
把它想象成一个动态表,其中 KEY 节点代表列名,Value 节点代表列的值.
Imagine of that just as a dynamic table where KEY node represent the column name and Value node represent column's value.
以下查询不起作用,因为键节点和值节点不是连续的:
The following query does not work because key and value nodes are not sequential:
select * from MyTable where
MyXmlField.exist('//d2p1:Key[.="One"]') = 1
AND MyXmlField.exist('//d2p1:Value[.="1"]') = 1
推荐答案
而不是寻找 //d2p1:key[.="One"]
和 //d2p1:Value[.="1"]
作为两个单独的搜索,执行一次同时查找两个的查询,如下所示:
Instead of looking for //d2p1:key[.="One"]
and //d2p1:Value[.="1"]
as two separate searches, do a single query that looks for both at once, like so:
//d2p1:KeyValueOfstringstring[./d2p1:Key="One"][./d2p1:Value=1]
这篇关于SQL Server - 用于 XML 的 XQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!