一段实现分页的存储过程


Declare @PageSize int
set @PageSize=8
Declare @PageIndex int
set @PageIndex=1
--定义输出的参数
Declare @RecoundCount int
Declare @DateEof Bit  --output,--返回数据,是否溢出
Declare @PageCount int  --output--返回数据:总页数

Declare @TempResult varchar(1000)
Declare @OrderCommand varchar(1000)
Set @TempResult = 'Select * From dbo.Info_ProductInfo where Display=1 And Audting=0'


Declare @GetRCountSumStr nvarchar(1500)
Set @GetRCountSumStr = 'Select  @RecoundCount = Count(*) From (' + @TempResult + ') as t'
Exec sp_executesql @GetRCountSumStr,N'@RecoundCount int output',@RecoundCount output

  
  
     
Declare @ResultMod int
     
Set @ResultMod = @RecoundCount % @PageSize
     
If @ResultMod = 0
        
Set @PageCount = @RecoundCount / @PageSize
      
Else
        
Set @PageCount = (@RecoundCount / @PageSize+ 1
     
Set @DateEof = 0
     
If @PageIndex < 1
        
Set @PageIndex = 1

     
if @PageIndex > @PageCount
        
Set @PageIndex = @PageCount

     
Declare @GetProductAuditingList varchar(3000)
     
Declare @indextable table(id int identity(1,1),PId int)
     
     
--'Insert Into @indextable(PId)' + @TempResult
     

     
Set @GetProductAuditingList =('
     Declare @indextable table(id int identity(1,1),SysId int)
     Insert Into @indextable(SysId) Select SysId From (
' + @TempResult + ') as t 
     Select P.*,u.UserName,C.CCnName,T.Id From @indextable as T,dbo.Info_UserInfo As u,Info_CompanyInfo as C,dbo.Info_ProductInfo as p Where Id > 
' + cast((@PageSize * @PageIndex- @PageSize as varchar(10)) + ' And Id <= ' + cast((@PageSize * @PageIndexas varchar(10)) + 
     
'And P.SysId = T.SysId And p.UserId=u.SysId and P.CompanyId=C.SysID Order By T.Id')



     
Exec(@GetProductAuditingList)
  

posted @ 2007-03-22 10:25 ddr888 阅读(380) 评论(6)  编辑 收藏 所属分类: 数据库

  回复  引用    
#1楼 2007-03-22 10:53 | sharelai [未注册用户]
楼主测试过了吗?当上百万时,用临时表会比没用临时表慢几秒!
  回复  引用  查看    
#2楼 [楼主]2007-03-22 11:07 | ddr888      
现在我就用的这个,数据有1000个 速度还不错,上万还没试过。
@sharelai
有什么思路么?
  回复  引用    
#4楼 2007-03-22 15:59 | ronglj [未注册用户]
能不能提供代码阿??

  回复  引用  查看    
#5楼 [楼主]2007-03-22 16:05 | ddr888      
@ronglj
自己看看书吧
  回复  引用  查看    
#6楼 [楼主]2007-03-22 16:06 | ddr888      
@lemongtree
:-) 学习中

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      


相关链接: