I have almost 1 mill roes and he size keeps growing. In order not to delay the query I need to display Top 100 followed by 101-200 followed by 201 -300 etc.I am not loading all the data at one time .I need the 100 rows first and then on button click retrieve the next 100 and so on .I have to dispay the data in a single view so paging is not allowed in the grid.Thank you !
In SQL 2008, you can use a cte and rownumber to get the records in batches of x records. See the sample below: DECLARE @start INT = 1 DECLARE @batchSize INT = 100 ;WITH ctePage AS ( SELECT ROW_NUMBER() OVER(ORDER BY
cp.id ) AS recordno, * FROM dwh.counterpart cp WHERE cp.reportDate = '2012-05-20' ) SELECT * FROM ctePage WHERE [ctePage].[recordno] BETWEEN @start AND @start + @batchSize In SQL 2012, you can use OFFSET in the order by clause, without the need for the CTE and rownumber.