分页存储过程(用存储过程实现数据库的分页代码)

2022-05-24 0 1,023

复制代码 代码如下:

–*******************************************************

–* 分页存储过程 *

–* 撒哈拉大森林 *

–* 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、本网站所有发布的源码、软件和资料均为收集各大资源网站整理而来;仅限用于学习和研究目的,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。 不得使用于非法商业用途,不得违反国家法律。否则后果自负!

2、本站信息来自网络,版权争议与本站无关。一切关于该资源商业行为与www.niceym.com无关。
如果您喜欢该程序,请支持正版源码、软件,购买注册,得到更好的正版服务。
如有侵犯你版权的,请邮件与我们联系处理(邮箱:skknet@qq.com),本站将立即改正。

NICE源码网 MsSql 分页存储过程(用存储过程实现数据库的分页代码) https://www.niceym.com/60089.html