SQL Server 分页技术(存储过程)

时间:2023-03-09 16:33:05
SQL Server 分页技术(存储过程)
alter proc proc_getpage
@tbname varchar(20),
@field varchar(200)='*',
@orderfield varchar(20),
@ordertype char(20)='asc',
@pagesize int ,
@pageindex int,
@strWhere varchar(500)=''
as
begin
declare @sql varchar(max)
if(@strWhere<>'')
set @strWhere=' where'+@strWhere set @sql='select * from
(select ROW_NUMBER()over(order by '+@orderfield+' '+@ordertype+') as pos, '+@field+' from '+@tbname+' '+@strWhere+' )as t
where pos between '+str((@pageindex-1)*@pagesize+1)+' and '+str(@pagesize*@pageindex) exec( @sql) end