In PostgreSQL there is the Limit
and Offset
keywords which will allow very easy pagination of result sets.
What is the equivalent syntax for Sql Server?
In PostgreSQL there is the Limit
and Offset
keywords which will allow very easy pagination of result sets.
What is the equivalent syntax for Sql Server?
In SQL server you would use TOP together with ROW_NUMBER()
This feature is now made easy in SQL Server 2012. This is working from SQL Server 2012 onwards.
Limit with offset to select 11 to 20 rows in SQL Server:
OFFSET
: number of skipped rowsNEXT
: required number of next rowsReference: https://docs.microsoft.com/en-us/sql/t-sql/queries/select-order-by-clause-transact-sql?view=sql-server-2017
There is here someone telling about this feature in sql 2011, its sad they choose a little different keyword "OFFSET / FETCH" but its not standart then ok.
The equivalent of
LIMIT
isSET ROWCOUNT
, but if you want generic pagination it's better to write a query like this:The advantage here is the parameterization of the offset and limit in case you decide to change your paging options (or allow the user to do so).
Note: the
@Offset
parameter should use one-based indexing for this rather than the normal zero-based indexing.Adding a slight variation on Aaronaught's solution, I typically parametrize page number (@PageNum) and page size (@PageSize). This way each page click event just sends in the requested page number along with a configurable page size: