SQL分页查询存储过程代码分享
作者:欣宇 时间:2024-01-12 23:51:04
话不多说,请看代码
-------------------------------------
-----作者:张欣宇
-----时间:2013-06-28
-----简介:根据参数和条件分页查询
-------------------------------------
Create proc [dbo].[Up_PagingQueryByParameter]
(
----- 表名或能查询到结果的SQL语句{SQL语句左右必须有括号例:(select * from tbl1)}
@TableName varchar(max),
----- 要查询的列名语句; 可空默认*
@ColumnName varchar(5000),
----- 用来排序的列; 不可为空
@OrderByColumnName varchar(50),
----- 排序desc(倒序5.4.3.2.1)或asc(正序1.2.3.4.5); 可空默认asc
@ShrtBy varchar(4),
----- Where条件; 可空默认1=1
@Where varchar(5000),
----- 每页显示数; 可空默认20
@PageShows int,
----- 当前页数; 可空默认1
@CurrentPage int,
----- 0为分页查询;其他为查询全部; 可空默认0
@IsPaging int
)
as
begin
----- 参数检查及规范
if isnull(@Where,N'')=N'' set @Where = N'1=1';
if isnull(@ColumnName,N'')=N'' set @ColumnName = N'*';
if isnull(@PageShows,0)<1 set @PageShows = 20;
if isnull(@CurrentPage,0)<1 set @CurrentPage = 1;
if isnull(@ShrtBy,N'')=N'' set @ShrtBy = 'asc';
if isnull(@IsPaging,0)<>1 set @IsPaging = 0;
----- 定义
-- 拼接的SQL语句
declare @SqlStr nvarchar(max);
declare @SqlWithStr nvarchar(max);
-- 开始条数
declare @StartIndex int;
-- 结束条数
declare @EndIndex int;
----- 赋值
set @StartIndex = (@CurrentPage-1)*@PageShows+1;
print(@CurrentPage);
print(@PageShows);
print(@StartIndex);
set @EndIndex = @CurrentPage*@PageShows;
print(@EndIndex);
set @OrderByColumnName=@OrderByColumnName+' '+@ShrtBy;
----- 分页查询
set @SqlWithStr = N'with temp as(select ROW_NUMBER() over(order by '+@OrderByColumnName+N') as RowNumber,'+@ColumnName+N' from '+@TableName+N' as tableName where '+@Where+N')';
if(@IsPaging = 0)
begin
set @SqlStr = @SqlWithStr + N' select '+@ColumnName+N' from temp where temp.RowNumber between '+convert(nvarchar(20),@StartIndex)+N' and '+convert(nvarchar(20),@EndIndex)+N'';
---- print(@SqlStr);
exec(@SqlStr);
----- 总数查询
set @SqlStr = @SqlWithStr + N' select count(*) as TotalNumber from temp';
---- print(@SqlStr);
exec(@SqlStr);
end
else
begin
set @SqlStr = @SqlWithStr + N' select '+@ColumnName+N' from temp';
---- print(@SqlStr);
exec(@SqlStr);
end
end
来源:http://www.cnblogs.com/zhang625161495/p/6217020.html
标签:sql,分页,存储过程
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python数据清洗系列之字符串处理详解
2023-08-02 04:00:07
![](https://img.aspxhome.com/file/2023/0/113060_0s.png)
Python3.4解释器用法简单示例
2022-01-04 10:30:39
MySql常用操作SQL语句汇总
2024-01-21 00:11:53
bitbucket搭建详细过程记录
2023-08-25 23:52:31
![](https://img.aspxhome.com/file/2023/2/131792_0s.jpg)
SpringBoot用多线程批量导入数据库实现方法
2024-01-23 23:03:36
让字体美起来
2011-06-14 09:50:21
Sql server2005 优化查询速度50个方法小结
2024-01-26 16:51:52
pytorch tensor int型除法出现的问题
2021-05-18 13:18:17
![](https://img.aspxhome.com/file/2023/5/121155_0s.jpg)
通过实例了解Render Props回调地狱解决方案
2024-04-22 13:04:36
Python设计模式结构型享元模式
2023-12-19 13:57:40
![](https://img.aspxhome.com/file/2023/4/115664_0s.png)
Java连接MYSQL数据库的实现步骤
2024-01-24 01:23:33
Python 动态导入对象,importlib.import_module()的使用方法
2023-01-24 01:48:47
Python中的类与类型示例详解
2023-08-12 12:21:42
![](https://img.aspxhome.com/file/2023/7/62287_0s.png)
css样式表实现首写字母大写
2007-10-08 12:11:00
MySQL索引之聚集索引介绍
2024-01-24 10:02:44
![](https://img.aspxhome.com/file/2023/9/124499_0s.jpg)
设计师的幸福
2009-05-21 11:59:00
使用sklearn对多分类的每个类别进行指标评价操作
2022-04-20 17:19:39
![](https://img.aspxhome.com/file/2023/9/114549_0s.jpg)
python解决网站的反爬虫策略总结
2022-11-04 12:57:34
如何实现论坛的树状记录表展开技术?
2010-05-19 21:37:00
python实现连接mongodb的方法
2022-10-18 08:34:18