存储过程 分页

CREATE PROCEDURE page_pro
@pagesize int,
@currentpage int
AS
declare @count int
declare @pagecount int
declare @first_ int
declare @list_ int 
declare @first int
declare @end int
declare @pagenum int
declare @mod int
set nocount on
 select @count=count(*) from 参数分类结构
   select @mod = @count%@pagesize
   if @mod=0
   begin
   select @pagenum = @count / @pagesize
   if @currentpage>@pagenum
   begin
   select @currentpage=@pagenum
   end
   if @currentpage<1
   begin
   select @currentpage=1
   end
   select @first = (@currentpage-1)*@pagesize+1
  if @currentpage=@pagenum
       begin
       select @end = @count 
       end
       else
       begin
        select @end = @first+@pagesize-1
       end
   end
   else
   begin
   select @pagenum=@count/@pagesize+1
    if @currentpage>@pagenum
   begin
   select @currentpage=@pagenum
   end
   if @currentpage<1
   begin
   select @currentpage=1
   end
    select @first = (@currentpage-1)*@pagesize+1
   if @currentpage=@pagenum
       begin
       select @end = @count 
       end
       else
       begin
        select @end = @first+@pagesize-1
       end
  end
   set @pagecount = @pagenum
   --设置游标--
   declare authors_cursor scroll CURSOR FOR select id from 参数分类结构 order by id
   open authors_cursor
   fetch absolute @first from authors_cursor into @first_
   fetch absolute @end from authors_cursor into @list_
   close authors_cursor
   deallocate authors_cursor
   if exists(select * from page_history where leixing = '参数分类结构')
   begin
   update page_history set rowscount =@count,pagecount=@pagenum,fist_=@first,end_=@end,current_=@currentpage where leixing = '参数分类结构' 
   end
   else
   begin
   insert into page_history (rowscount,pagecount,fist_,end_,current_,leixing) values(@count,@pagenum,@first,@end,@currentpage,'参数分类结构')
   end
   set nocount off
 select * from 参数分类结构 where id>=@first_ and id< =@list_
   return(@@rowcount)
 
GO

你可能感兴趣的:(存储过程)