SQL分頁語句及簡單的SQL分頁儲存過程

2021-04-29 05:07:34 字數 4641 閱讀 5854

假分頁:從資料庫中選擇所有的記錄後再進行分頁

真分頁:只從資料庫中選擇當前頁的記錄

sql**

select

top每頁顯示的記錄數 *fromtopicwhereid not

in  

(select

top(當前的頁數-1)×每頁顯示的記錄數 idfromtopicorder

byiddesc)   

order

byiddesc

select top 每頁顯示的記錄數 * from topic where id not in

(select top (當前的頁數-1)×每頁顯示的記錄數 id from topic order by id desc)

order by id desc

需要注意的是在access中不能是top 0,所以如果資料只有一頁的話就得做判斷了。。

sql2005中的分頁**:

sql**

withtemptblas(   

selectrow_number() over (order

byiddesc)asrow,    

...   

)   

select*fromtemptblwhererow between

@startindex and

@endindex  

with temptbl as (

select row_number() over (order by id desc)as row,

...)select * from temptbl where row between @startindex and @endindex

分頁儲存過程:

sql**

create

procedureproc_pager   

(   @startindexint,--開始記錄數

@endindexint

--結束記錄數

)   

as

begin

declare@indextabletable(idintidentity(1,1),nidint)   

insert

into@indextable(nid)selectorderidfromordersorder

byorderiddesc

select*    

fromorders o   

inner

join

@indextable i   

ono.orderid=i.nid   

wherei.id between

@startindex and

@endindex   

order

byi.id   

end

create procedure proc_pager

( @startindex int,--開始記錄數

@endindex int --結束記錄數)as

begin

declare @indextable table(id int identity(1,1),nid int)

insert into @indextable(nid) select orderid from orders order by orderid desc

select *

from orders o

inner join @indextable i

on o.orderid=i.nid

where i.id between @startindex and @endindex

order by i.id

end

sql**

create

procedureproc_pager1   

(   @pageindexint, -- 要選擇第x頁的資料

@pagesizeint

-- 每頁顯示記錄數

)   

as

begin

declare@sqlstrvarchar(500)   

set@sqlstr='select top '

+convert

(varchar(10),@pagesize)+   

' * from orders where orderid not in(select top '

+   

convert

(varchar(20),(@pageindex-1)*@pagesize)+   

' orderid from orders) order by orderid'

exec(@sqlstr)   

end

create procedure proc_pager1

( @pageindex int, -- 要選擇第x頁的資料

@pagesize int -- 每頁顯示記錄數)as

begin

declare @sqlstr varchar(500)

set @sqlstr='select top '+convert(varchar(10),@pagesize)+

' * from orders where orderid not in(select top '+

convert(varchar(20),(@pageindex-1)*@pagesize)+

' orderid from orders) order by orderid'

exec (@sqlstr)

end

下面這個僅適用sql2005

sql**

create

procedureproc_pager2   

(   @startindexint,--開始記錄數

@endindexint

--結束記錄數

)   

as

begin

withtemptblas

(selectrow_number() over (order

byorderiddesc)asrow, *fromorders)   

select*fromtemptbl   

whererow between

@startindex and

@endindex   

order

byrow   

end

實現分頁的簡單sql語句

要求選取 tbllendlist 中 第n頁的記錄,每一頁100條記錄。方法1 select top 100 from tbllendlist where fldserialno not in select top n 1 100 fldserialno from tbllendlist order...

Sql 分頁語句

with temptb as select row number over order by id as rowid,from pagecut select from temptb where rowid between 2 50 and 2 50 50 這是乙個資料分頁方法,從sql2005起就支...

SQL分頁語句

這個分頁方法 sql分頁語句 本人對原作者的方案二做了小小的改動 原語句 select top 頁大小 from table1 where id select isnull max id 0 from select top 頁大小 頁數 1 id from table1 order by id a ...