CREATE PROCEDURE dbo.GetDocuments
@PageSize int,
@PageNumber int
AS
DECLARE @Ignore int
DECLARE @LastID int
IF @PageNumber > 1
BEGIN
/* For pages > 1 compute how many records to ignore,
set ROWCOUNT and SELECT ID into @LastID */
SET @Ignore = @PageSize * (@PageNumber - 1)
SET ROWCOUNT @Ignore
SELECT @LastID = ID
FROM Documents
ORDER BY ID DESC
END
ELSE
BEGIN
/* For page #1 just set rowcount to pagesize */
SET ROWCOUNT @PageSize
END
/* Set rowcount to @PageSize and
SELECT page for output (note the WHERE clause) */
SET ROWCOUNT @PageSize
SELECT *
FROM Documents
WHERE ID < @LastID
ORDER BY ID DESC
SET ROWCOUNT 0
GO