侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

2分法分页存储过程脚本实例

技术  /  管理员 发布于 7年前   187

需要说明的是:这个存储过程参数比较多,我再实际使用中又在外面单独写了一个类,页面调用直接调用封装的类,方法有很多,主要是思路,大家可以参考下。

代码修改集中在类似
复制代码 代码如下:
if @Sort=0
set @strTmp = @strTmp + '<(select min('
 else
set @strTmp = @strTmp + '>(select max('

另外94行主要是配合我自己写的类,显示记录条数分页数等信息,如果不需要就去掉。

复制代码 代码如下:
  1ALTER PROCEDURE [dbo].[proc_ListPage]
  2(
  3 @tblName     nvarchar(200),        ----要显示的表或多个表的连接
  4 @fldName     nvarchar(500) = '*',    ----要显示的字段列表
  5 @pageSize    int = 10,        ----每页显示的记录个数
  6 @page        int = 1,        ----要显示那一页的记录
  7 @fldSort    nvarchar(200) = null,    ----排序字段列表或条件
  8 @Sort        bit = 0,        ----排序方法,0为升序,1为降序(如果是多字段排列Sort指代最后一个排序字段的排列顺序(最后一个排序字段不加排序标记)--程序传参如:' SortA Asc,SortB Desc,SortC ')
  9 @strCondition    nvarchar(1000) = null,    ----查询条件,不需where
 10 @ID        nvarchar(150),        ----主表的主键
 11 @Dist      bit = 0,           ----是否添加查询字段的 DISTINCT 默认0不添加/1添加
 12 @pageCount    int = 1 output,            ----查询结果分页后的总页数
 13 @Counts    int = 1 output                ----查询到的记录数
 14 )
 15 AS
 16 SET NOCOUNT ON
 17 Declare @sqlTmp nvarchar(1000)        ----存放动态生成的SQL语句
 18 Declare @strTmp nvarchar(1000)        ----存放取得查询结果总数的查询语句
 19 Declare @strID     nvarchar(1000)        ----存放取得查询开头或结尾ID的查询语句
 20
 21 Declare @strSortType nvarchar(10)    ----数据排序规则A
 22 Declare @strFSortType nvarchar(10)    ----数据排序规则B
 23
 24 Declare @SqlSelect nvarchar(50)         ----对含有DISTINCT的查询进行SQL构造
 25 Declare @SqlCounts nvarchar(50)          ----对含有DISTINCT的总数查询进行SQL构造
 26
 27
 28 if @Dist  = 0
 29 begin
 30     set @SqlSelect = 'select '
 31     set @SqlCounts = 'Count(0)'
 32 end
 33 else
 34 begin
 35     set @SqlSelect = 'select distinct '
 36     set @SqlCounts = 'Count(DISTINCT '+@ID+')'
 37 end
 38
 39
 40 if @Sort=0
 41 begin
 42     set @strFSortType=' ASC '
 43     set @strSortType=' DESC '
 44 end
 45 else
 46 begin
 47     set @strFSortType=' DESC '
 48     set @strSortType=' ASC '
 49 end
 50
 51
 52
 53 --------生成查询语句--------
 54 --此处@strTmp为取得查询结果数量的语句
 55 if @strCondition is null or @strCondition=''     --没有设置显示条件
 56 begin
 57     set @sqlTmp =  @fldName + ' From ' + @tblName
 58     set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName
 59     set @strID = ' From ' + @tblName
 60 end
 61 else
 62 begin
 63     set @sqlTmp = + @fldName + 'From ' + @tblName + ' where (1>0) ' + @strCondition
 64     set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName + ' where (1>0) ' + @strCondition
 65     set @strID = ' From ' + @tblName + ' where (1>0) ' + @strCondition
 66 end
 67
 68 ----取得查询结果总数量-----
 69 exec sp_executesql @strTmp,N'@Counts int out ',@Counts out
 70 declare @tmpCounts int
 71 if @Counts = 0
 72     set @tmpCounts = 1
 73 else
 74     set @tmpCounts = @Counts
 75
 76     --取得分页总数
 77     set @pageCount=(@tmpCounts+@pageSize-1)/@pageSize
 78
 79     /**//**当前页大于总页数 取最后一页**/
 80     if @page>@pageCount
 81         set @page=@pageCount
 82
 83     --/*-----数据分页2分处理-------*/
 84     declare @pageIndex int --总数/页大小
 85     declare @lastcount int --总数%页大小
 86
 87     set @pageIndex = @tmpCounts/@pageSize
 88     set @lastcount = @tmpCounts%@pageSize
 89     if @lastcount > 0
 90         set @pageIndex = @pageIndex + 1
 91     else
 92         set @lastcount = @pagesize
 93
 94 --为配合显示
 95 set nocount off
 96 select @page curpage,@pageSize pagesize,@pageCount countpage,@tmpCounts [Rowcount]
 97 set nocount on
 98
 99  --//***显示分页
100     if @strCondition is null or @strCondition=''     --没有设置显示条件
101     begin
102         if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2   --前半部分数据处理
103             begin
104                 if @page=1
105                     set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(40))+' '+ @fldName+' from '+@tblName                       
106                         +' order by '+ @fldSort +' '+ @strFSortType
107                 else
108                 begin                   
109                     set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(40))+' '+ @fldName+' from '+@tblName
110                         +' where '+@ID
111                     if @Sort=0
112                        set @strTmp = @strTmp + '>(select max('
113                     else
114                        set @strTmp = @strTmp + '<(select min('
115                     set @strTmp = @strTmp + @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName
116                         +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)'
117                         +' order by '+ @fldSort +' '+ @strFSortType
118                 end   
119             end
120         else
121            
122             begin
123             set @page = @pageIndex-@page+1 --后半部分数据处理
124                 if @page <= 1 --最后一页数据显示           
125                     set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(40))+' '+ @fldName+' from '+@tblName
126                         +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
127                 else
128                     begin
129                     set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(40))+' '+ @fldName+' from '+@tblName
130                         +' where '+@ID
131                         if @Sort=0
132                            set @strTmp=@strTmp+' <(select min('
133                         else
134                            set @strTmp=@strTmp+' >(select max('
135  set @strTmp=@strTmp+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
136                         +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)'
137                         +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
138                    end
139             end
140
141     end
142
143     else --有查询条件
144     begin
145         if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2   --前半部分数据处理
146         begin
147                 if @page=1
148                     set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(40))+' '+ @fldName+' from '+@tblName                       
149                         +' where 1=1 ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType
150                 else
151                 begin                   
152                     set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(40))+' '+ @fldName+' from '+@tblName
153                         +' where '+@ID
154                     if @Sort=0
155                        set @strTmp = @strTmp + '>(select max('
156                     else
157                        set @strTmp = @strTmp + '<(select min('
158
159                  set @strTmp = @strTmp + @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName
160                         +' where (1=1) ' + @strCondition +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)'
161                         +' '+ @strCondition +' order by '+ @fldSort +' '+ @strFSortType
162                 end           
163         end
164         else
165         begin
166             set @page = @pageIndex-@page+1 --后半部分数据处理
167             if @page <= 1 --最后一页数据显示
168                     set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(40))+' '+ @fldName+' from '+@tblName
169                         +' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType                    
170             else
171                   begin
172                     set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(40))+' '+ @fldName+' from '+@tblName
173                         +' where '+@ID
174                     if @Sort=0
175                        set @strTmp = @strTmp + '<(select min('
176                     else
177                        set @strTmp = @strTmp + '>(select max('
178                set @strTmp = @strTmp + @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
179                         +' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)'
180                         +' '+ @strCondition+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType 
181                  end             
182         end   
183  
184     end
185
186 ------返回查询结果-----
187 SET NOCOUNT off
188 exec sp_executesql @strTmp
189 print @strTmp


  • 上一条:
    查询存储过程中特定字符的方法
    下一条:
    使用row_number()实现分页实例
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 2024.07.09日OpenAI将终止对中国等国家和地区API服务(0个评论)
    • 2024/6/9最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(1个评论)
    • 国外服务器实现api.openai.com反代nginx配置(0个评论)
    • 2024/4/28最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(1个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2017-09
    • 2018-01
    • 2018-07
    • 2018-08
    • 2018-09
    • 2018-12
    • 2019-01
    • 2019-02
    • 2019-03
    • 2019-04
    • 2019-05
    • 2019-06
    • 2019-07
    • 2019-08
    • 2019-09
    • 2019-10
    • 2019-11
    • 2019-12
    • 2020-01
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-12
    • 2024-02
    • 2024-04
    • 2024-05
    • 2024-06
    • 2025-02
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客