sql - 从 SQL XML 中提取字段

标签 sql xml t-sql xpath xquery

我已经在这个网站和互联网上进行了搜索,但似乎无法找到任何可以满足这个简单请求的东西。我只想显示与术语相关的单词,例如使用 TEST 下面的 xml 列将被提取。

xml列如下:

<A>
  <resources id="1" resourceType="Template" term="TEST" version="1" />
</A>

我尝试使用以下命令,但它返回 NULL。

select 
Res.value('(/A/term)[1]','nvarchar(50)') as 'Res'

from Con
where Res is not null

提前谢谢您。

最佳答案

试试这样

select 
Res.value('(/A/resources/@term)[1]','nvarchar(50)') as 'Res'

from Con
where Res is not null

term是元素 @ 内的一个属性(需要 <resources> 符号)下面<A> 。您的 xpath 无法正确反射(reflect)这一点...

关于sql - 从 SQL XML 中提取字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49167864/

相关文章:

sql - 存储过程 - 将两个值传递给存储过程

mysql - 反向SQL查询可能吗?

sql - 如何在 WHERE 子句中编写带有 SELECT 语句的 SQL DELETE 语句?

xml - REXML 保留属性顺序

java - 使用 xpath 查询获取具有特定值的元素

t-sql - linq to nhibernate 中的条件行计数不起作用

sql-server - 我们可以在 select 语句中使用 EXEC()

sql - 如何在查询中多次使用同一参数

sql - 需要提交 SQL 双 "commit"

css - 玛根托 : how to properly move custom simple products options in the right column on product page?