SQL Server使用ROW_NUMBER函数进行分页

ALTER PROCEDURE PROC_PAGEING

    @qCols varchar(4000),             --  查询列	
    @qTables   varchar(4000),          --  查询表
    @qWhere   varchar(1000),          --  条件
    @oKey        varchar(100),        --  排序
    @pageSize         int,            --  大小
    @pageNumber       int = 1            --, 0  页码
AS
set nocount on
BEGIN

    DECLARE @sqlstr AS varchar(8000)
    DECLARE @sqlTable AS varchar(8000)

set @pageNumber = @pageNumber - 1;
set @sqltable='SELECT ROW_NUMBER() OVER(ORDER BY '+ @oKey + '  ) AS RowId,'+ @qCols + '  
      FROM '+@qTables + ' where  '+ @qWhere ;
set @sqlstr='SELECT * FROM ( '+@sqlTable+' ) AS D 
WHERE RowId between '+ str(@pageNumber*@pagesize +1)+ ' AND '+ str((@pageNumber +1)*@pagesize) ;
exec (@sqlstr);
END

你可能感兴趣的:(分页,sqlserver,row_number)