資料庫非儲存過程分頁

2021-09-17 20:42:16 字數 1626 閱讀 3125

oracle中的分頁

select * from (select a.*,rownum rc from 表名 where rownum<=endrow) a where a.rc>=startrow

select a1.* from (select student.*,rownum rn from student) a1 where rn between startpage and endpage;(使用較多)

db2中的分頁:

select * from (select rownumber() over() as rc,a.* from (select * from 表名 order by 列名) as a) where rc between startrow and endrow

mysql中的分頁:(感覺是所有資料庫中最簡單而且寫法統一的了)

select * from table where … limit 10; #返回前10行

select * from table where … limit 0,10; #返回前10行

select * from table where … limit 10,20; #返回第10-20行資料

而針對sqlserver中的分頁有多種:

常用的是用到了row_number()函式,但是只支援sqlserver2005及以上版本

select top pagenum * from (select row_number()over(order by id)rownumber,* from a)a1 where rownumber>startpage

select * from (select row_number()over(order by id)rownumber,* from a) a1 where rownumber>startpage and rownumberselect * from (select row_number()over(order by id)rownumber,* from a) a1 where rownumber between startpage+1 and endpage

還有這種:

select top pagenum * from a where not exists (select 1 from (select top 30 id from a order by id)a1 where a1.id=a.id) order by id

但是我想說的是被好多人所不關注的一種分頁方法:

select * from 表 order by id offset pageindex*pagenum rows fetch next pagenum rows only

這種方法是不是很簡單,但是這個只有在sql server 2012及以上版本中才能使用,無論是從邏輯讀取數還是響應時間實際執行行數等關鍵引數看,sql server 2012提供的offset/fetch next分頁方式都比row_number()方式有了較大的提公升。

注意:使用該方法必須使用order by ,不然會有語法錯誤。

資料庫端分頁儲存過程

近日專案中要用到大批量的資料分頁顯示,而vs2003中的datagrid中的分頁機制是整個的讀出資料到記憶體,再將記憶體中符合分頁條件的資料顯示到頁面,如果資料量達到一定程式會占用大量系統資源,導致整個程式反應遲鈍。因而只好採用在資料庫端分頁的方法,每次只讀出指定的記錄數。create proced...

MSSQL MySQL 資料庫分頁 儲存過程

先看看單條 sql 語句的分頁 sql 吧。方法1 適用於 sql server 2000 2005 複製 如下 select top 頁大小 from table1 where id not in select top 頁大小 頁數 1 id frwww.cppcns.comom table1 o...

高效資料庫分頁儲存過程詳解

我們要把大量的資料分頁顯示,以前在asp裡或其它程式裡的做法是把資料全部取出來,然後根據不同的頁碼用程式去計算應該顯示哪部分,如果資料量過大的話,可以想像每次都要調出來很多沒用的資料,是不是會很慢呢,而分頁儲存過程解決了這一問題,每次只調出有用的資料,其餘部分的資料並不會傳輸到程式裡,這樣就給資料的...