SQL:如何获取 XML 数据类型中的属性值? [英] SQL: How can I get the value of an attribute in XML datatype?

查看:70
本文介绍了SQL:如何获取 XML 数据类型中的属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有以下 xml:

I have the following xml in my database:

<email>
  <account language="en" ... />
</email>

我现在正在使用这样的东西:但仍然需要找到属性值.

I am using something like this now: but still have to find the attribute value.

 SELECT convert(xml,m.Body).query('/Email/Account')
 FROM Mail

如何使用 SQL 在我的选择语句中获取 language 属性的值?

How can I get the value of the language attribute in my select statement with SQL?

推荐答案

使用 XQuery:

declare @xml xml =
'<email>
  <account language="en" />
</email>'

select @xml.value('(/email/account/@language)[1]', 'nvarchar(max)')

<小时>

declare @t table (m xml)

insert @t values 
    ('<email><account language="en" /></email>'), 
    ('<email><account language="fr" /></email>')

select m.value('(/email/account/@language)[1]', 'nvarchar(max)')
from @t

输出:

en
fr

这篇关于SQL:如何获取 XML 数据类型中的属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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