表中主鍵必須為標識列,[id] int identity (1,1)
1.分頁方案一:(利用not in和select top分頁)
語句形式:
select top 10 *
from testtable
where (id not in
(select top 20 id
from testtable
order by id))
order by id
select top 頁大小 *
from testtable
where (id not in
(select top 頁大小*頁數 id
from 表
order by id))
order by id
2.分頁方案二:(利用id大於多少和select top分頁,效率最高)
語句形式:
select top 10 *
from testtable
where (id >
(select max(id)
from (select top 20 id
from testtable
order by id) as t))
order by id
select top 頁大小 *
from testtable
where (id >
(select max(id)
from (select top 頁大小*(頁數-1) id
from 表
order by id) as t))
order by id
3.分頁方案三:(利用sql的游標儲存過程分頁)
create procedure sqlpager
@sqlstr nvarchar(4000), --查詢字串
@currentpage int, --第n頁
@pagesize int --每頁行數
asset nocount on
declare @p1 int, --p1是游標的id
@rowcount int
exec sp_cursoropen @p1 output,@sqlstr,@scrollopt=1,@ccopt=1, @rowcount=@rowcount output
select ceiling(1.0*@rowcount/@pagesize) as 總頁數--,@rowcount as 總行數,@currentpage as 當前頁
set @currentpage=(@currentpage-1)*@pagesize+1
exec sp_cursorfetch @p1,16,@currentpage,@pagesize
exec sp_cursorclose @p1
set nocount off
第 4 種
[c-sharp]view plain
copy
print?
select top pagesize * from foodinfo
where foodid <= (select min(t.foodid) as
m from (select top (currpage * pagesize + 1) * from foodinfo order by foodid desc)
ast)
order by foodid desc
三種SQL分頁法
三種sql分頁法 表中主鍵必須為標識列,id int identity 1,1 1.分頁方案一 利用not in和select top分頁 語句形式 select top 10 from testtable where id not in select top 20 id from testtabl...
三種SQL分頁法
三種sql分頁法 表中主鍵必須為標識列,id int identity 1,1 1.分頁方案一 利用not in和select top分頁 語句形式 select top 10 from testtable where id not in select top 20 id from testtabl...
三種SQL分頁法
三種sql分頁法 表中主鍵必須為標識列,id int identity 1,1 1.分頁方案一 利用not in和select top分頁 語句形式 select top 10 from testtable where id not in select top 20 id from testtabl...