求一个分页的存储过程**********************************************

god_sun 2007-09-11 03:35:14
例如数据库: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算一组)

不知道我说清除了没有
...全文
111 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
bino 2007-09-11
  • 打赏
  • 举报
回复
@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
leo_lesley 2007-09-11
  • 打赏
  • 举报
回复
---不好意思,上面的是有点错误,用这个吧。
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
god_sun 2007-09-11
  • 打赏
  • 举报
回复
有错误
我传入1,3
怎么还是显示
2
2
4
4
4
应该是
2
2
4
4
4
5
ls写死了吧
leo_lesley 2007-09-11
  • 打赏
  • 举报
回复
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 2 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,2 --显示第二页


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

(2 row(s) affected)

*/


drop proc p
drop table tb

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧