一个比较实用的大数据量分页存储过程
时间:2024-01-13 16:11:29
create proc sp_PublicTurnPageWebSite(
@TBName nvarchar(100)='', --表名,如 pinyin
@PageSize int=10, --每页的记录数,默认为 10
@CurPage int=1, --表示当前页 1
@KeyField nvarchar(100)='ID', --关键字段名,默认为 ID,该字段要求是表中的索引 或 无重复和不为空的字段
@KeyAscDesc nvarchar(4)='ASC', --关键字的升、降序,默认为升序 ASC , 降序为 DESC
@Fields nvarchar(500)='*', --所选择的列名,默认为全选
@Condition nvarchar(200)='', --where 条件,默认为空
@Order nvarchar(200)='' --排序条件,默认为空
) with encryption as
if @TBName = ''
begin
raiserror('请指定表名!',11,1)
return
end
if @PageSize <=0 or @CurPage <0
begin
raiserror('当前页数和每页的记录数都必须大于零!',11,1)
return
end
if @KeyAscDesc = 'DESC'
set @KeyAscDesc = '<'
else
set @KeyAscDesc = '>'
if @Condition <> ''
set @Condition = ' where ' + @Condition
declare @SQL nvarchar(2000)
set @SQL = ''
if @CurPage = 1
set @SQL = @SQL + 'Select Top ' + cast(@PageSize as nvarchar(20)) + ' ' + @Fields + ' FROM ' + @TBName + @Condition + ' ' + @Order
else
begin
declare @iTopNum int
set @iTopNum = @PageSize * (@CurPage - 1)
set @SQL = @SQL + 'declare @sLastValue nvarchar(100)' + char(13)
set @SQL = @SQL + 'Select Top ' + cast(@iTopNum as nvarchar(20)) + ' @sLastValue=' + @KeyField + ' FROM ' + @TBName + @Condition + ' ' + @Order + char(13)
declare @Condition2 nvarchar(200)
if @Condition = ''
set @Condition2 = ' where ' + @KeyField + @KeyAscDesc + '@sLastValue '
else
set @Condition2 = ' and ' + @KeyField + @KeyAscDesc + '@sLastValue '
set @SQL = @SQL + 'Select Top ' + cast(@PageSize as nvarchar(20)) + ' ' + @Fields + ' FROM ' + @TBName + @Condition + @Condition2 + @Order
end
EXECUTE sp_executesql @SQL
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python双向链表原理与实现方法详解
PyCharm上安装Package的实现(以pandas为例)
![](https://img.aspxhome.com/file/2023/2/134722_0s.jpg)
Python实现JSON反序列化类对象的示例
Python的Flask项目中获取请求用户IP地址 addr问题
人工智能学习Pytorch梯度下降优化示例详解
![](https://img.aspxhome.com/file/2023/6/72816_0s.png)
JavaScript循环遍历的24个方法,你都知道吗
![](https://img.aspxhome.com/file/2023/6/136776_0s.png)
pandas DataFrame 行列索引及值的获取的方法
一段查看ASP文件源码的ASP程序
pyhanlp安装介绍和简单应用
![](https://img.aspxhome.com/file/2023/7/85277_0s.png)
基于Pydantic封装的通用模型在API请求验证中的应用详解
基于golang channel实现的轻量级异步任务分发器示例代码
深度解读Python如何实现dbscan算法
![](https://img.aspxhome.com/file/2023/2/68892_0s.png)
ASP状态封装类Cache、Cookie & Session
BluePrint CSS框架0.9版发布
python3中关于excel追加写入格式被覆盖问题(实例代码)
Django 批量插入数据的实现方法
Python 如何实现批量转换视频音频的采样率
![](https://img.aspxhome.com/file/2023/7/121027_0s.png)
mysql Event Scheduler: Failed to open table mysql.event
![](https://img.aspxhome.com/file/2023/8/128018_0s.png)
使用Cython中prange函数实现for循环的并行
![](https://img.aspxhome.com/file/2023/3/76773_0s.png)