Urggggg! I\'ve been struggling with this for a long time! I can do it with MySQL so easy but not with SQL Server :(
Here are the simplified tables which should be jo
We can achieve the same by CTE (Common Table Expressions).First we need to set current page number and offset of the result from which we need to fetch.Then we have to order the result set by ROW_NUMBER.and store the result by using cte feature.then filter the result with page size and offset against Row number. The SQL query is as follows
DECLARE @PageSize INT=1 ,@PageNumber INT=2
DECLARE @Offset int =(@PageSize * (@PageNumber - 1))+1
;WITH Results_CTE AS
(
SELECT *,
ROW_NUMBER() OVER (ORDER BY FieldName) AS RowNum
FROM TABLENAME
)
SELECT *
FROM Results_CTE
WHERE RowNum>=@Offset AND RowNum < @Offset + @PageSize
END
https://amonghorizon.blogspot.com/2020/07/sql-server-query-for-pagination-control.html