方案一:
1 create procedure [StorePaged]
2 @sql varchar(8000), --要执行的sql语句
3 @page int=1, --要显示的页码
4 @pageSize int, --每页的大小
5 @pageCount int=0 out, --总页数
6 @recordCount int=0 out --总记录数
7 as
8 set nocount off
9 if &#64;page < 1 set &#64;Page &#61; 1
10 if &#64;pageSize < 1 set &#64;pageSize &#61; 5
11 declare &#64;p1 int,&#64;CurrentPageOut int
12 set &#64;CurrentPageOut &#61; &#64;Page
13 exec sp_cursoropen &#64;p1 output,&#64;sql,&#64;scrollopt &#61; 1,&#64;ccopt &#61; 1,&#64;rowcount &#61; &#64;pagecount output
14 set &#64;recordCount &#61; &#64;pageCount
15 select &#64;pagecount&#61;ceiling(1.0 * &#64;pagecount/&#64;pagesize),&#64;page &#61; (&#64;page - 1) * &#64;pagesize &#43; 1
16 exec sp_cursorfetch &#64;p1,16,&#64;page,&#64;pagesize
17 exec sp_cursorclose &#64;p1
18 select &#64;pagecount as PageCount,&#64;recordCount as RecordCount,&#64;CurrentPageOut as CurrentPage,&#64;pageSize as PageSize