SQL Server存储过程同时返回分页结果集和总数
作者:garfieldzf 时间:2024-01-21 01:31:43
前言
好长时间没摸数据库了,周末在家写了个报表的存储过程,一时间对使用存储过程实现分页的同时并计算出记录总数不知道怎么更好的去实现。按照我们正常的业务逻辑,存储过程数据首先是分页,其次接受若干查询条件,返回分页结果集的同时还需要返回记录总数给客户端。
我对于这样一个业务存储过程总结如下: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
来源:http://www.cnblogs.com/sword-successful/archive/2017/01/16/6289282.html
标签:SQL,Server,存储过程
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python识别处理照片中的条形码
2022-12-10 17:30:42
![](https://img.aspxhome.com/file/2023/4/128284_0s.png)
pytorch 自定义参数不更新方式
2021-11-11 01:55:55
JS创建对象的写法示例
2024-04-16 08:54:48
Python中使用items()方法返回字典元素对的教程
2023-11-20 13:07:09
block 和 inline 答案揭晓~ 另付一则,关于 word-break
2009-12-08 13:06:00
![](https://img.aspxhome.com/file/UploadPic/200912/8/224026855-93s.gif)
ASP用csDrawGraph组件制作饼图、柱状图
2008-04-25 22:58:00
![](https://img.aspxhome.com/file/UploadPic/up/2008042523153992.gif)
mysql中group by与having合用注意事项分享
2024-01-15 02:24:55
CentOS7.2虚拟机上安装MySQL 5.6.32的教程
2024-01-23 07:30:59
![](https://img.aspxhome.com/file/2023/3/121553_0s.png)
使用Python3 编写简单信用卡管理程序
2021-01-17 20:57:59
MySQL错误中文参照列表
2010-09-30 14:41:00
详解Python的条件语句
2021-03-04 08:27:56
Vue官方推荐AJAX组件axios.js使用方法详解与API
2024-05-05 09:08:06
python连接mongodb数据库操作数据示例
2024-01-14 19:16:54
Python设计模式中的行为型策略模式
2023-02-28 17:04:25
![](https://img.aspxhome.com/file/2023/3/113063_0s.png)
微信小程序 scroll-view实现上拉加载与下拉刷新的实例
2024-04-23 09:30:40
![](https://img.aspxhome.com/file/2023/7/135487_0s.gif)
python分析inkscape路径数据方案简单介绍
2021-05-13 14:51:09
![](https://img.aspxhome.com/file/2023/6/127326_0s.png)
用于WebKit的CSS诀窍[译]
2009-03-11 20:03:00
![](https://img.aspxhome.com/file/UploadPic/20093/11/01-94s.jpg)
详解mysql中的存储引擎
2024-01-27 05:53:15
![](https://img.aspxhome.com/file/2023/2/71792_0s.png)
JSP实现登录功能之添加验证码
2023-07-16 12:18:09
网易首页的新闻代码
2022-02-24 16:01:15