在xquery中选择rownumber并将其转换为sql行

在xquery中选择rownumber并将其转换为sql行,sql,sql-server,xml,sql-server-2008,xquery,Sql,Sql Server,Xml,Sql Server 2008,Xquery,我有一个以xml格式保存用户搜索结果行的表,如下所示: <row id="5083" /> <row id="5085" /> <row id="5087" /> <row id="5090" /> <row id="5094" /> ... (about 500,000 rows) 第二个问题是如何将结果ID作为sql行而不是xmlnodelist获取? for $x in (row) where $x/position() >

我有一个以xml格式保存用户搜索结果行的表,如下所示:

<row id="5083" />
<row id="5085" />
<row id="5087" />
<row id="5090" />
<row id="5094" />
... (about 500,000 rows)
第二个问题是如何将结果ID作为sql行而不是xmlnodelist获取?

for $x in (row) where $x/position() > 10 return ($x)
$x在第10行之后迭代所有行子元素,但对于每个子元素,返回一个长度为1的序列,其中只有一个行元素,因此$x/position()始终为1

你可以用

row[position() >=10 and position() < 20]
行[position()>=10和position()<20]
它将返回位置10到19的元素。如果只需要id而不是元素节点,那么

row[position() >=10 and position() < 20]/string(@id)
行[position()>=10和position()<20]/string(@id)
您可以使用分解XML并使用提取属性
id
的值

查询1

declare @results xml = '
<row id="5083" />
<row id="5085" />
<row id="5087" />
<row id="5090" />
<row id="5094" />'

select T.N.value('@id', 'int') as id
from @results.nodes('row[position() >= 2 and position() < 4]') as T(N)
|   ID |
--------
| 5085 |
| 5087 |

谢谢你的回答。。。它帮助了很多。。。但是字符串函数出现了一个异常:XQuery[query()]:不支持XQuery语法“/function()”。您必须使用非标准或已损坏的XQuery引擎,但在这种情况下,您可能只需省略string()函数,并在最后使用
/@id
|   ID |
--------
| 5085 |
| 5087 |