日期:2014-05-18  浏览次数:20406 次

求一个分页的存储过程**********************************************
例如数据库:test有一列
mid    
2
2
4
4
4
5
6
7

现在我想求一分页的存储过程;
传入参数为     @page(页面号),   @pagecount(每个页面记录数)

比如,传入   @page=1     @pagecount=2
得到的是
2
2
4
4
4
传入   @page=2     @pagecount=2
得到的是
5
6
(相同的mid算一组)

不知道我说清除了没有

------解决方案--------------------
---不好意思,上面的是有点错误,用这个吧。
create table tb(mid int)
insert tb
select 2
union all select 2
union all select 4
union all select 4
union all select 4
union all select 5
union all select 6
union all select 7

go


create proc p @page int,@pagecount int
as
declare @str varchar(1000)
set @page=@page-1
set @str= 'select * from tb where mid in (select top '+rtrim(@pagecount)+ ' mid from tb where mid not in (select top '+rtrim(@page*@pagecount)+ 'mid from tb group by mid) group by mid) '
exec(@str)

go

exec p 1,2 --显示第一页

/* 结果
mid
-----------
2
2
4
4
4

(5 row(s) affected)
*/


exec p 2,3 --显示第二页


/* 结果
mid
-----------
5
6

(2 row(s) affected)

*/


drop proc p
drop table tb
------解决方案--------------------
@pageIndex 请求页码
@pageSize 每页记录数

CREATE proc pt_GetRechargeLog
(
@pageIndex int,
@pageSize int
)
as
declare @sql nvarchar(1024)
select @pageIndex = (@pageIndex-1) * @pageSize
select @sql = N 'select top '+ str(@pageSize) + ' * from [RechargeLog] where [ReChargeID] not in (select top ' + str(@pageIndex) + ' [ReChargeID] from [RechargeLog] order by [ReChargeTime] desc) order by [ReChargeTime] desc '
exec sp_executesql @sql

GO