用存儲(chǔ)過程實(shí)現(xiàn)數(shù)據(jù)庫(kù)的分頁(yè)代碼,加快頁(yè)面執(zhí)行速度。具體的大家可以測(cè)試下。
代碼如下:
–*******************************************************
–* 分頁(yè)存儲(chǔ)過程 *
–* 撒哈拉大森林 *
–* 2010-6-28 *
–*******************************************************
if exists(select * from sysobjects where type=’P’ and name=N’P_Paging’)
drop procedure P_Paging
go
create procedure P_Paging
@SqlStr nvarchar(4000), –查詢字符串
@CurrentPage int, –第N頁(yè)
@PageSize int –每頁(yè)行數(shù)
as
set nocount on
declare @P1 int, –P1是游標(biāo)的id
@rowcount int
exec sp_cursoropen @P1 output,@SqlStr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount output
select ceiling(1.0*@rowcount/@PageSize) as 總頁(yè)數(shù)–,@rowcount as 總行數(shù),@CurrentPage as 當(dāng)前頁(yè)
set @CurrentPage=(@CurrentPage-1)*@PageSize+1
exec sp_cursorfetch @P1,16,@CurrentPage,@PageSize
exec sp_cursorclose @P1
set nocount off
go
—-創(chuàng)建測(cè)試表
–if exists(select * from sysobjects where type=’U’ and name=N’Test_Students’)
— drop table Test_Students
–go
–create table Test_Students(
— id int IDENTITY(1,1) not null,
— name nvarchar(100) not null
–)
—
—-創(chuàng)建測(cè)試數(shù)據(jù)
–declare @i int
–set @i = 100000
–while @i>0
— begin
— insert into Test_Students values(‘姓名’)
— set @i = @i – 1
— end
—
—-執(zhí)行存儲(chǔ)過程
–exec P_Paging ‘select * from Test_Students order by id’,100,100 –執(zhí)行
—
—-刪除測(cè)試表
–if exists(select * from sysobjects where type=’U’ and name=N’Test_Students’)
— drop table Test_Students
–go