如何在数据库中分页显示数据?

IT9426 2012-02-25 04:19:44
要求:只在一个表查询,只能用top关键字
...全文
147 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sdl2005lyx 2012-02-27
  • 打赏
  • 举报
回复
分页的存储过程:

create proc PageNum
@PageSize int, --每页显示条数
@PageIndex int, --当前是第几页
@totalRows int output, --总行数
@totalPages int output --总页数
as
declare @startId int
declare @endId int
set @startId = @PageSize*(@PageIndex-1)+1
set @endId = @startId+@PageSize-1
select @totalRows=COUNT(*) from Books
set @totalPages=@totalRows/@PageSize
if(@totalRows%@PageSize != 0) --如果总行数除每页显示数据量的值不等于0,则总页数得加1
begin
set @totalPages=@totalPages+1
end
declare @IndexTable table(Id int identity(1,1) ,nId int)
insert into @IndexTable select Id from Books
select * from @IndexTable as t ,Books as b where t.Id>=@startId and t.Id<=@endId and t.nId=b.Id
go

declare @tr int --总行数
declare @tp int --总页数
exec PageNum 4,2,@tr output ,@tp output
print '这是总页数'+convert(varchar,@tp) --数据转换
print @tr
村长_乐 2012-02-25
  • 打赏
  • 举报
回复
1楼、2楼,周末了还这么积极,你让我们情何以堪
muyi66 2012-02-25
  • 打赏
  • 举报
回复
这里貌似是C#区。。。。。。去顶上看了看,我没记错,的确是C#区。

你干嘛在这里发别的区的问题呢?不相信那些区的人?
dalmeeme 2012-02-25
  • 打赏
  • 举报
回复
select top 20 * from A where id not in(select top 0 id from A order by id) order by id
每页20条,第一页。

select top 20 * from A where id not in(select top 20 id from A order by id) order by id
每页20条,第二页。

。。。。。。以此类推。
孟子E章 2012-02-25
  • 打赏
  • 举报
回复
参见
http://blog.csdn.net/wallimn/article/details/1778703

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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