Search

Saturday, September 10, 2011

Paging with using Offset/Fetch Next Rows of Order by Clause


Check out the following query. for example below i want to fetch row numbers from 101 to 200.
DECLARE @Rows INT = 100 -- Number of rows returns per page
DECLARE @PageNumber INT = 2 -- Page number
SELECT *
FROM  sys.objects
ORDER BY object_id
OFFSET (@Rows * (@PageNumber - 1)) ROWS
FETCH NEXT @Rows ROWS ONLY;
GO
Offset keyword is used to skip rows before retrieve any rows and Fetch next keyword is used to retrieve specified number of rows.

No comments:

Post a Comment