Skip to content
Advertisement

How to do pagination in SQL Server 2008

How do you do pagination in SQL Server 2008 ?

Advertisement

Answer

You can try something like

DECLARE @Table TABLE(
        Val VARCHAR(50)
)

DECLARE @PageSize INT,
        @Page INT

SELECT  @PageSize = 10,
        @Page = 2

;WITH PageNumbers AS(
        SELECT Val,
                ROW_NUMBER() OVER(ORDER BY Val) ID
        FROM    @Table
)
SELECT  *
FROM    PageNumbers
WHERE   ID  BETWEEN ((@Page - 1) * @PageSize + 1)
        AND (@Page * @PageSize)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement