Skip to content

Instantly share code, notes, and snippets.

@mystikraz
Created July 29, 2022 03:19
Show Gist options
  • Select an option

  • Save mystikraz/cae445230271f6875e375aecbd5f438b to your computer and use it in GitHub Desktop.

Select an option

Save mystikraz/cae445230271f6875e375aecbd5f438b to your computer and use it in GitHub Desktop.
Pagination in stored procedure
DECLARE @PageNumber AS INT
DECLARE @RowsOfPage AS INT
DECLARE @SortingCol AS VARCHAR(100) ='FruitName'
DECLARE @SortType AS VARCHAR(100) = 'DESC'
SET @PageNumber=1
SET @RowsOfPage=4
SELECT FruitName,Price FROM SampleFruits
ORDER BY
CASE WHEN @SortingCol = 'Price' AND @SortType ='ASC' THEN Price END ,
CASE WHEN @SortingCol = 'Price' AND @SortType ='DESC' THEN Price END DESC,
CASE WHEN @SortingCol = 'FruitName' AND @SortType ='ASC' THEN FruitName END ,
CASE WHEN @SortingCol = 'FruitName' AND @SortType ='DESC' THEN FruitName END DESC
OFFSET (@PageNumber-1)*@RowsOfPage ROWS
FETCH NEXT @RowsOfPage ROWS ONLY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment