基于sqlserver的四種分頁方式總結
第一種:ROW_NUMBER() OVER()方式
select * from (
select *, ROW_NUMBER() OVER(Order by ArtistId ) AS RowId from ArtistModels
) as b
where RowId between 10 and 20
---where RowId BETWEEN 當前頁數(shù)-1*條數(shù) and 頁數(shù)*條數(shù)---
執(zhí)行結果是:
第二種方式:offset fetch next方式(SQL2012以上的版本才支持:推薦使用 )
select * from ArtistModels order by ArtistId offset 4 rows fetch next 5 rows only
--order by ArtistId offset 頁數(shù) rows fetch next 條數(shù) rows only ----
執(zhí)行結果是:
第三種方式:--top not in方式 (適應于數(shù)據(jù)庫2012以下的版本)
select top 3 * from ArtistModels
where ArtistId not in (select top 15 ArtistId from ArtistModels)
------where Id not in (select top 條數(shù)*頁數(shù) ArtistId from ArtistModels)
執(zhí)行結果:
第四種方式:用存儲過程的方式進行分頁
CREATE procedure page_Demo
@tablename varchar(20),
@pageSize int,
@page int
AS
declare @newspage int,
@res varchar(100)
begin
set @newspage=@pageSize*(@page - 1)
set @res='select * from ' +@tablename+ ' order by ArtistId offset '+CAST(@newspage as varchar(10)) +' rows fetch next '+ CAST(@pageSize as varchar(10)) +' rows only'
exec(@res)
end
EXEC page_Demo @tablename='ArtistModels',@pageSize=3,@page=5
執(zhí)行結果:
ps:今天搞了一下午的分頁,通過上網(wǎng)查資料和自己的實驗,總結了四種分頁方式供大家參考,有問題大家一起交流學習。
相關文章
SQL Server誤區(qū)30日談 第9天 數(shù)據(jù)庫文件收縮不會影響性能
收縮文件的過程非常影響性能,這個過程需要移動大量數(shù)據(jù)從而造成大量IO,這個過程會被記錄到日志從而造成日志暴漲,相應的,還會占去大量的CPU資源2013-01-01
sqlserver數(shù)據(jù)庫導入數(shù)據(jù)操作詳解(圖)
本文主要介紹的是怎么使用Microsoft SQL Server Management Studio導入數(shù)據(jù),大家參考使用吧2014-01-01
SQL Server數(shù)據(jù)庫文件過大而無法直接導出解決方案
這篇文章主要介紹了SQL Server數(shù)據(jù)庫文件過大而無法直接導出解決方案,文中通過代碼示例講解的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下2024-08-08
SQL?Server?DATEDIFF()?函數(shù)用法
這篇文章主要介紹了SQL?Server?DATEDIFF()?函數(shù)的定義和用法,通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-12-12

