数据库 
首页 > 数据库 > 浏览文章

SQL Server存储过程同时返回分页结果集和总数

(编辑:jimmy 日期: 2024/5/16 浏览:3 次 )

前言

      好长时间没摸数据库了,周末在家写了个报表的存储过程,一时间对使用存储过程实现分页的同时并计算出记录总数不知道怎么更好的去实现。按照我们正常的业务逻辑,存储过程数据首先是分页,其次接受若干查询条件,返回分页结果集的同时还需要返回记录总数给客户端。

      我对于这样一个业务存储过程总结如下:1、内核层,通常也就是要查询的字段或者要计算的字段,这部分单独拿出来。  2、查询条件层。 如果内核只是查询一些字段的话,条件可以放在查询条件层拼接。 如果内核层完全是统计业务逻辑,那么查询条件则必须要放在内核层,像我们常用的SUM、GROUPBY 业务。 3、添加分页参数(也就是我们现在多数用的ROW_NUMBER添加rn参数)。   存储过程里我们一般会单独声明每个部分的变量用于执行时拼接。

存储过程

CREATE proc [dbo].[usp_manyidu]
(
 @seatno nvarchar(30),
 @pageIndex int,
 @pageSize int,
 @rsCount int out
)
as
begin
 declare @sql nvarchar(max)  --拼接内核SQL
 declare @where nvarchar(max)=' where 1=1' --查询条件拼接字符串
 declare @cols nvarchar(max)  --查询字段、计算字段
 declare @sort nvarchar(50)  --排序
  
 set @sql=' from dbo.log where seatno is not null and seatno<>'''' group by seatno '
 set @cols='seatno,SUM(case when manyidu=0 then 1 else 0 end) as manyi,
      SUM(case when manyidu=1 then 1 else 0 end) as yiban,
      SUM(case when manyidu=2 then 1 else 0 end) as bumanyi,
      SUM(case when manyidu IS null or manyidu='''' then 1 else 0 end) as weipingjia'
  
 set @sort='order by seatno'
  
 if(@seatno <>'')
  set @where+=' and seatno='+@seatno
   
  
 declare @strSQL nvarchar(max)
  
 set @strSQL=N'select * from (select ROW_NUMBER() over('+@sort+') as tmpid,* from( select * from (select '+@cols+@sql+') as tmpTable1'+@where+') as tmpTable2) as tmpTable3'
    +' where tmpid between '+STR((@pageIndex-1)*@pageSize+1)+' and '+STR(@pageIndex*@pageSize)
 print @strSQL
 exec(@strSQL) 
  
 set @strSQL='select @total=count(*) from (select '+@cols+@sql+') as tmpTable'+@where
  
 print @strSQL
 exec sp_executesql @strSQL,N'@total int out',@total=@rsCount out
  
   
end
GO

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:SQL2008 详解直接将XML存入到SQL中
下一篇:系统隐形杀手——阻塞与等待(SQL)
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网