最简单的存储过程分页

来源:互联网 发布:手机屏幕唤醒软件 编辑:程序博客网 时间:2024/05/16 12:12

最简单的SQL Server数据库存储过程分页:
1.只需要提供Sql语句和每页的记录数,页数就可以了
2,速度超快哟,100W记录1~3秒就分出来了 
3,对于存储过程特别好用 

Create procedure PagingDataBySql 

@strSql varchar(8000), 
@nPageSize int,   --每页记录数
@nPageCount int  -- 当前页索引/第几页
as 
SET NOCOUNT ON 
DECLARE @P1 INT, @nRowCount INT 
--//注意:@scrollopt = 1 会取得Select的时候的总行数 
EXEC sp_cursoropen @P1 OUTPUT, @strSql, @scrollopt = 2, @ccopt = 335873, @rowcount = @nRowCount OUTPUT 
IF (@P1 != 0) 
BEGIN 
--SELECT @nRowCount AS nRecordCount, ceiling(1.0 * @nRowCount / @nPageSize) AS nPageCount, @nPageCount AS nPage 
SET @nPageCount = (@nPageCount - 1) * @nPageSize + 1 
EXEC sp_cursorfetch @P1, 32, @nPageCount, @nPageSize 
EXEC sp_cursorclose @P1 
END 
GO 

---------

--//调用的方式 

exec PagingDataBySql  'select * from 表',10,3 

存储过程
exec PagingDataBySql 'exec 存储过程',10,1 


原创粉丝点击