如何在 Sql Server 2005 中对 xml 数据类型执行 where 子句命令?
xml结构
<User UserId="1" UserName="x">
<User UserId="2" UserName="y">
询问
SELECT XmlColumn from Table where XmlColumn.query('/User[@UserId'+ @dynamicValue +']')
预期输出
获取属性 UserId = 输入变量的所有用户标签
如何在 Sql Server 2005 中对 xml 数据类型执行 where 子句命令?
<User UserId="1" UserName="x">
<User UserId="2" UserName="y">
SELECT XmlColumn from Table where XmlColumn.query('/User[@UserId'+ @dynamicValue +']')
获取属性 UserId = 输入变量的所有用户标签
declare @T table
(
XMLColumn xml
)
insert into @T values ('<User UserId="1" UserName="x"/>')
insert into @T values ('<User UserId="2" UserName="y"/>')
declare @UserID int
set @UserID = 1
select XMLColumn
from @T
where XMLColumn.exist('/User[@UserId = sql:variable("@UserID")]') = 1
结果:
XMLColumn
---------------------------------
<User UserId="1" UserName="x" />