TSQL where on xml 数据类型 [英] TSQL where on xml data type

查看:29
本文介绍了TSQL where on xml 数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Sql Server 2005 中对 xml 数据类型执行 where 子句命令?

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

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

查询

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

预期输出

获取属性 UserId = 输入变量的所有用户标签

Intended Output

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

推荐答案

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" />

这篇关于TSQL where on xml 数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆