• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

mysql 搞代码 4年前 (2022-01-09) 20次浏览 已收录 0个评论

用存储过程实现数据库的分页代码,加快页面执行速度。具体的大家可以测试下。

代码如下:
–*******************************************************
–* 分页存储过程 *
–* 撒哈拉大森林 *
–* 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 wher本文来源gaodai$ma#com搞$$代**码网$e 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


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:分页存储过程(用存储过程实现数据库的分页代码)
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址