且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从 SQL Server 中的 XML 元素获取特定属性

更新时间:2022-11-27 22:04:46

尝试使用 .value 函数代替 .query :

Try using the .value function instead of .query:

SELECT 
  xmlCol.value('(/container/param[@name="paramB"]/@value)[1]', 'varchar(50)') 
FROM  
  LogTable

XPath 表达式可能返回一个节点列表,因此您需要向该潜在列表添加一个 [1] 以告诉 SQL Server 使用这些条目中的第一个(是的 - 即列表是基于 1 的 - 不是基于 0 的).作为第二个参数,您需要指定该值应该转换为什么类型——这里只是猜测.

The XPath expression could potentially return a list of nodes, therefore you need to add a [1] to that potential list to tell SQL Server to use the first of those entries (and yes - that list is 1-based - not 0-based). As second parameter, you need to specify what type the value should be converted to - just guessing here.

马克