看到大家对数据库分页有研究,也分享个本人的

一个完整的sql语句分解开来,也就分文字段,表名或表连接,条件,排序,分组一共五部分

select a.id,a.name,b.id as bid,b.name as bname

from 表1 a inner join 表2 b

where a.id > 1

order by a.id asc

如上述sql语句分解开来由以下四部分组成

字段部分:a.id,a.name,b.id as bid,b.name as bname

表部分:表1 a inner join 表2 b

条件部分:a.id > 1

还有排序部分 a.id asc

 

本人sql分页存储过程

  1. CREATE    proc [dbo].[pageView]
  2. @pIndex int,--页码
  3. @pSize int,--每页行数
  4. @id varchar(30),--主键ID
  5. @Fields varchar(4000),---字段
  6. @tabName varchar(4000),--表名,和要连接的表以及关系
  7. @where varchar(4000)='',--条件
  8. @sort varchar(4000)=''--排序字段
  9. as
  10. create table #ls
  11. (row int identity(1,1),
  12. aid int
  13. )
  14. create table #ls1
  15. (
  16. row1 int identity(1,1),
  17. row int,
  18. aid int
  19. )
  20. declare @start int
  21. declare @end int
  22. declare @str varchar(8000)
  23. set @end=@pIndex * @pSize
  24. set @start=@end-@pSize+1
  25. set @str='select count('+@id +') as rowcounts from '+@tabName --+ ' where '+@where +' order by '+@sort
  26. if len(@where)> 0
  27.  set @str =@str + ' where '+@where
  28. exec (@str)
  29. set @str='insert into #ls(aid) select top '+str(@end)+' '+@id +' from '+@tabName --+ ' where '+@where +' order by '+@sort
  30. if len(@where)> 0
  31.  set @str =@str + ' where '+@where
  32. if len(@sort)>0
  33.  set @str=@str + ' order by ' +@sort
  34. exec (@str)
  35. insert into #ls1(row,aid)
  36. select #ls.row,#ls.aid from #ls where #ls.row between @start and @end
  37. drop table #ls
  38. --select * from #ls1
  39. set @str='select #ls1.row1,#ls1.row,'+@Fields+' from '+ @tabName + ' join #ls1 on #ls1.aid='+@id +' order by #ls1.row1'
  40. exec(@str)

 

 

 

可以很快的对数据进行分页,并且返回总记录数,每页序号,总序号以及数据来,

 

转载于:https://www.cnblogs.com/qitianshuye/archive/2012/10/25/2739223.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值