SQL Server 2005通用分页存储过程及多表联接应用(sqlserver2008r2安装完成如何打开)居然可以这样

随心笔谈2年前发布 编辑
156 0
🌐 经济型:买域名、轻量云服务器、用途:游戏 网站等 《腾讯云》特点:特价机便宜 适合初学者用 点我优惠购买
🚀 拓展型:买域名、轻量云服务器、用途:游戏 网站等 《阿里云》特点:中档服务器便宜 域名备案事多 点我优惠购买
🛡️ 稳定型:买域名、轻量云服务器、用途:游戏 网站等 《西部数码》 特点:比上两家略贵但是稳定性超好事也少 点我优惠购买

if object_ID(‘[proc_SelectForPager]’) is not null

Drop Procedure [proc_SelectForPager]

Go

Create Proc proc_SelectForPager

(

@Sql varchar(max) ,

@Order varchar(4000) ,

@CurrentPage int ,

@PageSize int,

@TotalCount int output

)

As

Declare @Exec_sql nvarchar(max)

Set @Exec_sql=’Set @TotalCount=(Select Count(1) From (‘+@Sql+’) As a)’

Exec sp_executesql @Exec_sql,N’@TotalCount int output’,@TotalCount output

Set @Order=isnull(‘ Order by ‘+nullif(@Order,”),’ Order By getdate()’)

if @CurrentPage=1

Set @Exec_sql=’

;With CTE_Exec As

(

‘+@Sql+’

)

Select Top(@pagesize) *,row_number() Over(‘+@Order+’) As r From CTE_Exec Order By r

Else

Set @Exec_sql=’

;With CTE_Exec As

(

Select *,row_number() Over(‘+@Order+’) As r From (‘+@Sql+’) As a

)

Select * From CTE_Exec 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

© 版权声明

相关文章