通过存储过程分页(小技巧)

DECLARE @PageNumber int
DECLARE @PageSize int

DECLARE @Ignore int
DECLARE @LastID int

SELECT @PageNumber = 2, @PageSize = 15
 
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

posted @ 2004-06-17 11:58  活力豆  阅读(154)  评论(0编辑  收藏  举报