标签:
if object_ID(‘[proc_SelectForPager]‘) is not null Drop Procedure [proc_SelectForPager] Go Create Proc proc_SelectForPager ( @Tb_name varchar(max) ,--表名 @Order varchar(4000) ,--排序字段 @CurrentPage int ,--当前页 @Field varchar(400),--查询字段 @PageSize int,--每页显示记录数 @TotalCount int output ) As Declare @Exec_sql nvarchar(max) /* 查询总记录数*/ Set @Exec_sql=‘Set @TotalCount=(Select Count(1) From ‘+@Tb_name+‘ As a)‘ Exec sp_executesql @Exec_sql,N‘@TotalCount int output‘,@TotalCount output /*设置排序字段*/ Set @Order=isnull(‘ Order by ‘+nullif(@Order,‘‘),‘ Order By getdate()‘) /*分页查询*/ Set @Exec_sql=‘ ;With CTE As ( Select *,row_number() Over(‘+@Order+‘) As r From (Select * from ‘+@Tb_name+‘) As a ) Select ‘+@Field+‘ From CTE Where r Between (@CurrentPage-1)*@pagesize+1 And @CurrentPage*@pagesize Order By r ‘ Exec sp_executesql @Exec_sql,N‘@CurrentPage int,@PageSize int‘,@CurrentPage,@PageSize Go
标签:
原文地址:http://www.cnblogs.com/langhua/p/5010777.html