解决方案 1:
如果要检查NoSqlField
列是否有//root/entry
元素并且该元素包含1
(内部文本),则可以使用此解决方案:
SET ANSI_WARNINGS ON;
GO
DECLARE @TestData TABLE (
ID INT IDENTITY PRIMARY KEY,
NoSqlField XML NULL
);
INSERT @TestData (NoSqlField)
SELECT NULL
UNION ALL
SELECT '
<root version="1.0">
<entry key="mykey">1</entry>
</root>'
UNION ALL
SELECT '
<root version="1.0">
<entry key="mykey" anotherkey="myanotherkey">1</entry>
</root>'
UNION ALL
SELECT '
<root version="1.0">
<entry key="anotherkey" key2="a">1</entry>
</root>'
UNION ALL
SELECT '
<root version="1.0">
<entry key="mykey" key3="3">22</entry>
</root>';
SELECT *
FROM @TestData t
WHERE t.NoSqlField.exist('//root/entry[@key="mykey"][text() = "1"]') = 1
结果:
ID NoSqlField
-- ---------------------------------------------------------------------------------
2 <root version="1.0"><entry key="mykey">1</entry></root>
3 <root version="1.0"><entry key="mykey" anotherkey="myanotherkey">1</entry></root>
注1:您可以看到此解决方案允许(存在)其他属性(例如anotherkey
)。
解决方案 2:
如果你想要一个严格的过滤器(entry
只有一个属性的元素:),key
那么你可以使用这个查询:
SELECT *
FROM
(
SELECT *
,t.NoSqlField.exist('//root/entry[@key="mykey"][text() = 1]') AS XmlExist
,t.NoSqlField.query('
let $list := //root/entry/@*
for $i in $list
where local-name($i) != "key"
return <ret value="1"/>
').exist('//ret') AS HasAnotherAttribute
FROM @TestData t
) src
WHERE src.XmlExist = 1
AND src.HasAnotherAttribute = 0
结果:
ID NoSqlField XmlExist HasAnotherAttribute
-- ------------------------------------------------------- -------- -------------------
2 <root version="1.0"><entry key="mykey">1</entry></root> 1 0
注意 2:将构建一个包含元素let $list := //root/entry/@*
的所有 (@*) 属性的列表。//root/entry
注 3:本地名称()