1
--分页存储过程
2
create procedure GetNews
3
(
4
@type int,--文章的类型
5
@pgs int,--页大小
6
@pgn int--页码,页码为0为求总也数
7
)
8
as
9
declare @strsql nvarchar(500) --执行的sql语句
10
declare @zongshu int--纪录总数
11
declare @yeshu int --总页数
12
declare @strtemp int--临时变量
13
if @pgn=0
14
begin
15
select @zongshu=count(*) from LYNews where newstype=@type
16
if @zongshu<=@pgs
17
begin
18
if @zongshu=0
19
20
set @yeshu=0
21
22
else
23
set @yeshu=1
24
end
25
else--如果@zongshu>@pgs那么开始计算分页,如果@zongshu%@pgs=0的话,就说明每页都是慢的,如果不是的话,那么将存在多余的占一页
26
begin
27
set @strtemp=@zongshu%@pgs --如果@zongshu%@pgs=0的话,就说明每页都是满的,如果不是的话,那么将存在多余的纪录占一页
28
if @strtemp=0
29
30
set @yeshu=@zongshu/@pgs
31
32
else
33
begin
34
set @yeshu=@zongshu/@pgs+1--否则的话那么得在添加一页来显示多余的数据
35
end
36
set @strsql='select '+str(@zongshu)+' as zongshu, '+str(@yeshu)+' as yeshu' --返回总页数和总纪录数
37
end
38
end
39
else
40
begin
41
if @pgn=1
42
43
set @strsql='select top '+str(@pgs)+' * from Lynews where newstype='+str(@type)+' order by postdate desc'--返回第一页的纪录
44
45
else
46
47
set @strsql='select top '+str(@pgs)+' * from LyNews Where newstype='+str(@type)+' and postdate<(select min(postdate) from (select top '+str((@pgn-1)*@pgs)+' * from LyNews where newstype='+str(@type)+' order by postdate desc ) as t) order by postdate desc'
48
49
end
50
exec (@strsql)
51
go
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51