简体   繁体   English

SQL Server - XQuery for XML

[英]SQL Server - XQuery for 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: 就像其他类似的帖子一样,我需要从Xml列上的表应用条件中检索任何行,例如,假设你有一个像这样的xml列:

<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>

My query would be able to return all rows where node value <d2p1:Key> = 'some key value' AND node value <d2p1Value = 'some value value' . 我的查询将能够返回节点值<d2p1:Key> = 'some key value'和节点值<d2p1Value = 'some value value'

Imagine of that just as a dynamic table where KEY node represent the column name and Value node represent column's value. 想象一下,就像动态表一样,KEY节点表示列名,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"]作为两个单独的搜索,执行一次查找两个查询的查询,如下所示:

//d2p1:KeyValueOfstringstring[./d2p1:Key="One"][./d2p1:Value=1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM