網站首頁 編程語言 正文
一、創建測試表
CREATE TABLE [dbo].[Student]( [id] [int] NOT NULL, [name] [nvarchar](50) NULL, [age] [int] NULL)
二、創建測試數據
declare @i int set @i=1 while(@i<10000) begin insert into Student select @i,left(newid(),7),@i+12 set @i += 1 end
三、測試
1、使用top關鍵字
top關鍵字表示跳過多少條取多少條
declare @pageCount int --每頁條數 declare @pageNo int --頁碼 declare @startIndex int --跳過的條數 set @pageCount=10 set @pageNo=3 set @startIndex=(@pageCount*(@pageNo-1)) select top(@pageCount) * from Student where ID not in ( select top (@startIndex) ID from Student order by id ) order by ID
測試結果:
2、使用row_number()函數
declare @pageCount int --頁數 declare @pageNo int --頁碼 set @pageCount=10 set @pageNo=3 --寫法1:使用between and select t.row,* from ( select ROW_NUMBER() over(order by ID asc) as row,* from Student ) t where t.row between (@pageNo-1)*@pageCount+1 and @pageCount*@pageNo --寫法2:使用 “>”運算符 select top (@pageCount) * from ( select ROW_NUMBER() over(order by ID asc) as row,* from Student ) t where t.row >(@pageNo-1)*@pageCount --寫法3:使用and運算符 select top (@pageCount) * from ( select ROW_NUMBER() over(order by ID asc) as row,* from Student ) t where t.row >(@pageNo-1)*@pageCount and t.row<(@pageNo)*@pageCount+1
四、總結
ROW_NUMBER()只支持sql2005及以上版本,top有更好的可移植性,能同時適用于sql2000及以上版本、access。
原文鏈接:https://www.cnblogs.com/dotnet261010/p/10784315.html
相關推薦
- 2023-10-15 el-popover彈窗修改三角樣式或者位置
- 2023-07-08 后端返回svg文件流前端展示并下載
- 2022-09-15 C/C++如何實現循環左移,循環右移_C 語言
- 2022-08-22 pyecharts繪制時間輪播圖柱形圖+餅圖+玫瑰圖+折線圖_python
- 2022-05-03 C#面向對象設計原則之接口隔離原則_C#教程
- 2022-10-12 Docker部署搭建WebDav服務的詳細過程_docker
- 2022-01-19 【webpack5】webpack-dev-server 熱更新不能自動刷新瀏覽器
- 2023-03-29 C語言交換奇偶位與offsetof宏的實現方法_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支