Let me know what you think about the following code:
DECLARE @MaxIdValue int
DECLARE @MaxSortFieldValue nvarchar(50)
SELECT TOP 1 @MaxIdValue = [id], @MaxSortFieldValue = [SortField]
FROM (
SELECT TOP PageNumber*RowsPerPage [id], [SortField]
FROM MyTable
WHERE (FilterCondition) ORDER BY [SortField], [id]
) T
ORDER BY [SortField] DESC, [id] DESC
SELECT TOP RowsPerPage * FROM MyTable
WHERE ([SortField] >= @MaxSortFieldValue) AND (([id] > @MaxIdValue) OR
([SortField] <> @MaxSortFieldValue)) AND (FilterCondition)
ORDER BY [SortField], [id]
This is a dynamic SQL and it should be easily fixable.
PageNumber, RowsPerPage, FilterCondition and SortField are going to be
the variables and will be based on the user's search
condition/criteria.
-----------------------------
Thanks for you attention.