Fast SQL Server Paging
Just a quick tip if you ever need to implement paging in MSSQL. Here is a simple example of the query, which gets page x of size y and gets the total row count as well.
SELECT ID, Name, [RowCount] = COUNT(*) OVER()
FROM MyTable
WHERE Name LIKE '%Example%'
ORDER BY ID
OFFSET @Page ROWS
FETCH NEXT @PageSize ROWS ONLY
This must be used with an ORDER BY
clause. The @Page
and @PageSize
determine which set of records are returned out of the total result set.