Sql 資料分頁解決方案

2021-04-16 21:34:16 字數 1496 閱讀 3323

很多開始學習程式設計的朋友們在使用資料庫自定義分頁的時候,會遇到寫不好資料分頁儲存過程的問題。

這裡我就自己的一點經驗和學習心得提供幾種資料庫內分頁的儲存過程和大家分享一下。

1、使用top

1.1利用當前記錄號(currentnote)和分頁頁面大小(pagesize)進行分頁

create proc getnextpageinfo2

@pagesize int,               --page size

@currentnote int          --current note

asdeclare @sql nvarchar(200)

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

' * from dbo.userinfo where userid > ' + convert(varchar(10),@currentnote)

exec sp_executesql @sql

go1.2利用本頁頁碼和分頁頁面大小進行分頁

create proc getnextpageinfo

@pagesize int,       --page size

@page int     --currentpage no

asdeclare @jilu bigint

set @jilu=(@pagesize*@page)

declare @sql nvarchar(200)

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

' * from dbo.userinfo where userid not in (select top ' + convert(varchar(10),@jilu) +

' userid from userinfo order by userid) order by userid'

exec sp_executesql @sql

go兩種方式比較:第一種執行效率應該高於第二種,不過再id號多變的情況下第二種方法使用起來相對簡單。

2、使用rowcount

3、2.1利用當前記錄號(currentnote)和分頁頁面大小(pagesize)進行分頁

create proc getnextpageinfo3

@pagesize int,              ----分頁大小  

@currentnote int          ----當前頁最後一條記錄號as

set rowcount @pagesize

select * from userinfo where userid>@currentnote

當然,除上面的幾種外因該還有很多其他方法。希望大家在以後學習的過程中能過多多交流,大家也好共同進步。

Sql 資料分頁解決方案

很多開始學習程式設計的朋友們在使用資料庫自定義分頁的時候,會遇到寫不好資料分頁儲存過程的問題。這裡我就自己的一點經驗和學習心得提供幾種資料庫內分頁的儲存過程和大家分享一下。1 使用 top1.1 利用當前記錄號 currentnote 和分頁頁面大小 pagesize 進行分頁 create pro...

SQL 常用sql分頁解決方案

分頁方案一 利用not in和select top分頁 語句形式 select top10 from testtable where id notin select top20 idfrom testtable order byid order byid select top頁大小 from tes...

pageHelper分頁失效解決方案

pagehelper是一款優秀的mybatis分頁外掛程式,在專案中可以非常便利的使用,使開發效率得到很大的提公升,但不支援一對多結果對映的分頁查詢,所以在平時的使用時,對於一對多分頁會出現分頁錯誤,這篇文章主要對pagehelper分頁錯誤進行重現以及提出解決方案。使用者表 user 11條資料 ...