例如,我的表中有10.000条记录。每次执行查询时,我都会得到页码n
作为参数。我必须选择从n
到n*100
的记录,这些记录可以满足某些复杂条件。我还使用ORDER BY
,因此我无法保留最后一个元素的PrimaryKey
并使用主键选择前n
个记录。我怎样才能做到这一点?
答案 0 :(得分:4)
根据页面计算,在以下查询中传递From和To参数的值。
WITH NumberedMyTable AS
(
SELECT
*,
ROW_NUMBER() OVER (ORDER BY Id) AS RowNumber
FROM
MyTable
)
SELECT
*
FROM
NumberedMyTable
WHERE
RowNumber BETWEEN @From AND @To
答案 1 :(得分:1)
declare @n int
set @n=2
with my_query as(
select ROW_NUMBER() over (order by name) as ID, *
from sys.tables
)
select *
from my_query
where ID >=@n and ID<=(@n*10)