網(wǎng)站首頁 編程語言 正文
一、創(chuàng)建測試表
CREATE TABLE [dbo].[Student]( [id] [int] NOT NULL, [name] [nvarchar](50) NULL, [age] [int] NULL)
二、創(chuàng)建測試數(shù)據(jù)
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關(guān)鍵字
top關(guān)鍵字表示跳過多少條取多少條
declare @pageCount int --每頁條數(shù) declare @pageNo int --頁碼 declare @startIndex int --跳過的條數(shù) 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
測試結(jié)果:
2、使用row_number()函數(shù)
declare @pageCount int --頁數(shù) 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:使用 “>”運(yùn)算符 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運(yùn)算符 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
四、總結(jié)
ROW_NUMBER()只支持sql2005及以上版本,top有更好的可移植性,能同時適用于sql2000及以上版本、access。
原文鏈接:https://www.cnblogs.com/dotnet261010/p/10784315.html
相關(guān)推薦
- 2022-04-09 IDEA中命令行安裝git報錯:error: failed to push some refs to
- 2023-10-16 微信小程序瀏覽docx,pdf等文件在線預(yù)覽使用wx.openDocument
- 2022-09-16 Python中的socket網(wǎng)絡(luò)模塊介紹_python
- 2022-04-25 Pycharm下載pyinstaller報錯:You?should?consider?upgradi
- 2022-03-10 Android如何獲取APP啟動時間_Android
- 2022-03-26 docker?registry?私有倉庫的搭建過程_docker
- 2022-12-01 Python?subprocess庫六個實例快速掌握_python
- 2022-11-12 c++自定義sort()函數(shù)的排序方法介紹_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支