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
create procedure GetNews3
(4
@type int,--文章的类型5
@pgs int,--页大小6
@pgn int--页码,页码为0为求总也数7
)8
as9
declare @strsql nvarchar(500) --执行的sql语句10
declare @zongshu int--纪录总数11
declare @yeshu int --总页数12
declare @strtemp int--临时变量13
if @pgn=014
begin15
select @zongshu=count(*) from LYNews where newstype=@type16
if @zongshu<=@pgs17
begin18
if @zongshu=019

20
set @yeshu=021
22
else 23
set @yeshu=1 24
end25
else--如果@zongshu>@pgs那么开始计算分页,如果@zongshu%@pgs=0的话,就说明每页都是慢的,如果不是的话,那么将存在多余的占一页26
begin27
set @strtemp=@zongshu%@pgs --如果@zongshu%@pgs=0的话,就说明每页都是满的,如果不是的话,那么将存在多余的纪录占一页28
if @strtemp=029
30
set @yeshu=@zongshu/@pgs31
32
else33
begin34
set @yeshu=@zongshu/@pgs+1--否则的话那么得在添加一页来显示多余的数据35
end36
set @strsql='select '+str(@zongshu)+' as zongshu, '+str(@yeshu)+' as yeshu' --返回总页数和总纪录数37
end 38
end 39
else 40
begin41
if @pgn=142
43
set @strsql='select top '+str(@pgs)+' * from Lynews where newstype='+str(@type)+' order by postdate desc'--返回第一页的纪录44
45
else46
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
end50
exec (@strsql)51
go
1270

被折叠的 条评论
为什么被折叠?



