存储过程定义:
CREATE
procedure
[
dbo
]
.
[
SplitPage
]
(
@SelectCommandText nvarchar ( 4000 ), -- 要执行的查询命令
@CurrentPageIndex int = 0 , -- 当前页的索引,从 0 开始
@PageSize int = 20 , -- 每页的记录数
@RowCount int = 0 out, -- 总的记录数
@PageCount int = 0 out -- 总的页数
)
AS
IF @PageSize <= 0
BEGIN
RAISERROR ( ' 参数 PageSize 必须大于零。 ' , 16 , 1 );
RETURN
END
DECLARE @p1 int
DECLARE @RowIndex int
SET @CurrentPageIndex = @CurrentPageIndex + 1
EXEC sp_cursoropen
@p1 output,
@SelectCommandText ,
@scrollopt = 1 ,
@ccopt = 1 ,
@RowCount = @RowCount output;
SET @PageCount = ceiling ( 1.0 * @RowCount / @PageSize );
SET @RowIndex = ( @CurrentPageIndex - 1 ) * @PageSize + 1
EXEC sp_cursorfetch
@p1 ,
16 ,
@RowIndex ,
@PageSize ;
EXEC sp_cursorclose
@p1
(
@SelectCommandText nvarchar ( 4000 ), -- 要执行的查询命令
@CurrentPageIndex int = 0 , -- 当前页的索引,从 0 开始
@PageSize int = 20 , -- 每页的记录数
@RowCount int = 0 out, -- 总的记录数
@PageCount int = 0 out -- 总的页数
)
AS
IF @PageSize <= 0
BEGIN
RAISERROR ( ' 参数 PageSize 必须大于零。 ' , 16 , 1 );
RETURN
END
DECLARE @p1 int
DECLARE @RowIndex int
SET @CurrentPageIndex = @CurrentPageIndex + 1
EXEC sp_cursoropen
@p1 output,
@SelectCommandText ,
@scrollopt = 1 ,
@ccopt = 1 ,
@RowCount = @RowCount output;
SET @PageCount = ceiling ( 1.0 * @RowCount / @PageSize );
SET @RowIndex = ( @CurrentPageIndex - 1 ) * @PageSize + 1
EXEC sp_cursorfetch
@p1 ,
16 ,
@RowIndex ,
@PageSize ;
EXEC sp_cursorclose
@p1
调用方法:
DECLARE
@return_value
int
,
@RowCount int ,
@PageCount int
EXEC @return_value = [ dbo ] . [ SplitPage ]
@SelectCommandText = N ' SELECT * FROM Log ' ,
@CurrentPageIndex = 0 ,
@PageSize = 4 ,
@RowCount = @RowCount OUTPUT,
@PageCount = @PageCount OUTPUT
SELECT @RowCount as N ' @RowCount ' ,
@PageCount as N ' @PageCount '
SELECT ' Return Value ' = @return_value
GO
@RowCount int ,
@PageCount int
EXEC @return_value = [ dbo ] . [ SplitPage ]
@SelectCommandText = N ' SELECT * FROM Log ' ,
@CurrentPageIndex = 0 ,
@PageSize = 4 ,
@RowCount = @RowCount OUTPUT,
@PageCount = @PageCount OUTPUT
SELECT @RowCount as N ' @RowCount ' ,
@PageCount as N ' @PageCount '
SELECT ' Return Value ' = @return_value
GO