SQLServer存储过程实现单条件分页
话不多说,请看代码:
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 倒序输出若列 小于之前页数的最小值
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持我们!
上一篇:SQL Server 2012降级至2008R2的方法
栏 目:MsSql
下一篇:没有了
本文标题:SQLServer存储过程实现单条件分页
本文地址:https://www.xiuzhanwang.com/a1/MsSql/10517.html
您可能感兴趣的文章
- 01-10SQLServer中防止并发插入重复数据的方法详解
- 01-10SqlServer 在事务中获得自增ID的实例代码
- 01-10SqlServer快速检索某个字段在哪些存储过程中(sql 语句)
- 01-10SQLServer性能优化--间接实现函数索引或者Hash索引
- 01-10SQLServer数据库中开启CDC导致事务日志空间被占满的原因
- 01-10SQLServer中使用扩展事件获取Session级别的等待信息及SQLServer 2016中
- 01-10SQLServer2008新实例远程数据库链接问题(sp
- 01-10SQLServer存储过程创建和修改的实现代码
- 01-10Sqlserver事务备份和还原的实例代码(必看)
- 01-10Sql Server 存储过程调用存储过程接收输出参数返回值
阅读排行
本栏相关
- 01-10SQLServer存储过程实现单条件分页
- 01-10SQLServer中防止并发插入重复数据的方
- 01-10SQL Server 2012降级至2008R2的方法
- 01-10SQL Server性能调优之缓存
- 01-10SQL Server数据库定时自动备份
- 01-10Sql Server 死锁的监控分析解决思路
- 01-10实现SQL Server 原生数据从XML生成JSON数
- 01-10SqlServer快速检索某个字段在哪些存储
- 01-10SqlServer 在事务中获得自增ID的实例代
- 01-10SQLServer性能优化--间接实现函数索引或
随机阅读
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-10SublimeText编译C开发环境设置
- 04-02jquery与jsp,用jquery
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-10C#中split用法实例总结
- 01-10delphi制作wav文件的方法
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-11ajax实现页面的局部加载
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文