/*
分页存储过程
利用数据库游标
*/
CREATE PROCEDURE [dbo].[p_s_SplitPageByCursor]
@Sql varchar(8000), --查询字符串
@CurrentPage int, --当前页
@PageSize int, --每页记录条数
@RecordCount int output--总记录条数
AS
SET NOCOUNT ON
DECLARE @P1 int --P1是游标的id
EXEC sp_cursoropen @P1 output,@Sql,@scrollopt=1,@ccopt=1,@rowcount=@RecordCount output
DECLARE @TotalPage int --总页数
SET @TotalPage = ceiling(1.0*@RecordCount/@PageSize)
IF @CurrentPage>@TotalPage
BEGIN
SET @CurrentPage = @TotalPage
END
DECLARE @BeginIndex int
SET @BeginIndex=(@CurrentPage-1)*@PageSize+1
EXEC sp_cursorfetch @P1,16,@BeginIndex,@PageSize
EXEC sp_cursorclose @P1
SET NOCOUNT OFF
GO
-----调用
exec p_s_SplitPageByCursor 'select * from tab where 条件',2,20,120。