T-SQL、XQuery 无效列

2021-09-10 00:00:00 tsql sql-server xquery-sql

我只是在学习 XQUERY 并试图完成我认为非常简单的事情.这是我的 T-SQL 代码:

I'm just learning XQUERY and trying to accomplish what I thought would be very simple. Here's my T-SQL code:

DECLARE @XML xml
set @xml = '<resultsets><AppVersion>13.0</AppVersion></resultsets>'

-- Code below is wrong
select 
ResultSets.AppVersion.query('AppVersion').value('.', 'varchar(100)') as AppVersion
from @XML.nodes('/resultsets/AppVersion') ResultSets(AppVersion)

我不知道如何查询内部元素 appversion.我没有收到任何错误,但我无法在 appversion 内部元素中返回 13.0.有人可以帮忙吗?

I cannot figure out exactly how to query that inner element, appversion. I'm not getting any errors, but I'm unable to return that 13.0 within the appversion inner-element. Can someone please help?

推荐答案

如果您只想要一行,则无需使用 nodes.

If you only want one row as a result there is no need to use nodes.

DECLARE @XML xml
set @xml = '<resultsets><AppVersion>13.0</AppVersion></resultsets>'

select @XML.value('(/resultsets/AppVersion)[1]', 'varchar(100)') as AppVersion

相关文章