Skip to content

Instantly share code, notes, and snippets.

@AndiSHFR
Last active January 30, 2017 09:28
Show Gist options
  • Save AndiSHFR/eae134af7e58696c77de011686ba3ffc to your computer and use it in GitHub Desktop.
Save AndiSHFR/eae134af7e58696c77de011686ba3ffc to your computer and use it in GitHub Desktop.
Paging Example in Sql Server (like mysql LIMIT+OFFSET)
-- Pageing in SQL Server like mysql (LIMIT + OFFSET)
DECLARE @Offset INT = 3
DECLARE @Limit INT = 10
;WITH Results_CTE AS
(
SELECT
COUNT(*) OVER () as TotalRows,
ROW_NUMBER() OVER (ORDER BY name) AS RowNum,
id, name, crdate, refdate
FROM master..sysobjects
WHERE xtype = 'S'
)
SELECT *
FROM Results_CTE
WHERE RowNum >= @Offset
AND RowNum < @Offset + @Limit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment