每每面試,總會有公司問到分頁。在下不才,在這里寫幾種分頁,望路過的各位大神盡情拍磚。
先從創建數據庫說起。源碼如下
一.創建數據庫
1 /********************************************************************** 2 一.創建數據庫DBTest 3 @author: Alex Tian 4 Create Date: 2014-03-19 5 ***********************************************************************/ 6 use master --這里我們選擇master數據庫的目的是為了我們可以訪問表 7 --判斷數據庫清單中是否存在數據庫DBTest 8 if exists(select * from sysdatabases where name='DBTest') 9 Drop DataBase DBTest --刪除數據庫DBTest10 Go11 /*創建數據庫的SQL語句,這里我們就創建DBTest數據庫*/12 Create Database DBTest13 on PRimary --默認就是primary文件組,可省略14 (15 /*--數據文件的具體描述--*/16 name='DBTest_data', -- 主數據文件的邏輯名稱17 filename='D:/SQL/DBTest_data.mdf', -- 主數據文件的物理名稱18 size=5mb, --主數據文件的初始大小19 maxsize=100mb, -- 主數據文件增長的最大值20 filegrowth=15%--主數據文件的增長率21 )22 log on23 (24 /*--日志文件的具體描述,各參數含義同上--*/25 name='DBTest_log',26 filename='D:/SQL/DBTest_log.ldf',27 size=2mb,28 filegrowth=1mb29 )
二.創建表
1 /********************************************************************** 2 二.創建表Users 3 ***********************************************************************/ 4 use DBTest --選擇我們剛剛創建的數據庫DBTest 5 Go 6 if Exists (select * from sysobjects where name='Users') 7 Drop Table Users 8 go 9 Create Table Users10 (11 ID int identity(1,1) primary key, --表示是主鍵自增,標示種子是1.12 UName nvarchar(20) Not null, --用戶姓名不能為空13 USex char(2) --性別14 )
三.插入數據
1 /********************************************************************** 2 三.插入數據到表Users 3 ***********************************************************************/ 4 insert into Users 5 select 'yoyo','男' 6 union 7 select 'Alex','男' 8 union 9 select '蘭陽','女'10 union11 select '彭偉','男'12 union13 select '張瓊','男'14 union15 select '肖小仙','女'16 union17 select '毛毛','男'18 union19 select '田勇','男'20 union21 select '田紅','男'22 union23 select '柯麗','女'24 union25 select 'Gross','男'26 union27 select '何軍','男'28 union29 select 'Leo','男'30 union31 select '金瓊','女'32 union33 select '孫龍','男'34 union35 select '老姚','男'36 union37 select '李聰','女'38 union39 select '王超','男'40 union41 select '孫艷','女'42 union43 select '曹瑞','男'44 union45 select '王瓊','女'46 union47 select '沈炎','男'48 union49 select '莊雪月','女'50 union51 select '老丁','男'52 union53 select '景天','男'54 union55 select '雪見','女'56 Go
由于數據量太少,我這里重復插入了上面的測試數據,然后我們查詢當前的表Users 上面都是準備工作,廢話少說。直接插入主題。
1.下面我們用not in語句去分頁,為了方便大家看,直接存儲過程附上。
1 select top 10 * from Users where (ID not in (select top 20 ID from Users order by ID asc) ) 2 order by ID 3 4 create procedure sp_Page_View_with_not_in 5 ( 6 @pageIndex int,--頁索引。 7 @PageSize int--頁記錄數 8 ) 9 as10 begin11 set nocount on12 declare @strSQL varchar(1000)13 set @strSQL='(select top '+str(@PageSize)+' * from Users where (ID not In (select top '+str(@pageIndex*@PageSize)+' ID from Users order by ID asc)) order by ID)' 14 set nocount off15 end16 --drop procedure Page_View_with_not_in --刪除存儲過程
2.用Max分頁,源碼如下
1 --2.使用select top 和select Max(列鍵) 2 3 select top 10 * from Users where 4 ID> 5 ( 6 select MAX(ID) from 7 ( 8 select top 20 ID from Users order by ID 9 ) as temp10 )11 order by ID12 13 --創建存儲過程14 create procedure sp_Page_View_with_Max15 (16 @PageInde int, -- 當前頁索引17 @PageSize int --每頁要顯示的條數18 )19 as20 begin21 declare @strSQL nvarchar(1000)22 set @strSQL='select top '+str(@PageSize)+' * from Users where ID>(select MAX(ID) from (select top +'+str(@PageInde*@PageSize)+' ID from Users order by ID) as Temp )23 order by ID'24 end25 26 --drop procedure sp_Page_View_with_Max
3.用ROW_NUMBER()分頁(僅支持SQL Server 2005或2005之后的數據庫),源碼如下
1 --3.利用Row_number()給數據行加索引(適用于,有很多數據表中沒有identity ID的表) 2 --假設我們當前的Users表沒有identity 3 select RID,UName,USex from 4 ( 5 select *,ROW_NUMBER() over (order by ID) as RID 6 from Users 7 ) 8 as tempTable 9 where RID>20 and RID<=3010 11 create procedure sp_Page_View_With_ROW_NUMBER12 (13 @pageIndex int, 14 @pageSize int15 )16 as17 begin18 declare @strSQL nvarchar(1000)19 set @strSQL='select RID,UName,USex from (select *,ROW_NUMBER() over (order by ID asc) as RID from Users) as TempTable20 where RID>'+str(@pageIndex*@pageSize)+' and RID<='+str(@pageSize*(@pageIndex+1))+'' 21 end
以上是很常用的幾種分頁,當然還有很多種,比如 用臨時表及Row_number ,互聯網的大神們用select max方法用2分法等等等。這里由于數據量很小,我沒有過多的去考慮性能。在這里我不對性能方面作于評價。僅用于面試之類的孩紙們了解一下。
新聞熱點
疑難解答