SQLServer中的行偏移量SQLServer中是否有任何方法从给定的偏移量开始获取结果?例如,在另一种类型的SQL数据库中,可以这样做:SELECT * FROM MyTable OFFSET 50 LIMIT 25才能得到51-75的结果。此结构似乎不存在于SQLServer中。在不加载我不关心的所有行的情况下,我如何做到这一点?谢谢!
3 回答
蛊毒传说
TA贡献1895条经验 获得超3个赞
SELECT *
SQLServer 2005+
SELECT col1, col2 FROM ( SELECT col1, col2, ROW_NUMBER() OVER (ORDER BY ID) AS RowNum FROM MyTable) AS MyDerivedTableWHERE MyDerivedTable.RowNum BETWEEN @startRow AND @endRow
SQLServer 2000
摇曳的蔷薇
TA贡献1793条经验 获得超6个赞
TOP (25) ... WHERE Key > @last_key ORDER BY Key
ROW_NUMBER
BETWEEN
SELECT *FROM MyTable ORDER BY OrderingColumn ASC OFFSET 50 ROWS FETCH NEXT 25 ROWS ONLY
慕标琳琳
TA贡献1830条经验 获得超9个赞
SELECT * FROM( SELECT TOP (@pageSize) * FROM ( SELECT TOP (@pageNumber * @pageSize) * FROM tableName ORDER BY columnName ASC ) AS t1 ORDER BY columnName DESC) AS t2 ORDER BY columnName ASC
;WITH results AS ( SELECT rowNo = ROW_NUMBER() OVER( ORDER BY columnName ASC ) , * FROM tableName ) SELECT * FROM resultsWHERE rowNo between (@pageNumber-1)*@pageSize+1 and @pageNumber*@pageSize
添加回答
举报
0/150
提交
取消