Tuesday, 6 March 2012

paging in sql server stored procedure


CREATE  PROCEDURE dbo.PagingTest
(
    @PageNumber int,
    @PageSize int
)
AS


DECLARE @FirstId int, @FirstRow int


SET @FirstRow = ( (@PageNumber - 1) * @PageSize ) + 1
SET ROWCOUNT @FirstRow


-- Add check here to ensure that @FirstRow is not
-- greater than the number of rows in the table.


SELECT   @FirstId = [Id]
FROM     dbo.Country
ORDER BY [Id]


SET ROWCOUNT @PageSize


SELECT   *
FROM     dbo.Country
WHERE    [Id] >= @FirstId
ORDER BY [Id]


SET ROWCOUNT 0
GO 


===================================================
Just pass the page number and page size to the procedure.

No comments:

Post a Comment