加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

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

发布时间:2020-12-12 07:56:57 所属栏目:MsSql教程 来源:网络整理
导读:感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧! 代码如下: --******************************************************* --* 分页存储过程 * --* 撒哈拉大森林 * --* 2010-6-28 * --************************************************

感兴趣的小伙伴,下面一起跟随编程之家 52php.cn的小编两巴掌来看看吧!

代码如下:


--******************************************************* 
--* 分页存储过程 * 
--* 撒哈拉大森林 * 
--* 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读