数据分页和使用存储过程的数据分页

--使用存储过程的数据分页 
--pageSize  一页有多少条 
--pageIndex  第几页 
--totalCount 总共有多少条 

--分页的第一种方法 
select top(10) * from Ams_Area where ar_id not in 
( 
    Select top(0) ar_id from Ams_Area order by ar_id 
) 
order by ar_id 

--分页的第二种方法 
select * from 
( 
    select *,ROW_NUMBER() over(order by ar_id)as num from Ams_Area 
)as s 
where s.num between 0 and 10 

--使用存储过程的分页方法 
create proc GetPageDataBuilding 
@pageSize int,--一页有多少条数据 
@pageIndex int,--第几页 
@totalCount int output --总共有多少条 
as 
begin 
declare @str nvarchar(1000); 
set @str = ('select top('+CAST(@pageSize as nvarchar(32))+') * from Ams_Area where ar_id not in(Select top('+CAST(((@pageIndex-1)*@pageSize) as nvarchar(32))+') ar_id from Ams_Area order by ar_id) order by ar_id') 
print @str 
exec (@str) 
select @totalCount=Count(1) from Ams_Area 
end 

--第二个版本的分页 
create proc GetPageDataBuilding 
@pageSize int,--一页内有多少条数据 
@pageIndex int,--第几页 
@totalCount int output --共有多少条数据 
as 
begin 
declare @str nvarchar(1000) 
set @str = ('select * from 
( 
    select *,ROW_NUMBER() over(order by ar_id)as num from Ams_Area 
)as s 
where s.num between '+CAST((@pageIndex-1)*@pageSize as nvarchar(32))+' and '+CAST(@pageSize*@pageIndex as nvarchar(32))+'') 
print @str 
exec(@str) 
select @totalCount=COUNT(1) from Ams_Area 
end 

--执行上面分页的存储过程 
declare @Count int 
exec GetPageDataBuilding 10,2,@Count output 
print @Count 

--删除存储过程 
drop proc GetPageDataBuilding

使用到的数据库地址:点击下载  

下载的文件包括:

 image

博客园版权 

作者:郝喜路
出处:http://www.cnblogs.com/haoxilu/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
关注互联网信息::新浪微博 腾讯微博

原文地址:https://www.cnblogs.com/haoxilu/p/3556828.html