0

如何在 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 = 输入变量的所有用户标签

4

1 回答 1

1
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" />
于 2012-07-11T10:10:22.587 回答