SQL Server 存儲過程分頁


每每面試,總會有公司問到分頁。在下不才,在這裏寫幾種分頁,望路過的各位大神盡情拍磚。

先從創建數據庫說起。源碼如下

一.創建數據庫

 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   --刪除數據庫DBTest
10 Go
11 /*創建數據庫的SQL語句,這裏我們就創建DBTest數據庫*/
12 Create Database DBTest
13 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 on
23  (
24  /*--日誌文件的具體描述,各參數含義同上--*/
25      name='DBTest_log',
26      filename='D:\SQL\DBTest_log.ldf',
27      size=2mb,
28      filegrowth=1mb
29  )

二.創建表

 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 Users
10 (
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 union
11 select '彭偉',''
12 union
13 select '張瓊',''
14 union
15 select '肖小仙',''
16 union
17 select '毛毛',''
18 union
19 select '田勇',''
20 union
21 select '田紅',''
22 union
23 select '柯麗',''
24 union
25 select 'Gross',''
26 union
27 select '何軍',''
28 union
29 select 'Leo',''
30 union
31 select '金瓊',''
32 union
33 select '孫龍',''
34 union
35 select '老姚',''
36 union
37 select '李聰',''
38 union
39 select '王超',''
40 union
41 select '孫豔',''
42 union
43 select '曹瑞',''
44 union
45 select '王瓊',''
46 union
47 select '沈炎',''
48 union
49 select '莊雪月',''
50 union
51 select '老丁',''
52 union
53 select '景天',''
54 union
55 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  as
10  begin
11     set nocount on
12     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 off
15  end
16  --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 temp
10  )
11 order by ID
12 
13 --創建存儲過程
14  create procedure sp_Page_View_with_Max
15  (
16     @PageInde int, -- 當前頁索引
17     @PageSize int  --每頁要顯示的條數
18  )
19  as
20  begin
21     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  end
25  
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<=30
10 
11 create procedure sp_Page_View_With_ROW_NUMBER
12 (
13     @pageIndex int, 
14     @pageSize int
15 )
16 as
17 begin
18     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 TempTable
20 where RID>'+str(@pageIndex*@pageSize)+' and RID<='+str(@pageSize*(@pageIndex+1))+''    
21 end


以上是很常用的幾種分頁,當然還有很多種,比如 用臨時表及Row_number ,互聯網的大神們用select max方法用2分法等等等。
這裏由於數據量很小,我沒有過多的去考慮性能。在這裏我不對性能方面作於評價。僅用於面試之類的孩紙們瞭解一下。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章