下面小編就為大家分享一篇基于sqlserver的四種分頁方式總結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧。
第一種: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 當(dāng)前頁數(shù)-1*條數(shù) and 頁數(shù)*條數(shù)---
執(zhí)行結(jié)果是:
第二種方式: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í)行結(jié)果是:
第三種方式:--top not in方式 (適應(yīng)于數(shù)據(jù)庫2012以下的版本)
select top 3 * from ArtistModels
where ArtistId not in (select top 15 ArtistId from ArtistModels)
------whereIdnotin(selecttop條數(shù)*頁數(shù) ArtistIdfrom ArtistModels)
執(zhí)行結(jié)果:
第四種方式:用存儲過程的方式進(jìn)行分頁
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í)行結(jié)果:
ps:今天搞了一下午的分頁,通過上網(wǎng)查資料和自己的實驗,總結(jié)了四種分頁方式供大家參考,有問題大家一起交流學(xué)習(xí)。
- SQL Server 2016數(shù)據(jù)庫快照代理過程詳解
- SQL Server 全文搜索功能、全文索引方式介紹
- 關(guān)于SQL Serve數(shù)據(jù)庫r帳號被禁用的處理方法
- SQL數(shù)據(jù)庫查詢優(yōu)化技巧提升網(wǎng)站訪問速度的方法
- SQL數(shù)據(jù)庫開發(fā)中的SSIS 延遲驗證方法
- SQL Server數(shù)據(jù)庫建立新用戶及關(guān)聯(lián)數(shù)據(jù)庫的方法教程
- Oracle數(shù)據(jù)庫多條sql執(zhí)行語句出現(xiàn)錯誤時的控制方式
- Oracle數(shù)據(jù)庫基礎(chǔ):程序中調(diào)用sqlplus的方式
- oracle數(shù)據(jù)庫通過sqlplus連接的幾種方式介紹
- oracle數(shù)據(jù)庫常用分析函數(shù)與聚合函數(shù)的用法