欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

一、創(chuàng)建測(cè)試表

CREATE TABLE [dbo].[Student](
    [id] [int] NOT NULL,
    [name] [nvarchar](50) NULL,
    [age] [int] NULL)

二、創(chuàng)建測(cè)試數(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

三、測(cè)試

1、使用top關(guān)鍵字

top關(guān)鍵字表示跳過(guò)多少條取多少條

declare @pageCount int  --每頁(yè)條數(shù)
declare @pageNo int  --頁(yè)碼
declare @startIndex int --跳過(guò)的條數(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

測(cè)試結(jié)果:

2、使用row_number()函數(shù)

declare @pageCount int  --頁(yè)數(shù)
declare @pageNo int  --頁(yè)碼
set @pageCount=10
set @pageNo=3
--寫(xiě)法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
--寫(xiě)法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
--寫(xiě)法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)分頁(yè)方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

相關(guān)文章

最新評(píng)論