–多条件分页
alter proc Paging
(
@PageIndex int,
@PageSize int,
@DName varchar(50)=’’,
@BName varchar(50)=’’,
@PageCount int=0 out
)
as
begin
–定义一个Nvarchar的恒成立的Sql语句
declare @Sql Nvarchar(1000) = N’select * from Zong where 1 = 1 ’
–判断查询框DName是否有值
if(len(@DName)>0)
–有值就把这句sql语句拼接到恒成立的sql语句里
set @Sql += ’ and DName like ‘’%’+@DName+’%’’’
–判断查询框BName是否有值
if(len(@BName)>0)
–有值就把这句sql语句拼接到恒成立的sql语句里
set @Sql += ’ and BName = ‘+@BName+’’
–把拼接的字符串转成表明别名TT
set @Sql = ‘(’+@Sql+’)TT’
–总记录数
declare @SqlCount Nvarchar(1000) = N’select @PageCount = count() from’+@Sql
–系统存储过程拼接字符串
exec sp_executesql @sqlCount,N’@PageCount int out’,@PageCount out
–分页
declare @SqlPaging Nvarchar(1000) = ‘select top(’+convert(varchar(20),@PageSize)+’) * from ‘+@Sql+’
where CDJ not in(select top(’+convert(varchar(20),((@PageIndex - 1)@PageSize))+’) CDJ from ‘+@Sql+’)’
–系统存储过程执行字符串
exec sp_executesql @sqlpaging
end
–测试分页结果
declare @Count int
exec Paging 2,2,’’,’’,@Count out
select @Count在这里插入代码片
来源:CSDN
作者:我无敌,你随意!
链接:https://blog.csdn.net/weixin_45287188/article/details/103600771