SQLServer存储过程实现单条件分页
作者:Mazey 时间:2024-01-14 21:50:01
话不多说,请看代码:
SQLServer Procedure Pagination_basic:
ALTER PROCEDURE [qiancheng].[Pagination_basic] (
@Table_name VARCHAR (255),
--name of table
@Rows_target VARCHAR (1000) = '*',
--search rows
@Rows_condition VARCHAR (1000) = '',
--the condition to find target (no where)
@Rows_order VARCHAR (255) = '',
--the rows to rank
@Order_type INT = 0,
-- *Q*C* 0 normal 1 down
@PageSizes INT = 10,
--the size of each page
@PageIndex INT = 1,
--current page
@ShowPages INT,
--whether show the pages *Q*C* 1-yes 0-no
@ShowRecords INT,
--whether show the record *Q*C* 1-yes 0-no
@Records_total INT OUTPUT,
--returned total records
@Pages_total INT OUTPUT --returned total pages
) AS
DECLARE @MainSQL_QC nvarchar (2000) --Main SQL sentence
DECLARE @Var_QC VARCHAR (100) --Temporary variate
DECLARE @Order_QC VARCHAR (400) --the sort to rank
SET @Records_total = 0
SET @Pages_total = 0
IF @ShowRecords = 1
OR @ShowPages = 1
BEGIN
IF @Rows_condition != ''
SET @MainSQL_QC = 'select @Records_total = count(1) from [' + @Table_name + '] where ' +@Rows_condition
ELSE
SET @MainSQL_QC = 'select @Records_total = count(1) from [' + @Table_name + ']' EXEC sp_executesql @MainSQL_QC,
N'@Records_total int out' ,@Records_total OUTPUT
END
IF @ShowPages = 1
BEGIN
IF @Records_total <= @PageSizes
SET @Pages_total = 1
ELSE
BEGIN
SET @Pages_total = @Records_total /@PageSizes
IF (@Records_total %@PageSizes) > 0
SET @Pages_total = @Pages_total + 1
END
END
IF @Order_type = 1
BEGIN
SET @Var_QC = '<(select min'
SET @Order_QC = ' order by [' + @Rows_order + '] desc'
END
ELSE
BEGIN
SET @Var_QC = '>(select max'
SET @Order_QC = ' order by [' + @Rows_order + '] asc'
END
IF @PageIndex = 1
BEGIN
IF @Rows_condition != ''
SET @MainSQL_QC = 'select top ' + str(@PageSizes) + ' ' +@Rows_target + ' from [' + @Table_name + '] where ' + @Rows_condition + ' ' + @Order_QC
ELSE
SET @MainSQL_QC = 'select top ' + str(@PageSizes) + ' ' +@Rows_target + ' from [' + @Table_name + '] ' + @Order_QC
END
ELSE
BEGIN
IF @Rows_condition != ''
SET @MainSQL_QC = 'select top ' + str(@PageSizes) + ' ' +@Rows_target + ' from [' + @Table_name + '] where [' + @Rows_order + ']' + @Var_QC + '([' + @Rows_order + ']) from (select top ' + str((@PageIndex - 1) *@PageSizes) + ' [' + @Rows_order + '] from [' + @Table_name + '] where ' + @Rows_condition + ' ' + @Order_QC + ') as Tmep_QC) and ' + @Rows_condition + ' ' + @Order_QC
ELSE
SET @MainSQL_QC = 'select top ' + str(@PageSizes) + ' ' +@Rows_target + ' from [' + @Table_name + '] where [' + @Rows_order + ']' + @Var_QC + '([' + @Rows_order + ']) from (select top ' + str((@PageIndex - 1) *@PageSizes) + ' [' + @Rows_order + '] from [' + @Table_name + ']' + @Order_QC + ') as Tmep_QC)' + @Order_QC
END EXEC (@MainSQL_QC)
调用:execute pagination_basic 'UserDetail','*','','id','1','5','1','1','1','',''
主要是末尾的语句,拆分下来便是这样:
select top 每页数 列名 from [表名] where [排序字段名] < --1 倒序输出若列 小于之前页数的最小值
(select min ( [排序字段名] )from --2 获得一个指定列名中的最小值并输出
(select top (当前页-1)*每页数 [排序字段名] from [表名] where [条件] [排序类型]) --3 选择之前页数总数据倒序输出
as Tmep_QC)--4 建立一个名为Tmep_QC的临时表--2 获得一个指定列名中的最小值并输出
and [条件] [排序类型]--1 倒序输出若列 小于之前页数的最小值
来源:http://www.cnblogs.com/mazey/p/6535510.html
标签:sqlserver,存储过程,分页
0
投稿
猜你喜欢
使用Title提升可访问性二
2009-11-16 12:53:00
Python中的Numpy入门教程
2023-04-10 06:59:10
基于Python编写一个宝石消消乐小游戏
2021-10-25 05:46:06
python中os操作文件及文件路径实例汇总
2023-03-20 23:54:09
SQLSERVER对索引的利用及非SARG运算符认识
2024-01-14 23:24:34
实例讲解Python的函数闭包使用中应注意的问题
2022-05-17 23:29:53
Matplotlib 3D 绘制小红花原理
2022-11-23 11:53:08
利用pyinstaller打包exe文件的基本教程
2022-06-09 12:32:13
jupyter notebook 多环境conda kernel配置方式
2022-09-05 04:45:36
python实现批量获取指定文件夹下的所有文件的厂商信息
2021-12-14 20:42:27
python+Django+apache的配置方法详解
2021-02-18 06:39:06
python实现12306登录并保存cookie的方法示例
2021-08-05 18:37:55
Python实现获取命令行输出结果的方法
2023-04-23 09:34:13
带农历的JavaScript日期时间js代码
2010-08-01 10:29:00
Oracle 常用的SQL语句
2024-01-17 08:55:26
SQL Server 的T-SQL高级查询详解
2024-01-17 21:21:04
SQL Server简单模式下误删除堆表记录恢复方法(绕过页眉校验)
2024-01-15 00:18:11
python异步任务队列示例
2021-05-04 03:09:07
OpenLayers3实现图层控件功能
2024-04-17 10:30:46
为您解读CSS优先级
2009-06-18 18:29:00