按照******排序
跳过 @ItemsPerPage * (@CurrentPage - 1) 行
只获取接下来的 @ItemsPerPage 行
DECLARE @CurrentPage int = 1;
DECLARE @ItemsPerPage int = 10;
SELECT DISTINCT m.logID
FROM Monitor_data m
inner join Monitor_object o on (o.objID = m.domainID)
inner join Monitor_event e on (e.mainID = m.logID)
WHERE (o.name = @objName
and m.service = @service
and e.statement = @statement
and m.start >= @start
and m.end <= @end)
ORDER BY m.logID
OFFSET @ItemsPerPage * (@CurrentPage - 1) ROWS
FETCH NEXT @ItemsPerPage ROWS ONLY
分页示例
DECLARE @myTable TABLE(Id int, Name nvarchar(50),EventDate date);
INSERT INTO @myTable(Id, Name, EventDate)
VALUES (1, 'a', '2018-01-01'),
(2, 'b', '2018-01-02'),
(3, 'c', '2018-01-03'),
(4, 'd', '2018-01-04'),
(5, 'e', '2018-01-05'),
(6, 'f', '2018-01-06');
DECLARE @CurrentPage int = 1;
DECLARE @ItemsPerPage int = 4;
SELECT * FROM @myTable
ORDER BY EventDate DESC
OFFSET @ItemsPerPage * (@CurrentPage - 1) ROWS
FETCH NEXT @ItemsPerPage ROWS ONLY
OFFSET
和FETCH
是在2012年引入的。如果你正在运行2008R2,那么你现在的首要任务应该是迁移到其他版本,因为它将在一年内完全停止支持。 - alroc