现在位置:
首页 > SQL Server > 正文
sql server使用ROW_NUMBER分页
- ALTER PROC usp_GetPageList
- (
- @tableName VARCHAR(20) ,
- @selectColunms VARCHAR(1000) ,
- @sortColumn VARCHAR(50) ,
- @sortType VARCHAR(4) ,
- @pageIndex INT ,
- @pageSize INT
- )
- AS
- BEGIN
- DECLARE @sql NVARCHAR(4000)
- SET @sql = '
- SELECT *
- FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY '+ @sortColumn +' '+ @sortType +' ) AS orderIndex ,
- ' + @selectColunms + '
- FROM ' + @tableName + '
- ) t
- WHERE t.orderIndex > '
- + CAST(@pageSize * ( @pageIndex - 1 ) AS VARCHAR(2)) + '
- AND t.orderIndex <=' + CAST(@pageIndex * @pageSize AS VARCHAR(2))
- EXEC(@sql)
- END