程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> 其他數據庫知識 >> 更多數據庫知識 >> 分頁存儲過程(用存儲過程實現數據庫的分頁代碼)

分頁存儲過程(用存儲過程實現數據庫的分頁代碼)

編輯:更多數據庫知識

復制代碼 代碼如下:
--*******************************************************
--* 分頁存儲過程 *
--* 撒哈拉大森林 *
--* 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頁
@PageSize int --每頁行數
as
set nocount on
declare @P1 int, --P1是游標的id
@rowcount int
exec sp_cursoropen @P1 output,@SqlStr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount output
select ceiling(1.0*@rowcount/@PageSize) as 總頁數--,@rowcount as 總行數,@CurrentPage as 當前頁
set @CurrentPage=(@CurrentPage-1)*@PageSize+1
exec sp_cursorfetch @P1,16,@CurrentPage,@PageSize
exec sp_cursorclose @P1
set nocount off
go


----創建測試表
--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
--)
--
----創建測試數據
--declare @i int
--set @i = 100000
--while @i>0
-- begin
-- insert into Test_Students values('姓名')
-- set @i = @i - 1
-- end
--
----執行存儲過程
--exec P_Paging 'select * from Test_Students order by id',100,100 --執行
--
----刪除測試表
--if exists(select * from sysobjects where type='U' and name=N'Test_Students')
-- drop table Test_Students
--go

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved