sql 分页

       今天在做基于ajaxpro分页的时候,发现如果按照前一篇文章所指示,和gridview交互很是不方便,于是就写了基于存储过程和DataTable的分页,主要是存储过程,我就把存储过程给出,其他的很简单,就不再列出
 1None.gif--分页存储过程
 2None.gifcreate procedure GetNews
 3None.gif(
 4None.gif  @type int,--文章的类型
 5None.gif  @pgs int,--页大小
 6None.gif  @pgn int--页码,页码为0为求总也数
 7None.gif)
 8None.gifas
 9None.gifdeclare @strsql nvarchar(500--执行的sql语句
10None.gifdeclare @zongshu int--纪录总数
11None.gifdeclare @yeshu int --总页数
12None.gifdeclare @strtemp int--临时变量
13None.gifif @pgn=0
14None.gif  begin
15None.gif     select @zongshu=count(*from LYNews where newstype=@type
16None.gif    if @zongshu<=@pgs
17None.gif        begin
18None.gif          if @zongshu=0
19None.gif
20None.gif                 set @yeshu=0
21None.gif            
22None.gif           else           
23None.gif             set  @yeshu=1             
24None.gif        end
25None.gif     else--如果@zongshu>@pgs那么开始计算分页,如果@zongshu%@pgs=0的话,就说明每页都是慢的,如果不是的话,那么将存在多余的占一页
26None.gif         begin
27None.gif          set @strtemp=@zongshu%@pgs  --如果@zongshu%@pgs=0的话,就说明每页都是满的,如果不是的话,那么将存在多余的纪录占一页
28None.gif           if @strtemp=0
29None.gif             
30None.gif                set @yeshu=@zongshu/@pgs
31None.gif            
32None.gif           else
33None.gif              begin
34None.gif                set @yeshu=@zongshu/@pgs+1--否则的话那么得在添加一页来显示多余的数据
35None.gif              end
36None.gif             set @strsql='select '+str(@zongshu)+' as zongshu, '+str(@yeshu)+' as yeshu'  --返回总页数和总纪录数
37None.gif          end   
38None.gifend 
39None.gifelse 
40None.gif   begin
41None.gif    if @pgn=1
42None.gif      
43None.gif         set @strsql='select top '+str(@pgs)+'  * from Lynews where newstype='+str(@type)+' order by postdate desc'--返回第一页的纪录
44None.gif       
45None.gif    else
46None.gif     
47None.gif      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'
48None.gif      
49None.gif   end
50None.gifexec (@strsql)
51None.gifgo

转载于:https://www.cnblogs.com/symjie520/archive/2007/05/25/760206.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值