Possible Duplicate:
What is the best way to paginate results in MS SQLServer
I am building stored procedure that should return to me first 15 rows when from database table when it is required.
On user action I need to load next 15 and on each user action load 15 more rows.
This table potently can be very large (over 1.000.000 rows).
I make it this way. I just want to be sure that I am doing this right:
@m_HospitalId int,
@m_Id int
AS
SELECT *
FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY CreatedOnDate ) AS RowNum, *
FROM dbo.Items
WHERE ItemId >= @m_Id
) AS RowConstrainedResult
WHERE HospitalId = @m_HospitalId AND RowNum > 1 AND RowNum < 15
ORDER BY RowNum
I use ROW_NUMBER()
function for the first time and I am not sure if I am doing it right.
Do I always need to pass last ID that I receive to get next 15 items.
In application I handle this when I get collection of 15 items I store 15th item ID in Session
and value from session
I pass to get next 15 items.