SQL : 查找 XML 节点是否存在

SQL : Find if XML node exists

假设我有一个 table 数据如下:

我想要 Select 包含节点 Name="Hello World" 的所有 Value(XML 数据)。我怎样才能实现它?

SQL Fiddle

set @f = @XML.exist('/ArrayOfFilterColumn/SelectColumn[(@Name) eq "Hello World"]');
select @f;

我不确定如何将它添加到我的 where 条件中,所以我将它放在 fiddle.

跳过 XML 变量的使用,在查询 table.

时将 exist 放在 where 子句中
select F.Value
from XML_FILES as F
where F.Value.exist('/ArrayOfArrayOfSelectColumn/SelectColumn[@Name eq "Hello World"]') = 1

您的专栏显然是 text,因此您需要更改它,因为 text 已被弃用并且已经有一段时间了。

ntext, text, and image (Transact-SQL)

ntext , text, and image data types will be removed in a future version of Microsoft SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead.

在您的情况下,您当然应该改为 XML。

在您修复之前可以在查询中强制转换为 XML。

select F.Value
from XML_FILES as F
where cast(F.Value as xml).exist('/ArrayOfArrayOfSelectColumn/SelectColumn[@Name eq "Hello World"]') = 1