zoukankan      html  css  js  c++  java
  • 目前来看较完美的通用二分法分页存储过程,not in模式,适用于非数值ID,可多字段排序,可以distinct

    CREATE PROCEDURE proc_pagination
    (
    @tblName nvarchar(1000),        ----要显示的表或多个表的连接,必须参数
    @fldName  nvarchar(4000) = '*',    ----要显示的字段列表
    @fldSort  nvarchar(4000) = null,    ----排序字段列表或条件
    @fldFSort nvarchar(4000) = null,    ----反向排序字段列表或条件(这个需要在调用前准备好,且一定要与原始排序完全相反)
    @strCondition  nvarchar(4000) = null,    ----查询条件,不需where
    @ID nvarchar(1000),        ----主表的主键,必须参数
    @Dist bit = 0,           ----是否添加查询字段的 DISTINCT 默认0不添加/1添加
    @pageSize int = 10,        ----每页显示的记录个数
    @currentpage int = 1,        ----要显示那一页的记录
    @pageCount int = 1 output,            ----查询结果分页后的总页数
    @Counts int = 1 output                ----查询到的记录数
    )
    AS
    SET NOCOUNT ON
    Declare @sqlTmp nvarchar(4000)        ----存放动态生成的SQL语句
    Declare @strTmp nvarchar(4000)        ----存放取得查询结果总数的查询语句
    Declare @strID nvarchar(1000)        ----存放取得查询开头或结尾ID的查询语句
    
    Declare @strSort nvarchar(4000)    ----数据排序规则A
    Declare @strFSort nvarchar(4000)    ----数据排序规则B
    
    Declare @SqlSelect nvarchar(50)         ----对含有DISTINCT的查询进行SQL构造
    Declare @SqlCounts nvarchar(50)          ----对含有DISTINCT的总数查询进行SQL构造
    
    Declare @IDSords nvarchar(4000)        ----ID和所有参加排序的字段,用于distinct和order by
    
    if @Dist  = 0    --不带distinct
    begin
        set @SqlSelect = 'select '
        set @SqlCounts = 'Count(1)'
    end
    else
    begin
        set @SqlSelect = 'select distinct '
        set @SqlCounts = 'Count(DISTINCT '+@ID+')'
    end
    
    --本来应该在这里分析@fldSort,然后自动生成反向排序字段
    set @strSort=@fldSort
    set @strFSort=@fldFSort
    
    --------生成查询语句--------
    --此处@strTmp为取得查询结果数量的语句
    if @strCondition is null or @strCondition=''     --没有查询条件,不带where
    begin
        set @sqlTmp =  @fldName + ' From ' + @tblName
        set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName
        set @strID = ' From ' + @tblName
    end
    else
    begin
        set @sqlTmp = + @fldName + ' From ' + @tblName + ' where ' + @strCondition
        set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName + ' where ' + @strCondition
        set @strID = ' From ' + @tblName + ' where ' + @strCondition
    end
    
    
    ----取得查询结果总数量-----
    exec sp_executesql @strTmp,N'@Counts int out ',@Counts out
    
    declare @tmpCounts int
    if @Counts = 0
        set @tmpCounts = 1
    else
        set @tmpCounts = @Counts
    
        --取得分页总数
        set @pageCount=(@tmpCounts+@pageSize-1)/@pageSize
    
        /**//**当前页大于总页数 取最后一页**/
        if @currentpage>@pageCount
            set @currentpage=@pageCount
    
        --/*-----数据分页2分处理-------*/
        declare @pageIndex int --总数/页大小
        declare @lastcount int --总数%页大小 
    
        set @pageIndex = @tmpCounts/@pageSize
        set @lastcount = @tmpCounts%@pageSize
        if @lastcount > 0
            set @pageIndex = @pageIndex + 1    ----@lastcount>0说明最后一页还有记录,所以就应该再多一页
        else
            set @lastcount = @pageSize
    
        --//***显示分页
        if @strCondition is null or @strCondition=''     --没有查询条件where
            begin
                if @pageIndex<2 or @currentpage<=@pageIndex / 2 + @pageIndex % 2   --前半部分数据处理
                    begin 
                        if @fldSort is null or @fldSort=''    --没有排序
                            set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
                                    +' where '+@ID+' not in(select '+@ID+' from '
                                    +'('+ @SqlSelect+' top '+ CAST(@pageSize*(@currentpage-1) as Varchar(20))
                                    +' '+ @fldName +' from '+@tblName +') as TempTBL)'
                            --取的是id不在前(@currentpage-1)页中,即不是前(@currentpage-1)*@pagesize条记录的前@pagesize条记录
                        else    --如果有排序的话
                            set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
                                    +' where '+@ID+' not in(select '+@ID+' from ('
                                    + @SqlSelect+' top '+ CAST(@pageSize*(@currentpage-1) as Varchar(20)) +' '+ @fldName
                                    +' from '+@tblName
                                    +' order by '+ @strSort+') as TempTBL)'
                                    +' order by '+ @strSort 
                            --按正序排序取前(@currentpage-1)*@pagesize条记录,然后再按正序排序把前(@currentpage-1)*@pagesize条记录排除取前@pagesize条记录
                    end
                else                                 --后半部分数据处理
                    begin
                    set @currentpage = @pageIndex-@currentpage+1
                        if @currentpage <= 1 --最后一页数据显示
                            if @fldSort is null or @fldSort=''    --没有排序
                                set @strTmp=@SqlSelect+' '+@fldName+' from ('
                                    + @SqlSelect +' ' + @fldName+ ' from '+@tblName
                                    +' where '+@ID + ' not in (select '+@ID+' from('
                                    +@SqlSelect+' top '+CAST(@pageSize*(@pageIndex-1) as VARCHAR(4))+' '+@fldName
                                    +' from '+@tblName
                                    +') as TempTBL)) AS TempTB'
                                    --如果是最后一页就没必要再用top了,直接把不在前@pagesize*(@pageindex-1)页中的记录取出就可以
                            else    --如果有排序的话
                                set @strTmp=@SqlSelect+' '+@fldName+' from ('
                                    +@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName
                                    +' from '+@tblName
                                    +' order by '+ @strFSort +') AS TempTB'+' order by '+ @strSort
                                    --先反向排序,取出前@lastcount条记录,再进行正向排序
                        else    --不是最后一页,即后半部分的其他页
                            if @fldSort is null or @fldSort=''    --没有排序时
                                set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName
                                        +' where '+@ID+' not in(select '+@ID+' from('
                                        + @SqlSelect+' top '+ CAST(@pageSize*(@currentpage-1) as Varchar(20))
                                        +' '+ @fldName +' from '+@tblName +') as TempTBL)'
                                        --不排序时跟前半部分应该一样处理
                            /*
                                set @strTmp=@SqlSelect+' '+@fldName+' from ('
                                    +@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName
                                    +' from '+@tblName +' where '+@ID+' not in('
                                    + @SqlSelect+' top '+ CAST(@pageSize*(@currentpage-2)+@lastcount as Varchar(20)) +' '+ @fldName
                                     +' from '+@tblName +')'
                                    +') AS TempTB' 
                                    */
                            else        --有排序时   
                                set @strTmp=@SqlSelect+' '+@fldName+' from ('
                                    +@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName
                                    +' from '+@tblName +' where '+@ID+' not in(select '+@ID+' from('
                                    + @SqlSelect+' top '+ CAST(@pageSize*(@currentpage-2)+@lastcount as Varchar(20)) +' '+ @fldName
                                    +' from '+@tblName +' order by '+ @strFSort +') as TempTBL)'
                                    +' order by '+ @strFSort +') AS TempTB'
                                    +' order by '+ @strSort
                                    --先反向排序取出前(@currentpage-1)页的记录(其中包括不满一整页的最后一页,所以是前@pagesize*(@currentpage-2)+@lastcount条记录)
                                    --然后仍然反向排序,这回取不在上一结果集里的前@pagesize条记录,即该得到的记录
                                    --最后进行一次正向排序
                    end
            end
        else --有查询条件
            begin
                if @pageIndex<2 or @currentpage<=@pageIndex / 2 + @pageIndex % 2   --前半部分数据处理
                    begin
                        if @fldSort is null or @fldSort=''    --没有排序
                            set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName +' from  '+@tblName
                                +' where '+@ID+' not in(select '+@ID+' from('
                                + @SqlSelect+' top '+ CAST(@pageSize*(@currentpage-1) as Varchar(20)) +' '+ @fldName
                                +' from '+@tblName
                                +' Where ' + @strCondition + ') as TempTBL)'
                                +' and ' + @strCondition
                                --先取出带where后的前@pagesize*(@currentpage-1)条记录
                                --再取出不在这@pagesize*(@currentpage-1)条记录中的还where的前@pagesize条记录
                        else        --有排序
                            set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName +' from  '+@tblName
                                +' where '+@ID+' not in(select '+@ID+' from('
                                + @SqlSelect+' top '+ CAST(@pageSize*(@currentpage-1) as Varchar(20)) +' '+ @fldName
                                +' from '+@tblName +' Where ' + @strCondition + ' order by '+ @strSort +') as TempTBL)'
                                +' and ' + @strCondition + ' order by '+ @strSort
                                --1、正向排序,取出带where后的前@pagesize*(@currentpage-1)条记录
                                --2、正向排序,取出不在这@pagesize*(@currentpage-1)条记录中的还where的前@pagesize条记录
                    end
                else    --后半部分
                    begin 
                        set @currentpage = @pageIndex-@currentpage+1 --后半部分数据处理
                        if @currentpage <= 1 --最后一页数据显示
                            if @fldSort is null or @fldSort=''
                                set @strTmp=@SqlSelect+' '+@fldName + ' from '+@tblName
                                    +' where '+@ID + ' not in (select '+@ID+' from('
                                    +@SqlSelect+' top '+CAST(@pagesize*(@pageIndex-1) as VARCHAR(4))+' '+@fldName
                                    +' from '+@tblName +' where '+@strCondition
                                    +') as TempTBL) and '+@strCondition
                                --取不在前@pageIndex-1页中的记录
                            else    --有排序,即既有条件又有排序
                                set @strTmp=@SqlSelect+' '+@fldName+' from ('
                                    +@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName
                                    +' from '+@tblName+' where '+ @strCondition
                                    +' order by '+ @strFSort +') AS TempTB'+' order by '+ @strSort
                                --先倒序排列,取前@lastcount条记录
                                --再正序排序
                        else    --后半部分,但不是最后一页
                            if @fldSort is null or @fldSort=''    --没有排序条件
                                set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName +' from  '+@tblName
                                    +' where '+@ID+' not in(select '+@ID+' from('
                                    + @SqlSelect+' top '+ CAST(@pageSize*(@currentpage-1) as Varchar(20)) +' '+ @fldName
                                    +' from '+@tblName
                                    +' Where ' + @strCondition + ') as TempTBL)'
                                    +' and ' + @strCondition
                                    --不排序时应该与前半部分相同处理
                            else    --有排序字段、有查询条件、后半部分非最末页
                                set @strTmp=@SqlSelect+' '+@fldName+' from ('
                                    +@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName
                                    +' from '+@tblName +' where '+@ID+' not in ('
                                    +'select '+@ID+' from ('
                                    + @SqlSelect+' top '+ CAST(@pageSize*(@currentpage-2)+@lastcount as Varchar(20)) +' '+ @fldName
                                    +' from '+@tblName +' where '+ @strCondition +' order by '+ @strFSort +') as TempTBL)'
                                    +' and ' + @strCondition +' order by '+ @fldFSort +') AS TempTB'+' order by '+ @strSort
                    end    
            end
    
    ------返回查询结果-----
    exec sp_executesql @strTmp
    --print @strTmp
    SET NOCOUNT OFF
  • 相关阅读:
    详细聊聊k8s deployment的滚动更新(一)
    更新k8s镜像版本的三种方式
    深入理解docker信号机制以及dumb-init的使用
    10分钟教你理解反射
    nodejs的交互式解释器模式常用命令
    nrm的安装和使用
    复杂sql语句之单字段分类count计数和多字段count计数
    navicat连接mysql出现2059
    mongodb常规操作语句
    System.Web.NullPointerException
  • 原文地址:https://www.cnblogs.com/jeffrey77/p/2610148.html
Copyright © 2011-2022 走看看