USE [数据库]
GO
/****** Object: StoredProcedure [dbo].[proc_GetPayDetailsList] Script Date: 2020/5/12 15:32:27 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create Procedure [dbo].[proc_GetPayDetailsList]
@pageIndex int,
@pageSize int,
@sqlWhere varchar(1024),
@orderBy varchar(100),
@totalCount int output
as
begin
declare @sql nvarchar(1024)
declare @temp nvarchar(1024)
declare @total int
set @temp=' from 表名字 b
'
declare @countSql nvarchar(max)
set @countSql='select @a=count(1) '+@temp+' '+@sqlWhere
exec sp_executesql @countSql,N'@a int output',@totalCount output
/* == 如果查询页没有数据,加载第一页的数据 == */
set @total=ceiling(convert(decimal(18,1),@totalCount)/@pageSize)
if @total<@pageIndex
set @pageIndex=@total
set @sql=' select tab.* from
(select row_number() over('+@orderBy+') as num,b.*
'+@temp+' '+@sqlWhere +') tab '
set @sql=@sql+' where tab.num between '+cast(((@pageIndex-1)*@pageSize+1) as varchar) +' and '+cast((@pageIndex*@pageSize) as varchar)
exec(@sql)
--print(@sql)
end