Last active
December 19, 2015 11:09
-
-
Save marifrahman/5945927 to your computer and use it in GitHub Desktop.
Pagination on the result set returned by a query
This file contains 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
//Before SQL Server 2012 | |
SELECT * FROM LargeTable WHERE indexColumn >= 'something' ORDER BY indexColumn | |
SELECT * | |
FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY [PK] ) AS RowNum, * | |
FROM [funtest].[dbo].[LargeTable] | |
-- WHERE indexColumn >= 'something' | |
) AS RowConstrainedResult | |
WHERE RowNum >= 50 | |
AND RowNum <= 700 | |
ORDER BY RowNum | |
//More details example: | |
With myres as( | |
SELECT ROW_NUMBER() OVER (ORDER BY Joinning DESC) rownum,* FROM ( | |
SELECT | |
LastName, Country, HireDate AS Joinning | |
FROM Employees | |
WHERE Region IS NOT NULL | |
) a | |
) Select * from myres where myres.rownum > 0 and myres.rownum < = 0+20 | |
//For 2012 | |
SELECT * FROM LargeTable ORDER BY indexColumn OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment