新人开博,先来个存储过程。
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [YR_DataPaging]
@sql NVARCHAR(4000), --查询字符串
@currentpage INT = 1, --当前页
@pagesize INT = 10, --每页行数
@pagecount INT = 0 OUT, --总页数
@totalcount INT = 0 OUT --总记录数
AS
SET NOCOUNT ON
DECLARE @p1 INT --P1是游标的id
EXEC sp_cursoropen @p1 OUTPUT, @sql, @scrollopt = 1, @ccopt=1, @rowcount = @pagecount OUTPUT
SELECT @totalcount = @pagecount
, @pagecount = CEILING(1.0 * @pagecount / @pagesize)
, @currentpage = (@currentpage - 1) * @pagesize + 1
EXEC sp_cursorfetch @p1, 16, @currentpage, @pagesize
EXEC sp_cursorclose @p1