TSQL where on xml data type

雨燕双飞 提交于 2019-12-25 05:06:54

问题


How does one execute where clause command on a xml data type in Sql Server 2005 ?

Xml Structure

<User UserId="1" UserName="x">
<User UserId="2" UserName="y">

Query

SELECT XmlColumn from Table where XmlColumn.query('/User[@UserId'+ @dynamicValue +']')

Intended Output

Get all the user tags where the attribute UserId = input variable


回答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

Result:

XMLColumn
---------------------------------
<User UserId="1" UserName="x" />


来源:https://stackoverflow.com/questions/11430187/tsql-where-on-xml-data-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!