一个分页存储过程

来源:http://dotnet.mblogger.cn/zzjjmm/posts/1603.aspx

CREATE PROCEDURE dbo.PostGetPostByPage

 (
  @page int,
  @forumid int,
  @topornot int
 )

AS
 /* SET NOCOUNT ON */
 declare @begin int,@end int,@f int,@l int,@count int,@top int
 
 select @top=count(*) from Posts as p1 where p1.PostID=(select min(PostID) from Posts as p2 where p1.ThreadID=p2.ThreadID)
 and p1.ForumID=@forumid and PostType=4
 
 if @topornot=1
 
 select p1.PostType,p1.Title,p1.UserName,p1.TotalViews,p1.PostID,p1.ThreadID,p1.ForumID,FileName,
 Reply=(select Count(*) from Posts as p2 where p1.ThreadID=p2.ThreadID)-1,
 LastDate=(select Max(PostDate)from Posts as p2 where p1.ThreadID=p2.ThreadID),
 LastWriter=(select UserName from posts as p2 where p2.PostID=(select Max(PostID)from Posts as p2 where p1.ThreadID=p2.ThreadID))
 from Posts as p1 where p1.PostID=(select min(PostID) from Posts as p2 where p1.ThreadID=p2.ThreadID)
 and p1.ForumID=@forumid and PostType=4
 
 else if @topornot=2
 begin
 
 select @count=count(*) from Posts as p1 where p1.PostID=(select min(PostID) from Posts as p2 where p1.ThreadID=p2.ThreadID)
 and p1.ForumID=@forumid and PostType<>4
 
 declare my_cursor SCROLL CURSOR for
 select p1.PostID from Posts as p1 where p1.PostID=(select min(PostID) from Posts as p2 where p1.ThreadID=p2.ThreadID)
 and p1.ForumID=@forumid and PostType<>4 order by (select max(PostID) from Posts as p2 where p1.ThreadID=p2.ThreadID) desc
 open my_cursor
 
 
 
 if @count+@top<25 and @page=1
 begin
 select @f=1
 select @l=@count
 end
 if @count+@top>=25 and @page=1
 begin
 select @f=1
 select @l=25-@top
 end
 
 if(@page*25-@top>@count) and @page>1
 begin
 select @f=(@page-1)*25+1-@top
 select @l=@count
 end
 if(@page*25-@top<=@count) and @page>1
 begin
 select @f=(@page-1)*25+1-@top
 select @l=@page*25-@top
 end
 
 
 fetch absolute @f from my_cursor into @begin
 fetch absolute @l from my_cursor into @end
 set nocount off
  
 select p1.PostType,p1.Title,p1.UserName,p1.TotalViews,p1.PostID,p1.ThreadID,p1.ForumID,FileName,
 Reply=(select Count(*) from Posts as p2 where p1.ThreadID=p2.ThreadID)-1,
 LastDate=(select Max(PostDate)from Posts as p2 where p1.ThreadID=p2.ThreadID),
 LastWriter=(select UserName from posts as p2 where p2.PostID=(select Max(PostID)from Posts as p2 where p1.ThreadID=p2.ThreadID))
 from Posts as p1 where p1.PostID=(select min(PostID) from Posts as p2 where p1.ThreadID=p2.ThreadID)
 and p1.ForumID=@forumid and PostID<=@begin and PostID>=@end and PostType<>4 order by (select max(PostID) from Posts as p2 where p1.ThreadID=p2.ThreadID) desc
 
 close my_cursor
 end
 RETURN @@Rowcount

GO

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值