我在项目里面如果使用存储过程分页,都是这样做的。
如果大家不喜欢用游标,那就将下面部分改为
不使用游标的方法
1
2
3
4
5
6
7CREATE PROCEDURE page
8 @PageIndex int , /*当前页数*/
9 @PageSize int /*每页大小*/
10 AS
11
12
13IF @PageIndex > 0
14 BEGIN
15 DECLARE @PageLowerBound int
16 DECLARE @StartID int
17 DECLARE @sql varchar(225)
18
19 SET @PageLowerBound = @PageSize * (@PageIndex-1)
20
21 SET ROWCOUNT @PageLowerBound
22
23 SELECT @StartID = [id] FROM table ORDER BY id desc
24
25 SET ROWCOUNT 0
26
27
28 SET @sql='select top '+str(@PageSize) +' COLUMNS from table where and [id]>'+ str(@StartID) +' ORDER BY id '
29
30 EXEC(@sql)
31 END
32
2
3
4
5
6
7CREATE PROCEDURE page
8 @PageIndex int , /*当前页数*/
9 @PageSize int /*每页大小*/
10 AS
11
12
13IF @PageIndex > 0
14 BEGIN
15 DECLARE @PageLowerBound int
16 DECLARE @StartID int
17 DECLARE @sql varchar(225)
18
19 SET @PageLowerBound = @PageSize * (@PageIndex-1)
20
21 SET ROWCOUNT @PageLowerBound
22
23 SELECT @StartID = [id] FROM table ORDER BY id desc
24
25 SET ROWCOUNT 0
26
27
28 SET @sql='select top '+str(@PageSize) +' COLUMNS from table where and [id]>'+ str(@StartID) +' ORDER BY id '
29
30 EXEC(@sql)
31 END
32
如果大家不喜欢用游标,那就将下面部分改为
1SET ROWCOUNT @PageLowerBound
2
3 SELECT @StartID = [id] FROM table ORDER BY id desc
4
5 SET ROWCOUNT 0
2
3 SELECT @StartID = [id] FROM table ORDER BY id desc
4
5 SET ROWCOUNT 0
不使用游标的方法
1
2 set sql='SELECT top ' +str(PageLowerBound)+ ' @startID=[id] FROM table ORDER BY id desc'
3exec(sql)
4
2 set sql='SELECT top ' +str(PageLowerBound)+ ' @startID=[id] FROM table ORDER BY id desc'
3exec(sql)
4