欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-04 22:14:22
需要说明的是:这个存储过程参数比较多,我再实际使用中又在外面单独写了一个类,页面调用直接调用封装的类,方法有很多,主要是思路,大家可以参考下。 代码修改集中在类似复制代码...

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

代码修改集中在类似

复制代码 代码如下:

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