SQL server分頁的三種方法

2021-10-19 04:32:20 字數 1823 閱讀 5365

var datacount  = test.

orderby

(t =

> t.testid)

.skip

(pagesize *

(pageindex -1)

).take

(pagesize)

.tolist()

;

select

top(需要顯示的條目數)

*from

dbtest

where testid notin(

select

top(需要剔除的條目數) testid from dbtest)

第一種:

create

proc proc_testpage

@pageindex

int--第幾頁

@pagesize

int--每頁顯示的條數

@pagecount

int output --總的頁數,因為需要顯示頁數,因此是個輸出引數

asdeclare

@datacount

int--總資料條數

select

@datacount

=count(*

)from dbtest--獲得總資料條數值並賦給引數

set@pagecount

=ceiling(

1.0*

@datacount

/@pagesize

)--獲得總頁數,並賦給引數

select

top(

@pagesize)*

from dbtest where testid not

int(

select

top(

@pagesize*(

@pageindex-1

))from dbtest)

select

@pagecount

=count(*

)from dbtest

第二種:

create

proc p_test --建立儲存過程p_test

@pagesize

int,

--每頁資料條數

@pageindex

int,

--當前頁數(頁碼)

@pagecount

int output --總的頁數,因為需要顯示頁數,因此是個輸出引數

asdeclare

@datacount

int--總資料條數

select

@datacount

=count(*

)from dbtest --獲得總資料條數值並賦給引數

set@pagecount

=ceiling(

1.0*

@datacount

/@pagesize

)--獲得總頁數,並賦給引數

--接下來是獲得指定頁資料

select

*from

(select

*,row_number(

)over

(order

by testid )

as num from dbtest)

astemp

where num between

@pagesize*(

@pageindex-1

)+1and

@pagesize

*@pageindex

SQL Server的三種分頁方式

目前常見的三種sql分頁方式 top not in方式 select top 條數 from tablename where id not in select top 條數 頁數 id from tablename row number over 方式 select from select row ...

sqlserver三種分頁查詢方法

假設有表student,每頁顯示10條記錄,查詢第5頁的內容。from student where idnotin 40是這麼計算出來的 10 5 1 select top 40 idfrom student order byid order by id原理 需要拿出資料庫的第5頁,就是40 50條...

SqlServer 三種分頁查詢語句

先說好吧,查詢的資料排序,有兩個地方 1 分頁前的排序。2 查詢到當前頁資料後的排序 1 先查詢當前頁碼之前的所有資料id select top 當前頁數 1 每頁資料條數 id from 表名 2 再查詢所有資料的前幾條,但是id不在之前查出來的資料中 select top 每頁資料條數 from...