分页存储过程-利用select top和中间变量实现分页

来源:互联网 发布:scratch趣味编程100例 编辑:程序博客网 时间:2024/06/06 11:39
create procedure proc_paged_with_Midvar  --利用ID>最大ID值和中间变量    
     
    (    
     
    @pageIndex int,    
     
    @pageSize int   
     
    )    
     
    as   
     
    declare @count int   
     
    declare @ID int
     
    declare @timediff datetime    
     
    declare @sql nvarchar(500)    
     
    begin    
     
    set nocount on;    
     
    select @count=0,@ID=0 ,@timediff=getdate()    
     
    select @count=@count+1,@ID=case when @count<=@pageSize*@pageIndex then menuId else  @ID  end from menus
     
    print @count;
     
    set @sql='select top '+str(@pageSize)+' * from menus where menuId>'+str(@ID)    
     
    execute(@sql)    
     
    select datediff(ms,@timediff,getdate()) as 耗时    
     
    set nocount off;    
     
    end
     
    --调用存储过程

    exec dbo.proc_paged_with_Midvar 1,2
0 0
原创粉丝点击