最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SQL Server實(shí)現(xiàn)分頁方法介紹

 更新時(shí)間:2022年03月15日 10:11:37   作者:.NET開發(fā)菜鳥  
這篇文章介紹了SQL Server實(shí)現(xiàn)分頁的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、創(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有更好的可移植性,能同時(shí)適用于sql2000及以上版本、access。

這篇文章介紹了SQL Server實(shí)現(xiàn)分頁方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

相關(guān)文章

最新評(píng)論

孙吴县| 广州市| 新密市| 黄石市| 库车县| 辽中县| 濉溪县| 梧州市| 墨江| 革吉县| 四平市| 望城县| 平南县| 错那县| 无棣县| 临猗县| 宿松县| 阿尔山市| 林周县| 商洛市| 贺兰县| 河北区| 兴仁县| 南康市| 颍上县| 赤壁市| 彩票| 钟山县| 隆化县| 承德县| 齐河县| 阿巴嘎旗| 永福县| 沿河| 平利县| 辽阳县| 观塘区| 长沙市| 乐业县| 云阳县| 尉犁县|