基于参数的分页(OFFSET FETCH NEXT)

时间:2017-05-19 05:41:45

标签: sql-server

当参数为空时,如何显示所有数据

DECLARE @PageSize INT = 5
        ,@PageNo  INT = 2

SELECT *
FROM [MyTable]
ORDER BY [NO]
OFFSET @PageSize * (@PageNo - 1) ROWS
FETCH NEXT @PageSize ROWS ONLY

1 个答案:

答案 0 :(得分:2)

你可以试试这个

SET @PageSize = ISNULL(@PageSize, 2147483647) -- max int
SET @PageNo = ISNULL(@PageNo, 1)

SELECT *
FROM [MyTable]
ORDER BY [NO]
OFFSET @PageSize * (@PageNo - 1) ROWS
FETCH NEXT @PageSize ROWS ONLY