1. USE [数据库]
    2. GO
    3. /****** Object: StoredProcedure [dbo].[proc_GetPayDetailsList] Script Date: 2020/5/12 15:32:27 ******/
    4. SET ANSI_NULLS ON
    5. GO
    6. SET QUOTED_IDENTIFIER ON
    7. GO
    8. create Procedure [dbo].[proc_GetPayDetailsList]
    9. @pageIndex int,
    10. @pageSize int,
    11. @sqlWhere varchar(1024),
    12. @orderBy varchar(100),
    13. @totalCount int output
    14. as
    15. begin
    16. declare @sql nvarchar(1024)
    17. declare @temp nvarchar(1024)
    18. declare @total int
    19. set @temp=' from 表名字 b
    20. '
    21. declare @countSql nvarchar(max)
    22. set @countSql='select @a=count(1) '+@temp+' '+@sqlWhere
    23. exec sp_executesql @countSql,N'@a int output',@totalCount output
    24. /* == 如果查询页没有数据,加载第一页的数据 == */
    25. set @total=ceiling(convert(decimal(18,1),@totalCount)/@pageSize)
    26. if @total<@pageIndex
    27. set @pageIndex=@total
    28. set @sql=' select tab.* from
    29. (select row_number() over('+@orderBy+') as num,b.*
    30. '+@temp+' '+@sqlWhere +') tab '
    31. set @sql=@sql+' where tab.num between '+cast(((@pageIndex-1)*@pageSize+1) as varchar) +' and '+cast((@pageIndex*@pageSize) as varchar)
    32. exec(@sql)
    33. --print(@sql)
    34. end