sql 存储过程分页代码 支持亿万庞大数据量
时间:2024-01-24 03:31:00
CREATE PROCEDURE page
@tblName varchar(255), -- 表名
@strGetFields varchar(1000) = '*', -- 需要返回的列
@fldName varchar(255)='id', -- 排序的字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@doCount bit = 0, -- 返回记录总数, 非 0 值则返回
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序 0:asc 1:desc
@strWhere varchar(1500) = '', -- 查询条件 (注意: 不要加 where)
@ID nvarchar(50)='id' --主表的列。。最好是主键
AS
declare @strSQL varchar(5000) -- 主语句
declare @strTmp varchar(110) -- 临时变量
declare @strOrder varchar(400) -- 排序类
if @doCount != 0 begin
if @strWhere !=''
set @strSQL = 'select count(*) as Total from ' + @tblName+ ' where '+@strWhere
else
set @strSQL = 'select count(*) as Total from ' + @tblName + ''
end
--以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况
else begin
if @OrderType != 0 begin
set @strTmp = '<(select min'
set @strOrder = ' order by ' + @fldName +' desc'
--如果@OrderType不是0,就执行降序,这句很重要!
end
else begin
set @strTmp = '>(select max'
set @strOrder = ' order by ' + @fldName +' asc'
end
if @PageIndex = 1 begin
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from ' + @tblName + ' where ' + @strWhere + ' ' + @strOrder
else
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from '+ @tblName + ' '+ @strOrder
--如果是第一页就执行以上代码,这样会加快执行速度
end
else begin
--以下代码赋予了@strSQL以真正执行的SQL代码
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from '
+ @tblName + ' where ' + @fldName + ' ' + @strTmp + '( '+ @ID + ' ) from (select top ' + str((@PageIndex-1)*@PageSize) + ' '+ @fldName + ' from ' + @tblName + @strOrder + ') as tblTmp)'+ @strOrder
if @strWhere != ''
set @strSQL = 'select top ' + str(@PageSize) +' '+@strGetFields+ ' from '
+ @tblName + ' where ' + @fldName + ' ' + @strTmp + '('
+ @ID + ') from (select top ' + str((@PageIndex-1)*@PageSize) + ' '
+ @fldName + ' from ' + @tblName + ' where ' + @strWhere + ' '
+ @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder
end
end
exec (@strSQL)
标签:存储过程,分页
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
django静态文件加载的方法
2022-12-26 13:57:56
Python-嵌套列表list的全面解析
2022-11-08 23:33:08
Pycharm学习教程(7)虚拟机VM的配置教程
2022-10-12 16:49:33
![](https://img.aspxhome.com/file/2023/8/130988_0s.png)
python获得一个月有多少天的方法
2022-02-21 00:55:15
对python中的six.moves模块的下载函数urlretrieve详解
2023-10-20 00:23:45
![](https://img.aspxhome.com/file/2023/6/108146_0s.jpg)
Explain命令在优化查询中的实际应用
2024-01-20 03:54:13
vc6编写python扩展的方法分享
2022-05-12 16:39:30
T-SQL篇如何防止SQL注入的解决方法
2024-01-23 00:17:45
![](https://img.aspxhome.com/file/UploadPic/201211/30/20121130203624954s.png)
Javascript中Math.max和Math.max.apply的区别和用法详解
2024-04-10 10:55:04
Python tempfile模块学习笔记(临时文件)
2022-05-27 02:32:08
python怎么自定义捕获错误
2022-05-25 02:57:35
Python实现抓取网页并且解析的实例
2022-01-12 13:24:53
oracle行转列方法集合汇总(推荐!)
2024-01-12 19:42:14
![](https://img.aspxhome.com/file/2023/1/65671_0s.png)
Django项目使用CircleCI的方法示例
2022-10-17 11:43:24
TensorFlow2.1.0安装过程中setuptools、wrapt等相关错误指南
2023-07-30 10:28:08
![](https://img.aspxhome.com/file/2023/6/93836_0s.png)
如何实现对整个站点所有页面的操作?
2010-05-19 21:20:00
手把手教你Python yLab的绘制折线图的画法
2023-03-11 21:21:43
![](https://img.aspxhome.com/file/2023/7/112587_0s.png)
python GUI库图形界面开发之PyQt5信号与槽的高级使用技巧(自定义信号与槽)详解与实例
2022-02-07 05:22:10
![](https://img.aspxhome.com/file/2023/1/69061_0s.png)
浅谈vue项目利用Hbuilder打包成APP流程,以及遇到的坑
2024-04-10 13:46:11
![](https://img.aspxhome.com/file/2023/3/139693_0s.jpg)
详解python使用Nginx和uWSGI来运行Python应用
2023-07-25 20:40:55