Created
July 29, 2022 03:19
-
-
Save mystikraz/cae445230271f6875e375aecbd5f438b to your computer and use it in GitHub Desktop.
Pagination in stored procedure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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