ITPub博客

首页 > Linux操作系统 > Linux操作系统 > sqlserver存储过程实现多表分页

sqlserver存储过程实现多表分页

原创 Linux操作系统 作者:iSQlServer 时间:2010-10-19 13:36:26 0 删除 编辑

if @PageIndex = 1 

 

begin 

   if @strWhere != ’’   

    set @strSQL = ’select top ’ + str(@PageSize) +’ ’+@strGetFields+ ’ from [’+ @tblName +’] where ’ + @strWhere + ’ ’ + @strOrder 

    else 

    set @strSQL = ’select top ’ + str(@PageSize) +’ ’+@strGetFields+ ’ from [’+ @tblName +’] ’+ @strOrder 

--如果是第一页就执行以上代码,这样会加快执行速度 

end 

else 

begin 

--以下代码赋予了@strSQL以真正执行的SQL代码 

set @strSQL = ’select top ’ + str(@PageSize) +’ ’+@strGetFields+ ’ from [’ + @tblName +’] where [’ + @fldName + ’]’ + @strTmp + ’([’+ @fldName + ’]) 
from (select top ’ + str((@PageIndex-1)*@PageSize) + ’ [’+ @fldName + ’] from [’+ @tblName +’]’ + @strOrder + ’) as tblTmp)’+ @strOrder 

 

if @strWhere != ’’ 

   set @strSQL = ’select top ’ + str(@PageSize) +’ ’+@strGetFields+ ’ from [’+ @tblName +’] where [’ + @fldName + ’]’ + @strTmp + ’([’+ @fldName + ’]) from (select top ’ + str((@PageIndex-1)*@PageSize) + ’ [’+ @fldName + ’] 
from [’+ @tblName +’] where ’ + @strWhere + ’ ’ + @strOrder + ’) as tblTmp) and ’ + @strWhere + ’ ’ + @strOrder 

end 

end   

exec ( @strSQL)
GO

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/16436858/viewspace-676263/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-10-17

  • 博文量
    1319
  • 访问量
    2342759