sql pagination to fetch 20 record for each page

 -- Assume we have a table called Products with columns Id, Name, and Price

-- Assume we want to order the records by Price in descending order

-- Assume we want to fetch the records for page number @PageNum


-- Declare a variable for the page number

DECLARE @PageNum INT = 1 -- Change this value as needed


-- Declare a variable for the page size

DECLARE @PageSize INT = 20


-- Use OFFSET and FETCH clauses to skip and limit the number of records

SELECT Id, Name, Price

FROM Products

ORDER BY Price DESC

OFFSET (@PageNum - 1) * @PageSize ROWS -- Skip the previous pages

FETCH NEXT @PageSize ROWS ONLY -- Limit the current page


Vikash Chauhan

C# & .NET experienced Software Engineer with a demonstrated history of working in the computer software industry.

Post a Comment

Previous Post Next Post

Contact Form