也談數據分頁
分頁的相關概念:
1)當前頁:即要顯示或打開的那一頁的頁數
currPage
2)頁面大小:即每頁要顯示數據的數量
如:每頁都顯示10條數據
pageSize
3)總數據:要顯示的數據的總數,即要顯示的結果集
totalSize
4)總頁數:即顯示總數據需要的頁數
totalPage
它的計算公式為: (totalSize+pageSize-1)/pageSize
例如:
如要顯示17條數據,每頁顯示5條,那么總頁數: totalPage=(17+5-1)/5=4
5)計算當前頁的第一條數據 計算公式:
(currPage-1)*pageSize+1
如要顯示17 條數據,每頁顯示5條,那么第3頁數據第一條數據是:
(3-1)*5+1=11
6)計算當前頁的最后一條數據 計算公式:
(currPage-1)*pageSize+pageSize
如要顯示17 條數據,每頁顯示5條,那么第3頁數據最后一條數據是:
(3-1)*5+5=15
SQL SERVER 2005 實現分頁的方式
一:存儲過程方式
if exists(select * from sysobjects where name='pro_pageData')
drop procedure pro_pageData
go
create procedure pro_pageData
@pageNum int
as
select *
from
(
select *, row_number() over (order by stuid) as rowno
from student
) as s
where rowno>=(@pageNum-1)*5+1 and rowno <= (@pageNum -1)*5+5;
go
exec pro_pageData 4
go
二:一般語句方式(預編譯)
select *
from
(
select *, row_number() over (order by stuid) as rowno
from student
) as s
where rowno>=(?-1)*5+1 and rowno <= (?-1)*5+5;
三:按條件查詢后再對結果進行分頁
select *
from
(
select *, row_number() over (order by stuid) as rowno
from student
where 1=1 and stuName like '%羅%'
) as s
where rowno>=(1-1)*5+1 and rowno <= (1 -1)*5+5;
注意:第一個where 用來匹配查詢條件;
第二個where 用來顯示特定頁數據;
補充:分頁數據的存儲過程(完整版)
go
use Db_TonyPaging
go
if exists (select 1
from sysobjects
where id = object_id('DepartDemo')
and type = 'U')
drop table DepartDemo
go
/*==============================================================*/
/* Table: DepartDemo */
/*==============================================================*/
create table DepartDemo (
PKID int identity(1,1),
DName nvarchar(200) null,
DCode nvarchar(500) null,
Manager nvarchar(50) null,
ParentID int null default 0,
AddUser nvarchar(50) null,
AddTime datetime null,
ModUser nvarchar(50) null,
ModTime datetime null,
CurState smallint not null default 0,
Remark nvarchar(500) null,
F1 int not null default 0,
F2 nvarchar(300) null,
constraint PK_DEPARTDEMO primary key (PKID)
)
go
truncate table DepartDemo
go
/***************創建54 條測試數據*********************
****************downmoo 3w@live.cn ***************/
declare @d datetime
set @d=getdate()
declare @i int
set @i=1
while @i<=54
begin
--插入一條測試數據
insert into DepartDemo
select '國家統計局房產審計'+Cast(@i as Nvarchar(10))+'科','0','胡不歸',0,'DemoUser',getdate(),
'','1900-01-01',1,'專業評估全國房價,為老百姓謀福祉',0,''
set @i=@i+1
end
go
--***********分頁存儲過程用于SQL server2005/2008、2008R2****************************
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create PROCEDURE [dbo].[ZJF_CPP_GetPagedRecordFor2005_2008]
( @Table varchar(1000), --表名,多表是請使用 tA a inner join tB b On a.AID = b.AID
@TIndex nvarchar(100), --主鍵,可以帶表頭 a.AID
@Column nvarchar(2000) = '*',--讀取字段
@Sql nvarchar(3000) = '',--Where條件
@PageIndex int = 1, --開始頁碼
@PageSize int = 10, --頁大小
@Sort nvarchar(200) = '' --排序字段
)
AS
IF @Sql IS NOT NULL AND len(ltrim(rtrim(@Sql)))>0
BEGIN
SET @strWhere = ' WHERE ' + @Sql + ' '
END
ELSE
BEGIN
SET @strWhere = ''
END
if (charindex(ltrim(rtrim(@TIndex)),@Sort)=0)
begin
if(@Sort='')
set @Sort = @TIndex + ' DESC '
else
set @Sort = @Sort+ ' , '+@TIndex + ' DESC '
end
IF @PageIndex < 1
SET @PageIndex = 1
if @PageIndex = 1 --第一頁提高性能
begin
set @strsql = 'select top ' + str(@PageSize) +' '+@Column
+ ' from ' + @Table + ' ' + @strWhere + ' ORDER BY '+ @Sort
end
else
begin
/* Execute dynamic query */
DECLARE @START_ID nvarchar(50)
DECLARE @END_ID nvarchar(50)
SET @START_ID = convert(nvarchar(50),(@PageIndex - 1) * @PageSize + 1)
SET @END_ID = convert(nvarchar(50),@PageIndex * @PageSize)
set @strsql = ' SELECT '+@Column+ '
FROM (SELECT ROW_NUMBER() OVER(ORDER BY '+@Sort+') AS rownum,
'+@Column+ '
FROM '+@Table +' WITH(NOLOCK) ' + @strWhere +') AS D
WHERE rownum BETWEEN '+@START_ID+' AND ' +@END_ID +' ORDER BY '+@Sort
END
EXEC(@strsql)
print @strsql
set @strsql = 'SELECT Count(1) as TotalRecords FROM ' + @Table +' WITH(NOLOCK) ' + @strWhere
print @strsql
EXEC(@strsql)
| 作者: XuGang 網名:鋼鋼 |
| 出處: http://xugang.cnblogs.com |
| 聲明: 本文版權歸作者和博客園共有。轉載時必須保留此段聲明,且在文章頁面明顯位置給出原文連接地址! |

浙公網安備 33010602011771號