What is the best way (performance wise) to paginate results in SQL Server 2000, 2005, 2008, 2012 if you also want to get the total number of results (before paginating)?
Sql – the best way to paginate results in SQL Server
paginationperformancesqlsql-server
Related Question
- Sql-server – Check if table exists in SQL Server
- Sql – Parameterize an SQL IN clause
- Sql-server – the best way to auto-generate INSERT statements for a SQL Server table
- Sql – DateTime2 vs DateTime in SQL Server
- Sqlite – Improve INSERT-per-second performance of SQLite
- Javascript – the difference between call and apply
- Sql – How to UPDATE from a SELECT in SQL Server
- Sql – How to delete using INNER JOIN with SQL Server
Best Solution
Finally, Microsoft SQL Server 2012 was released, I really like its simplicity for a pagination, you don't have to use complex queries like answered here.
For getting the next 10 rows just run this query:
https://docs.microsoft.com/en-us/sql/t-sql/queries/select-order-by-clause-transact-sql#using-offset-and-fetch-to-limit-the-rows-returned
Key points to consider when using it:
ORDER BY
is mandatory to useOFFSET ... FETCH
clause.OFFSET
clause is mandatory withFETCH
. You cannot useORDER BY ... FETCH
.TOP
cannot be combined withOFFSET
andFETCH
in the same query expression.