SqlServer 分页存储过程

时间:2021-01-15 14:04:44
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_CommonDataResourcePaged]
(
    @TableName          varchar(500),           ----要显示的表或多个表的连接
    @FieldList          varchar(5000) = '*',    ----要显示的字段列表
    @PageSize           int = 20,                ----每页显示的记录个数
    @PageNumber         int = 1,                 ----要显示那一页的记录
    @SortFields         varchar(1000) = null,   ----排序字段列表或条件
    @EnabledSort        bit = 0,                 ----排序方法,0为升序,1为降序(如果是多字段排列Sort指代最后一个排序字段的排列顺序(最后一个排序字段不加排序标记)--程序传参如:' SortA Asc,SortB Desc,SortC ')
    @QueryCondition     nvarchar(1500) = null,   ----查询条件,不需WHERE
    @Primarykey         varchar(50),            ----主表的主键
    @EnabledDistinct    bit = 0,                 ----是否添加查询字段的 DISTINCT 默认0不添加/1添加
    @PageCount          int = 1 output,          ----查询结果分页后的总页数
    @RecordCount        int = 1 output           ----查询到的记录数
)
AS
    SET NOCOUNT ON
    Declare @PrimaryKeyAlias varchar(50)
    Declare @SqlResult nvarchar(4000)        ----存放动态生成的SQL语句
    Declare @SqlTotalCount nvarchar(4000)        ----存放取得查询结果总数的查询语句
    Declare @SqlStartOrEndID  nvarchar(3000)        ----存放取得查询开头或结尾ID的查询语句
    
    Declare @SortTypeA nvarchar(10)    ----数据排序规则A
    Declare @SortTypeB nvarchar(10)    ----数据排序规则B
    
    Declare @SqlDistinct nvarchar(50)         ----对含有DISTINCT的查询进行SQL构造
    Declare @SqlCountDistinct nvarchar(50)          ----对含有DISTINCT的总数查询进行SQL构造
    
    declare @ExecuteTime datetime  --耗时测试时间差

    --如果字段名带有表名或者其他的前缀(literature.tbl_Literatures.ID),通过(select * from table)显示后,在排序,此时的字段名是不包含前缀的,
    --所以要处理一下,该方法适合Sql Server,如果要迁移其他数据库上,可以修改获取字段名的方法
    DECLARE @ConvertedSortFields varchar(50)
    IF CHARINDEX('.',@SortFields)>0
      BEGIN
        SELECT @ConvertedSortFields=REVERSE(SUBSTRING(REVERSE(@SortFields),1,CHARINDEX('.',REVERSE(@SortFields))-1))
      END
    ELSE
      BEGIN
        SET @ConvertedSortFields=@SortFields
      END
    SELECT @ExecuteTime=GETDATE()

    --在分页的过程中,会涉及到子查询,当子查询根据主键查询后,外层循环再使用该值的时候已经不是第一次赋的值了,此处要做转换,否则会有错误
    SET @PrimaryKeyAlias='CustomID'
    
    if @EnabledDistinct  = 0
        begin
            set @SqlDistinct = 'SELECT '
            set @SqlCountDistinct = 'Count(*)'
        end
    else
        begin
            set @SqlDistinct = 'SELECT DISTINCT '
            set @SqlCountDistinct = 'Count(DISTINCT '+@Primarykey+')'
        end
    
    if @EnabledSort=0
        begin
            set @SortTypeB=' ASC '
            set @SortTypeA=' DESC '
        end
    else
        begin
            set @SortTypeB=' DESC '
            set @SortTypeA=' ASC '
        end
    
    --------生成查询语句--------
    --此处@SqlTotalCount为取得查询结果数量的语句
    if @QueryCondition is null or @QueryCondition='' or @QueryCondition='''' or LEN(@QueryCondition)<3   --没有设置显示条件
        begin
            set @SqlResult =  @FieldList + ' From ' + @TableName
            set @SqlTotalCount = @SqlDistinct+' @RecordCount='+@SqlCountDistinct+' FROM '+@TableName
            set @SqlStartOrEndID = ' From ' + @TableName
        end
    else
        begin
            set @SqlResult = + @FieldList + ' From ' + @TableName + ' WHERE (1>0) and ' + @QueryCondition
            set @SqlTotalCount = @SqlDistinct+' @RecordCount='+@SqlCountDistinct+' FROM '+@TableName + ' WHERE (1>0) and ' + @QueryCondition
            set @SqlStartOrEndID = ' From ' + @TableName + ' WHERE (1>0) and ' + @QueryCondition
        end
    
    ----取得查询结果总数量-----
    exec sp_executesql @SqlTotalCount,N'@RecordCount int out ',@RecordCount out

    declare @TemporaryRecordCount int --临时统计
    if @RecordCount = 0
        set @TemporaryRecordCount = 1
    else
        set @TemporaryRecordCount = @RecordCount
    
        --取得分页总数
        set @PageCount=(@TemporaryRecordCount+@PageSize-1)/@PageSize
    
        /**当前页大于总页数 取最后一页**/
        if @PageNumber>@PageCount
            set @PageNumber=@PageCount
    
        --/*-----数据分页2分处理-------*/
        declare @TemporaryPageCount int --总数/页大小,临时存储总的分页数据
        declare @ExtraPageNumber int --总数%页大小 此处用于判断是否有余数,有就增加一页,没有就保持原有分页总数
    
        set @TemporaryPageCount = @TemporaryRecordCount/@PageSize
        set @ExtraPageNumber = @TemporaryRecordCount%@PageSize

        --如果对总记录条数和页大小取余,结果大于零,分页数量需要加一
        if @ExtraPageNumber > 0
          BEGIN
            set @TemporaryPageCount = @TemporaryPageCount + 1
          END
        ELSE
          BEGIN
            SET @ExtraPageNumber=@PageSize
          END
    
        --//***显示分页
        if @QueryCondition is null or @QueryCondition='' or @QueryCondition='''' or LEN(@QueryCondition)<3     --没有设置显示条件
            begin
                if @TemporaryPageCount<2 or @PageNumber<=@TemporaryPageCount / 2 + @TemporaryPageCount % 2   --前半部分数据处理
                    begin 
                        if @PageNumber=1
                            set @SqlTotalCount=@SqlDistinct+' TOP '+ CAST(@PageSize as VARCHAR(4))+' '+ @FieldList+' FROM '+@TableName+' ORDER BY '+ @SortFields +' '+ @SortTypeB
                        else
                        begin
                            if @EnabledSort=1
                                begin                    
                                    set @SqlTotalCount=@SqlDistinct+' TOP '+ CAST(@PageSize as VARCHAR(4))+' '+ @FieldList+' FROM '+@TableName    +' WHERE '+@Primarykey+' <(SELECT MIN('+ @PrimaryKeyAlias +') FROM ('+ @SqlDistinct+' TOP '+ CAST(@PageSize*(@PageNumber-1) as Varchar(20)) +' '+ @Primarykey +' AS '+@PrimaryKeyAlias+' FROM '+@TableName    +' ORDER BY '+ @SortFields +' '+ @SortTypeB+') AS TBMinID)'+' ORDER BY '+ @SortFields +' '+ @SortTypeB
                                end
                            else
                                begin
                                    set @SqlTotalCount=@SqlDistinct+' TOP '+ CAST(@PageSize as VARCHAR(4))+' '+ @FieldList+' FROM '+@TableName +' WHERE '+@Primarykey+' >(SELECT MAX('+ @PrimaryKeyAlias +') FROM ('+ @SqlDistinct+' TOP '+ CAST(@PageSize*(@PageNumber-1) as Varchar(20)) +' '+ @Primarykey +' AS '+@PrimaryKeyAlias+' FROM '+@TableName    +' ORDER BY '+ @SortFields +' '+ @SortTypeB+') AS TBMinID)'+' ORDER BY '+ @SortFields +' '+ @SortTypeB
                                end
                        end    
                    end
                else
                    begin
                    set @PageNumber = @TemporaryPageCount-@PageNumber+1 --后半部分数据处理

                        if @PageNumber <= 1 --最后一页数据显示
                           begin
                            set @SqlTotalCount=@SqlDistinct+' * FROM ('+@SqlDistinct+' TOP '+ CAST(@ExtraPageNumber as VARCHAR(4))+' '+ @FieldList+' FROM '+@TableName+' ORDER BY '+ @SortFields +' '+ @SortTypeA+') AS TempTB'+' ORDER BY '+ @ConvertedSortFields +' '+ @SortTypeB
                            end
                        else
                            if @EnabledSort=1
                                begin
                                    set @SqlTotalCount=@SqlDistinct+' * FROM ('+@SqlDistinct+' TOP '+ CAST(@PageSize as VARCHAR(4))+' '+ @FieldList+' FROM '+@TableName    +' WHERE '+@Primarykey+' >(SELECT MAX('+ @PrimaryKeyAlias +') FROM('+ @SqlDistinct+' TOP '+ CAST(@PageSize*(@PageNumber-2)+@ExtraPageNumber as Varchar(20)) +' '+ @Primarykey +' AS '+@PrimaryKeyAlias+' FROM '+@TableName+' ORDER BY '+ @SortFields +' '+ @SortTypeA+') AS TBMaxID)'+' ORDER BY '+ @SortFields +' '+ @SortTypeA+') AS TempTB'+' ORDER BY '+ @ConvertedSortFields +' '+ @SortTypeB
                                end
                            else
                                begin
                                    set @SqlTotalCount=@SqlDistinct+' * FROM ('+@SqlDistinct+' TOP '+ CAST(@PageSize as VARCHAR(4))+' '+ @FieldList+' FROM '+@TableName
                                        +' WHERE '+@Primarykey+' <(SELECT MIN('+ @PrimaryKeyAlias +') FROM('+ @SqlDistinct+' TOP '+ CAST(@PageSize*(@PageNumber-2)+@ExtraPageNumber as Varchar(20)) +' '+ @Primarykey +' AS '+@PrimaryKeyAlias+' FROM '+@TableName    +' ORDER BY '+ @SortFields +' '+ @SortTypeA+') AS TBMaxID)'+' ORDER BY '+ @SortFields +' '+ @SortTypeA+') AS TempTB'+' ORDER BY '+ @ConvertedSortFields +' '+ @SortTypeB
                                end
                    end
            end
        else --有查询条件
            begin
                if @TemporaryPageCount<2 or @PageNumber<=@TemporaryPageCount / 2 + @TemporaryPageCount % 2   --前半部分数据处理
                    begin
                        if @PageNumber=1
                            set @SqlTotalCount=@SqlDistinct+' TOP '+ CAST(@PageSize as VARCHAR(4))+' '+ @FieldList+' FROM '+@TableName +' WHERE 1=1 and ' + @QueryCondition + ' ORDER BY '+ @SortFields +' '+ @SortTypeB                        
                        else if(@EnabledSort=1)
                            begin                    
                                set @SqlTotalCount=@SqlDistinct+' TOP '+ CAST(@PageSize as VARCHAR(4))+' '+ @FieldList+' FROM '+@TableName
                                    +' WHERE '+@Primarykey+' <(SELECT MIN('+ @PrimaryKeyAlias +') FROM ('+ @SqlDistinct+' TOP '+ CAST(@PageSize*(@PageNumber-1) as Varchar(20)) +' '+ @Primarykey +' AS '+@PrimaryKeyAlias+' FROM '+@TableName+' WHERE (1=1) and ' + @QueryCondition +' ORDER BY '+ @SortFields +' '+ @SortTypeB+') AS TBMinID)'+' and '+ @QueryCondition +' ORDER BY '+ @SortFields +' '+ @SortTypeB
                            end
                        else
                            begin
                                set @SqlTotalCount=@SqlDistinct+' TOP '+ CAST(@PageSize as VARCHAR(4))+' '+ @FieldList+' FROM '+@TableName
                                    +' WHERE '+@Primarykey+' >(SELECT MAX('+ @PrimaryKeyAlias +') FROM ('+ @SqlDistinct+' TOP '+ CAST(@PageSize*(@PageNumber-1) as Varchar(20)) +' '+ @Primarykey +' AS '+@PrimaryKeyAlias+' FROM '+@TableName    +' WHERE (1=1) and ' + @QueryCondition +' ORDER BY '+ @SortFields +' '+ @SortTypeB+') AS TBMinID)'+' and '+ @QueryCondition +' ORDER BY '+ @SortFields +' '+ @SortTypeB
                            end           
                    end
                else
                    begin 
                        set @PageNumber = @TemporaryPageCount-@PageNumber+1 --后半部分数据处理
                        if @PageNumber <= 1 --最后一页数据显示
                                set @SqlTotalCount=@SqlDistinct+' * FROM ('+@SqlDistinct+' TOP '+ CAST(@ExtraPageNumber as VARCHAR(4))+' '+ @FieldList+' FROM '+@TableName +' WHERE (1=1) and '+ @QueryCondition +' ORDER BY '+ @SortFields +' '+ @SortTypeA+') AS TempTB'+' ORDER BY '+ @ConvertedSortFields +' '+ @SortTypeB
                        else if(@EnabledSort=1)
                                set @SqlTotalCount=@SqlDistinct+' * FROM ('+@SqlDistinct+' TOP '+ CAST(@PageSize as VARCHAR(4))+' '+ @FieldList+' FROM '+@TableName+' WHERE '+@Primarykey+' >(SELECT MAX('+ @PrimaryKeyAlias +') FROM('+ @SqlDistinct+' TOP '+ CAST(@PageSize*(@PageNumber-2)+@ExtraPageNumber as Varchar(20)) +' '+ @Primarykey+' AS '+@PrimaryKeyAlias+' FROM '+@TableName+' WHERE (1=1) and '+ @QueryCondition +' ORDER BY '+ @SortFields +' '+ @SortTypeA+') AS TBMaxID)' +' and '+ @QueryCondition+' ORDER BY '+ @SortFields +' '+ @SortTypeA+') AS TempTB'+' ORDER BY '+ @ConvertedSortFields +' '+ @SortTypeB
                        else
                                set @SqlTotalCount=@SqlDistinct+' * FROM ('+@SqlDistinct+' TOP '+ CAST(@PageSize as VARCHAR(4))+' '+ @FieldList+' FROM '+@TableName+' WHERE '+@Primarykey+' <(SELECT MIN('+ @PrimaryKeyAlias +') FROM('+ @SqlDistinct+' TOP '+ CAST(@PageSize*(@PageNumber-2)+@ExtraPageNumber as Varchar(20)) +' '+ @Primarykey +' AS '+@PrimaryKeyAlias+' FROM '+@TableName+' WHERE (1=1) and '+ @QueryCondition +' ORDER BY '+ @SortFields +' '+ @SortTypeA+') AS TBMaxID)'+' and '+ @QueryCondition+' ORDER BY '+ @SortFields +' '+ @SortTypeA+') AS TempTB'+' ORDER BY '+ @ConvertedSortFields +' '+ @SortTypeB
                    end    
            end
    
    ------返回查询结果-----
    print @SqlTotalCount
    exec sp_executesql @SqlTotalCount
    --SELECT datediff(ms,@ExecuteTime,getdate()) as 耗时
    SET NOCOUNT OFF



--调用
DECLARE @return_value int,
 @PageCount int,   
 @RecordCount int

SELECT @PageCount = 0 SELECT @RecordCount = 0

EXEC @return_value = [dbo].[usp_CommonDataResourcePaged]   
   @TableName = N'WebErrorLog',   
@FieldList = N'[ID],[Date],[Thread],[Level],[Logger],[Message],[Exception]',   
@PageSize = 30,   
@PageNumber = 50,   
@SortFields = [ID],   
@EnabledSort =N'1' ,   
@QueryCondition = N'',   
@Primarykey = N'[ID]',  
@EnabledDistinct = 1,   
@PageCount = @PageCount OUTPUT,   
@RecordCount = @RecordCount OUTPUT

SELECT @PageCount as N'@PageCount',   @RecordCount as N'@RecordCount'

SELECT 'Return Value' = @return_value