Universal paging stored procedures (SQL Server) universal paging stored procedure

Universal paging stored procedure

if exists(select name from sysobjects where name = 'Proc_Pagination' and type = 'P')

drop proc Proc_Pagination

go

Create PROCEDURE Proc_Pagination

    @tblName varchar(255), -- 表名

    @strGetFields varchar (1000) = '*', - the need to return to the column

    @fldName varchar (255) = '', - sorting field name

    @PageSize int = 10, - page size

    @PageIndex int = 1, - p

    @doCount bit = 0, - the total number of records returned, non-zero value

    @OrderType bit = 0, - set the sort type, a non-zero value in descending order

    @strWhere varchar (1500) = '' - the query criteria (Note: Do not add where)

AS

 

declare @strSQL varchar (5000) - the main statement

declare @strTmp varchar (110) - temporary variable

declare @strOrder varchar (400) - type of ordering

if @doCount != 0

    begin

        if @strWhere !=''

            set @strSQL = 'select count(*) as Total from [' + @tblName + '] where '+@strWhere

        else

            set @strSQL = 'select count(*) as Total from [' + @tblName + ']'

    end

    - the code above means that if @doCount passed over is not zero, on the implementation of statistical aggregates. All of the following code is @doCount is 0

 

else

    begin

        if @OrderType != 0

            begin

                set @strTmp = '<(select min'

                set @strOrder = ' order by [' + @fldName +'] desc'

                - If @OrderType not 0, on the implementation of descending, the sentence is very important!

            end

       

        else

            begin

                set @strTmp = '>(select max'

                set @strOrder = ' order by [' + @fldName +'] asc'

            end

            if @PageIndex = 1

                begin

                    if @strWhere != ''

                        set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from [' + @tblName + '] where ' + @strWhere + ' ' + @strOrder

                    else

                        set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ['+ @tblName + '] '+ @strOrder

                        - If the first page on the implementation of the above code, it will speed up execution

                end

            else

                begin

                    - The following code gives @strSQL to really execute SQL code

                    set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ['

                        + @tblName + '] where [' + @fldName + ']' + @strTmp + '(['+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['+ @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'+ @strOrder

                        if @strWhere != ''

                    set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ['

                        + @tblName + '] where [' + @fldName + ']' + @strTmp + '(['

                        + @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['

                        + @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '

                        + @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder

                            end

       end

exec (@strSQL)

GO

if exists(select name from sysobjects where name = 'Proc_Pagination' and type = 'P')

drop proc Proc_Pagination

go

Create PROCEDURE Proc_Pagination

    @tblName varchar(255), -- 表名

    @strGetFields varchar (1000) = '*', - the need to return to the column

    @fldName varchar (255) = '', - sorting field name

    @PageSize int = 10, - page size

    @PageIndex int = 1, - p

    @doCount bit = 0, - the total number of records returned, non-zero value

    @OrderType bit = 0, - set the sort type, a non-zero value in descending order

    @strWhere varchar (1500) = '' - the query criteria (Note: Do not add where)

AS

 

declare @strSQL varchar (5000) - the main statement

declare @strTmp varchar (110) - temporary variable

declare @strOrder varchar (400) - type of ordering

if @doCount != 0

    begin

        if @strWhere !=''

            set @strSQL = 'select count(*) as Total from [' + @tblName + '] where '+@strWhere

        else

            set @strSQL = 'select count(*) as Total from [' + @tblName + ']'

    end

    - the code above means that if @doCount passed over is not zero, on the implementation of statistical aggregates. All of the following code is @doCount is 0

 

else

    begin

        if @OrderType != 0

            begin

                set @strTmp = '<(select min'

                set @strOrder = ' order by [' + @fldName +'] desc'

                - If @OrderType not 0, on the implementation of descending, the sentence is very important!

            end

       

        else

            begin

                set @strTmp = '>(select max'

                set @strOrder = ' order by [' + @fldName +'] asc'

            end

            if @PageIndex = 1

                begin

                    if @strWhere != ''

                        set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from [' + @tblName + '] where ' + @strWhere + ' ' + @strOrder

                    else

                        set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ['+ @tblName + '] '+ @strOrder

                        - If the first page on the implementation of the above code, it will speed up execution

                end

            else

                begin

                    - The following code gives @strSQL to really execute SQL code

                    set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ['

                        + @tblName + '] where [' + @fldName + ']' + @strTmp + '(['+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['+ @fldName + '] from [' + @tblName + ']' + @strOrder + ') as tblTmp)'+ @strOrder

                        if @strWhere != ''

                    set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ['

                        + @tblName + '] where [' + @fldName + ']' + @strTmp + '(['

                        + @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['

                        + @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '

                        + @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder

                            end

       end

exec (@strSQL)

GO

Guess you like

Origin www.cnblogs.com/zunzunQ/p/11577968.html