Paging Query Using RowNumber Function

Although we have many ways to do paging query in SQL Server (with 'not exist', 'not in'), SQL Server provide an inner function to help us to do paging query after SQL Server 2005. Below is a very simple example.

Conditions
1.We have a Table1,
2.We want to do paging query on records order by start time then by create time.
3.We'd like to query records between startIndex and endIndex.


SELECT * FROM ( 
			SELECT ROW_NUMBER() OVER (ORDER BY [t0].StartTime desc, [t0].CreateTime desc )
                              AS [ROW_NUMBER], [t0].*                       
                              FROM table1 AS [t0]) 
                          AS [t1]                       
                          WHERE [t1].[ROW_NUMBER] BETWEEN {startIndex} AND {endIndex}
                          ORDER BY [t1].[ROW_NUMBER];

你可能感兴趣的:(sql,SQL Server)