加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

使用t-SQL检索XML元素名称

发布时间:2020-12-12 16:14:52 所属栏目:MsSql教程 来源:网络整理
导读:如果我有: quotes quote namejohn/name contentsomething or other/content /quote quote namemary/name contentrandom stuff/content /quote/quotes 如何使用T-SQL获取元素名称’name’和’content’的列表? 到目前为止,最好的是: declare @xml xmlset @x
如果我有:
<quotes>
  <quote>
    <name>john</name>
    <content>something or other</content>
  </quote>
  <quote>
    <name>mary</name>
    <content>random stuff</content>
  </quote>
</quotes>

如何使用T-SQL获取元素名称’name’和’content’的列表?

到目前为止,最好的是:

declare @xml xml
set @xml = ...
select r.value('quotes/name()[1]','nvarchar(20)' as ElementName
from @xml.nodes('/quotes') as records(r)

但是,当然,我不能让这个工作.

解决方法

其实对不起,我最好的是:
select distinct r.value('fn:local-name(.)','nvarchar(50)') as t
FROM
    @xml.nodes('//quotes/*/*') AS records(r)

猜我回答了我自己的问题

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读