SQL Server使用row_number分页的实现方法
2019/6/30 17:05:36
本文主要是介绍SQL Server使用row_number分页的实现方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
本文为大家分享了SQL Server使用row_number分页的实现方法,供大家参考,具体内容如下
1、首先是
select ROW_NUMBER() over(order by id asc) as 'rowNumber', * from table1
生成带序号的集合
2、再查询该集合的 第 1 到第 5条数据
select * from (select ROW_NUMBER() over(order by id asc) as 'rowNumber', * from table1) as temp where rowNumber between 1 and 5
完整的Sql语句
declare @pagesize int; declare @pageindex int; set @pagesize = 3 set @pageindex = 1; --第一页 select * from (select ROW_NUMBER() over(order by id asc) as 'rowNumber', * from table1) as temp where rowNumber between (((@pageindex-1)*@pagesize)+1) and (@pageindex*@pagesize) set @pageindex = 2; --第二页 select * from (select ROW_NUMBER() over(order by id asc) as 'rowNumber', * from table1) as temp where rowNumber between (((@pageindex-1)*@pagesize)+1) and (@pageindex*@pagesize) set @pageindex = 3; --第三页 select * from (select ROW_NUMBER() over(order by id asc) as 'rowNumber', * from table1) as temp where rowNumber between (((@pageindex-1)*@pagesize)+1) and (@pageindex*@pagesize) set @pageindex = 4;--第四页 select * from (select ROW_NUMBER() over(order by id asc) as 'rowNumber', * from table1) as temp where rowNumber between (((@pageindex-1)*@pagesize)+1) and (@pageindex*@pagesize)
下面我们来写个存储过程分页
Alter Procedure PagePager @TableName varchar(80), @File varchar(1000),--- @Where varchar(500),---带and连接 @OrderFile varchar(100), -- 排序字段 @OrderType varchar(10),--asc:顺序,desc:倒序 @PageSize varchar(10), -- @PageIndex varchar(10) -- as if(ISNULL(@OrderFile, '') = '') begin set @OrderFile = 'ID'; end if(ISNULL(@OrderType,'') = '') begin set @OrderType = 'asc' end if(ISNULL(@File,'') = '') begin set @File = '*' end declare @select varchar(8000) set @select = ' select ' + @File + ' from ( select *,ROW_NUMBER() over(order by ' + @OrderFile + ' '+ @OrderType + ') as ''rowNumber'' from ' + @TableName + ' where 1=1 ' + @Where + ' ) temp where rowNumber between (((' + @PageIndex + ' - 1) * ' + @PageSize + ')+1) and (' + @PageIndex + '*'+ @PageSize+')' exec(@select)
以上就是本文的全部内容,希望对大家学习row_number分页有所帮助。
这篇关于SQL Server使用row_number分页的实现方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-01-08Docker下的SqlServer发布订阅启用
- 2023-06-05Docker安装MS SQL Server并使用Navicat远程连接
- 2023-05-25深入浅出 SQL Server CDC 数据同步
- 2023-05-12通过空间占用和执行计划了解SQL Server的行存储索引
- 2023-04-24以SQLserver为例的Dapper详细讲解
- 2022-11-30SQL server高级函数查询
- 2022-11-26SQL SERVER数据库服务器CPU不能全部利用原因分析
- 2022-11-21SQL Server 时间算差值/常用函数
- 2022-11-20调试Archery连接SQL Server提示驱动错误
- 2022-10-22SQL Server 完整、差异备份+完整、差异还原(详细讲解,规避错误)