1 Create proc [dbo].[GetPageList] 2 @TableName varchar(20), 3 @IdName varchar(10), 4 @PageIndex int, 5 @PageSize int, 6 @RowsCount int output 7 as 8 begin 9 --获取总行数 10 declare @sql1 nvarchar(200) 11 set @sql1=N'select @Count=count(*) from '+@TableName 12 exec sp_executesql @sql1 ,N'@Count int out',@RowsCount out 13 --获取分页数据 14 declare @sql2 nvarchar(100) 15 set @sql2=N'select *,ROW_NUMBER() over(order by '+@IdName+ ' desc) as id1 16 from ' +@TableName 17 set @sql2=N'select top '+ltrim(str(@PageSize))+'* from ('+@sql2+') as t1 18 where id1>' +ltrim(str((@PageIndex-1)*@PageSize)) 19 exec (@sql2) 20 end