看到很多朋友费很多劲写的分页存储过程,也出来分享一下,其实可以很简单。
代码
1 create procedure [dbo].[sp_GetDataList_Pager]
2 (
3 @pagesize int, --每页大小
4 @pageindex int, --要提取的页码
5 @docount bit --是否返回记录数
6 )
7 /*
8 当@docount时,返回的是记录数,当false时返回当前页下的记录集
9 ----by JasperZhu http://www.cnblogs.com/jasperzhu 201010
10 */
11 as
12 begin
13 if(@docount=1)
14 begin
15 select count(*)
16 from
17 (
18 select *
19 from v_Article_Show
20 where 1=1
21 ) as myTempTable
22 end
23 else
24 begin
25 with temptbl as (
26 SELECT ROW_NUMBER() OVER (ORDER BY Art_CreateTime desc)AS Row,*
27 from
28 (
29 select Art_ID,Art_Name,Art_Source,Art_CreateTime
30 from v_Article_Show
31 where 1=1
32 ) as myTempTable1
33 )
34
35 select * FROM temptbl
36 where Row between (@pageindex-1)*@pagesize+1 and (@pageindex-1)*@pagesize+@pagesize
37 end
38 end
2 (
3 @pagesize int, --每页大小
4 @pageindex int, --要提取的页码
5 @docount bit --是否返回记录数
6 )
7 /*
8 当@docount时,返回的是记录数,当false时返回当前页下的记录集
9 ----by JasperZhu http://www.cnblogs.com/jasperzhu 201010
10 */
11 as
12 begin
13 if(@docount=1)
14 begin
15 select count(*)
16 from
17 (
18 select *
19 from v_Article_Show
20 where 1=1
21 ) as myTempTable
22 end
23 else
24 begin
25 with temptbl as (
26 SELECT ROW_NUMBER() OVER (ORDER BY Art_CreateTime desc)AS Row,*
27 from
28 (
29 select Art_ID,Art_Name,Art_Source,Art_CreateTime
30 from v_Article_Show
31 where 1=1
32 ) as myTempTable1
33 )
34
35 select * FROM temptbl
36 where Row between (@pageindex-1)*@pagesize+1 and (@pageindex-1)*@pagesize+@pagesize
37 end
38 end