sql server dynamic pagination

RYPlatformManagerDB the USE 
the GO 
the SET the ANSI_NULLS, the ON the QUOTED_IDENTIFIER 
the GO 
the CREATE Proc [the dbo] [WEB_PageView]. 
    @Tablename NVARCHAR ( 2000 ), - table 
    @ReturnFields NVARCHAR ( 1000 ) = ' * ' , - queries column 
    @PageSize the INT = 10 - page number 
    @PageIndex the INT = . 1 , - the current page 
    @Where NVARCHAR ( 1000 ) = '' , - query 
    @OrderBy NVARCHAR ( 1000), - sorting field 
    @PageCount the INT the OUTPUT,                 - maximum page number 
    @RecordCount the INT the OUTPUT                 - total number of records 
the WITH the ENCRYPTION the AS

 - setting properties 
the SET the NOCOUNT the ON

 - variable definition 
the DECLARE the INT @TotalRecord 
the DECLARE the INT @TotalPage 
the DECLARE the INT @CurrentPageSize 
the DECLARE the INT @TotalRecordForPageIndex 

the BEGIN 
    the IF @Where the SET NULL @Where the IS = N ''
     
    - total number of records 
    the DECLARE @countSql NVARCHAR ( 4000 )   
    
    the IF @RecordCount the IS NULL 
    the BEGIN 
        the SET @countSql ='SELECT @TotalRecord=Count(*) From '+@TableName+' '+@Where
        EXECUTE sp_executesql @countSql,N'@TotalRecord int out',@TotalRecord OUT
    END
    ELSE
    BEGIN
        SET @TotalRecord=@RecordCount
    END        
    
    SET @RecordCount=@TotalRecord
    SET @TotalPage=(@TotalRecord-1)/@PageSize+1    
    SET @CurrentPageSize=(@PageIndex-1)*@PageSize

    -- 返回总页数和总记录数
    SET @PageCount=@TotalPage
    SET @RecordCount=@TotalRecord
        
    -- 返回记录
    SET @TotalRecordForPageIndex=@PageIndex*@PageSize
    
    EXEC    ('SELECT *
            FROM (SELECT TOP '+@TotalRecordForPageIndex+' '+@ReturnFields+', ROW_NUMBER() OVER ('+@OrderBy+') AS PageView_RowNo
            FROM '+@TableName+ ' ' + @Where +' ) AS TempPageViewTable
            WHERE TempPageViewTable.PageView_RowNo > 
            '+@CurrentPageSize)
    
END
RETURN 0

GO

 

Guess you like

Origin www.cnblogs.com/codeDevotee/p/11332221.html