多条件分页存储过程

if OBJECT_ID('lianxi') is not null
drop proc lianxi
go
create proc lianxi
@ye int=1,
@tiao int=3,
@zong int out,
@sheng int=0,
@shi int=0,
@xian int=0,
@qu int=0
as
declare @sql varchar(max),
@where varchar(max),
@rid int,
@sqlcount nvarchar(max)
set @sql='';
set @where=' where 1=1 ';
set @rid=(@ye-1)*@tiao;
set @sqlcount=' select @total=count(*) from shitu';
if @sheng !=0
begin
set @where+=' and shitu.shengid='+STR(@sheng)
end
if @shi !=0
begin
set @where+=' and shitu.shiid='+STR(@shi)
end
if @xian !=0
begin
set @where+=' and shitu.xianid='+STR(@xian)
end
if @qu !=0
begin
set @where+=' and shitu.quid='+STR(@qu)
end
set @sqlcount+=@where
exec sp_executesql @sqlcount,N'@total int out',@total=@zong out
set @sql='select top'+STR(@tiao)+' * from shitu'+@where+'and rid>'+str(@rid);
print(@sql)
exec(@sql)

原文地址:https://www.cnblogs.com/Zhangbao001/p/12160044.html