問題描述
我在文檔"表中有一個(gè)元數(shù)據(jù)"字段,其中包含以下數(shù)據(jù):
I have a 'Metadata' field in table 'Document' that has the following data:
<properties>
<property name="someProp">xyz</property>
<property name="reportId">55</property>
<property name="someOtherProp">abc</property>
</properties>'
如何編寫一個(gè)查詢來返回存在名稱為reportId"的屬性元素并且reportId"屬性元素的值為 55 的記錄?有時(shí)reportId"屬性節(jié)點(diǎn)是唯一存在的節(jié)點(diǎn),有時(shí)不存在,并且它并不總是按上述順序排列,因此我無法查詢絕對位置.有什么想法嗎?
How can I write a query that returns records where a property element exists with a name of "reportId" and that "reportId" property element has a value of 55? Sometimes the "reportId" property node is the only one that exists, sometimes not, and it's not always in the above order so I can't query on absolute positions. Any ideas?
推薦答案
不需要提取值.使用 exist() 方法(xml 數(shù)據(jù)類型) 代替.
There is no need to extract the value. Use exist() Method (xml Data Type) instead.
select *
from Document
where Metadata.exist('/properties/property[@name="reportId" and . = 55]') = 1
這篇關(guān)于SQL Server XML 列上的 Where 子句過濾屬性 AND 值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!