SqlServer内部的分页功能,强啊!!

1)只需要提供Sql语句和每页的记录数,页数就可以了
2)速度超快哟,100W记录1~3秒就分出来了
3)对于存储过程特别好用
--//调用的方式

exec up_zbh_DivPageBySql 'select * from 表',10,3
存储过程
exec up_zbh_DivPageBySql 'exec 存储过程',10,1

--//我吧它封装成一个存储过程,调用的时候方便的很哈!!

alter procedure Sp_DivPageBySql
 @strSql varchar(8000),
 @nPageSize int,--每页记录数
 @nPageNo int--第x页
as
/*
调用方法:Exec Sp_DivPageBySql 'select * from td_dwzl order by E0',20,3
*/
    SET NOCOUNT ON
    DECLARE @P1 INT, @nRowCount INT,@nStartRecNo Int

    --//注意:@scrollopt = 1 会取得Select的时候的总行数
    EXEC sp_cursoropen @P1 OUTPUT, @strSql, @scrollopt = 2, @ccopt = 335873, @rowcount = @nRowCount OUTPUT
    Print @nRowCount

    IF (@P1 != 0)
    BEGIN
 --SELECT @nRowCount AS nRecordCount, ceiling(1.0 * @nRowCount / @nPageSize) AS nPageCount, @nPageCount AS nPage
 SET @nStartRecNo = (@nPageNo - 1) * @nPageSize + 1
 EXEC sp_cursorfetch @P1, 32, @nStartRecNo, @nPageSize 
 EXEC sp_cursorclose @P1
    END

--//调用的方式

exec up_zbh_DivPageBySql 'select * from 表',10,3
存储过程
exec up_zbh_DivPageBySql 'exec 存储过程',10,1

好东东,拿出来给大家共享,哈哈!!
比以前的那些个存储过程分页方便,简单多了!!

posted on 2008-02-14 22:14  xumx  阅读(141)  评论(0)    收藏  举报

导航