zoukankan      html  css  js  c++  java
  • 2分分页处理存储过程通用存储过程

      1--/*----- 对数据进行了2分处理使查询前半部分数据与查询后半部分数据性能相同 -------*/
      2  --/*-----存储过程 分页处理  2005-04-21修改 添加Distinct查询功能-------*/
      3  --/*-----存储过程 分页处理  2005-05-18修改 多字段排序规则问题-------*/
      4  --/*-----存储过程 分页处理  2005-06-15修改 多字段排序修改-------*/
      5  ALTER PROCEDURE dbo.proc_ListPage
      6  (
      7  @tblName     nvarchar(200),                ----要显示的表或多个表的连接
      8  @fldName     nvarchar(500= '*',        ----要显示的字段列表
      9  @pageSize    int = 1,                    ----每页显示的记录个数
     10  @page        int = 10,                    ----要显示那一页的记录
     11 @pageCount    int = 1 output,           ----查询结果分页后的总页数
     12 @Counts    int = 1 output,              ----查询到的记录数
     13 @fldSort    nvarchar(200= null,        ----排序字段列表或条件
     14 @Sort        bit = 0,                    ----排序方法,0为升序,1为降序(如果是多字段排列Sort指代最后一个排序字段的排列顺序(最后一个排序字段不加排序标记)--程序传参如:' SortA Asc,SortB Desc,SortC ')
     15 @strCondition    nvarchar(1000= null,    ----查询条件,不需where
     16 @ID        nvarchar(150),                ----主表的主键
     17 @Dist                 bit = 0           ----是否添加查询字段的 DISTINCT 默认0不添加/1添加
     18 )
     19 AS
     20 SET NOCOUNT ON
     21 Declare @sqlTmp nvarchar(1000)            ----存放动态生成的SQL语句
     22 Declare @strTmp nvarchar(1000)            ----存放取得查询结果总数的查询语句
     23 Declare @strID     nvarchar(1000)       ----存放取得查询开头或结尾ID的查询语句
     24 
     25 Declare @strSortType nvarchar(10)        ----数据排序规则A
     26 Declare @strFSortType nvarchar(10)        ----数据排序规则B
     27 
     28 Declare @SqlSelect nvarchar(50)         ----对含有DISTINCT的查询进行SQL构造
     29 Declare @SqlCounts nvarchar(50)         ----对含有DISTINCT的总数查询进行SQL构造
     30 
     31 
     32 if @Dist  = 0
     33 begin
     34     set @SqlSelect = 'select '
     35     set @SqlCounts = 'Count(*)'
     36 end
     37 else
     38 begin
     39     set @SqlSelect = 'select distinct '
     40     set @SqlCounts = 'Count(DISTINCT '+@ID+')'
     41 end
     42 
     43 
     44 if @Sort=0
     45 begin
     46     set @strFSortType=' ASC '
     47     set @strSortType=' DESC '
     48 end
     49 else
     50 begin
     51     set @strFSortType=' DESC '
     52     set @strSortType=' ASC '
     53 end
     54 
     55 
     56 
     57 --------生成查询语句--------
     58 --此处@strTmp为取得查询结果数量的语句
     59 if @strCondition is null or @strCondition=''     --没有设置显示条件
     60 begin
     61     set @sqlTmp =  @fldName + ' From ' + @tblName
     62     set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName
     63     set @strID = ' From ' + @tblName
     64 end
     65 else
     66 begin
     67     set @sqlTmp = + @fldName + 'From ' + @tblName + ' where (1>0) ' + @strCondition
     68     set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName + ' where (1>0) ' + @strCondition
     69     set @strID = ' From ' + @tblName + ' where (1>0) ' + @strCondition
     70 end
     71 
     72 ----取得查询结果总数量-----
     73 exec sp_executesql @strTmp,N'@Counts int out ',@Counts out
     74 declare @tmpCounts int
     75 if @Counts = 0
     76     set @tmpCounts = 1
     77 else
     78     set @tmpCounts = @Counts
     79 
     80     --取得分页总数
     81     set @pageCount=(@tmpCounts+@pageSize-1)/@pageSize
     82 
     83     /**//**//**//**当前页大于总页数 取最后一页**/
     84     if @page>@pageCount
     85         set @page=@pageCount
     86 
     87     --/*-----数据分页2分处理-------*/
     88     declare @pageIndex int --总数/页大小
     89     declare @lastcount int --总数%页大小 
     90 
     91     set @pageIndex = @tmpCounts/@pageSize
     92     set @lastcount = @tmpCounts%@pageSize
     93     if @lastcount > 0
     94         set @pageIndex = @pageIndex + 1
     95     else
     96         set @lastcount = @pagesize
     97 
     98     --//***显示分页
     99     if @strCondition is null or @strCondition=''     --没有设置显示条件
    100    begin
    101        if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2   --前半部分数据处理
    102            begin 
    103                set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
    104                        +' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1as Varchar(20)) +' '+ @ID +' from '+@tblName
    105                        +' order by '+ @fldSort +' '+ @strFSortType+')'
    106                        +' order by '+ @fldSort +' '+ @strFSortType 
    107            end
    108        else
    109            begin
    110            set @page = @pageIndex-@page+1 --后半部分数据处理
    111                if @page <= 1 --最后一页数据显示
    112                    set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName
    113                        +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType 
    114                else                
    115                    set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
    116                        +' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
    117                        +' order by '+ @fldSort +' '+ @strSortType+')'
    118
    119                        +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType 
    120            end
    121    end
    122
    123    else --有查询条件
    124    begin
    125        if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2   --前半部分数据处理
    126        begin 
    127                set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName +' from  '+@tblName
    128                    +' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1as Varchar(20)) +' '+ @ID +' from '+@tblName
    129                    +' Where (1>0) ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType+')'
    130                    +' ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType                 
    131        end
    132        else
    133        begin 
    134            set @page = @pageIndex-@page+1 --后半部分数据处理
    135            if @page <= 1 --最后一页数据显示
    136                    set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName
    137                        +' where (1>0) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType
    138            else
    139                    set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
    140                        +' where '+@ID+' not in('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName
    141                        +' where (1>0) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+')'
    142                        + @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType 
    143        end    
    144    end
    145
    146------返回查询结果-----
    147exec sp_executesql @strTmp
    148--print @strTmp
    149SET NOCOUNT OFF
  • 相关阅读:
    类和对象
    函数复习
    20145214 《网络对抗技术》 Web安全基础实践
    20145214 《网络对抗技术》 Web基础
    20145214 《网络对抗技术》 网络欺诈技术防范
    20145214 《网络对抗技术》 信息搜集与漏洞扫描
    20145214 《网络对抗技术》 MSF基础应用
    20145214 《网络对抗技术》 恶意代码分析
    20145214《网络对抗技术》免杀原理与实践
    20145214《网络对抗》MAL_后门原理与实践
  • 原文地址:https://www.cnblogs.com/vaiyanzi/p/439919.html
Copyright © 2011-2022 走看看