闲坐敲棋

有约不来过夜半,闲敲棋子落灯花

导航

分页存储过程

Posted on 2009-11-25 15:25  闲坐敲棋  阅读(143)  评论(0编辑  收藏  举报
Create  [dbo].[PageView]
 @sql NVARCHAR(4000),
 @CurrentPage INT,
 @PageSize INT
AS
BEGIN
 DECLARE @p1 INT
 DECLARE @RecordCurrent INT
 DECLARE @PageCount INT
 DECLARE @RecordCount INT
 SET NOCOUNT ON
 EXEC sp_cursoropen @cursor=@p1 OUTPUT,@stmt=@sql,@scrollopt=1,@ccopt=1,@rowcount=@RecordCount OUTPUT
 SET @PageCount=(@RecordCount+@PageSize-1)/@PageSize
 IF ISNULL(@CurrentPage,0)<1
  SET @CurrentPage=1
 ELSE if ISNULL(@CurrentPage,0)>@PageCount
  SET @CurrentPage=@PageCount
 SELECT @CurrentPage AS CurrentPage,@RecordCount AS RecordCount,@PageSize AS PageSize,@PageCount AS PageCount
 SET @RecordCurrent=(@CurrentPage-1)*@PageSize+1
 EXEC sp_cursorfetch @p1,16,@RecordCurrent,@PageSize
 EXEC sp_cursorclose @p1
END