create  proc PagingForAll
 @TableName nvarchar(50), ---表
 @Columns nvarchar(500), ---查询的列
 @Where nvarchar(500)='', ---查询条件
 @IdentityColumn nvarchar(50), ---列,增长列,一般为主键
 @OrderBy nvarchar(50)='', --通过哪列排序
 @PageIndex int,  ----第N页
 @PageSize int =50 ----每页多少行
as
 set nocount on
 declare @sql nvarchar(1000) 
 if  len(@OrderBy)=0
   set @OrderBy =@IdentityColumn
 set @sql='select top '+convert(nvarchar(10), @PageSize)+' '+@Columns+'  from '+@TableName+' where 1=1 '+@Where+' and '+@IdentityColumn+'>
     ( select isnull(max('+@IdentityColumn+'),0) from
      ( select top '+convert(nvarchar(10), @PageSize*(@PageIndex-1))+' '+@IdentityColumn+' from ['+@TableName+'] 
      where 1=1  '+@Where+' order by '+@OrderBy+' ) t
      )
   '
 exec(@sql)
 set nocount off
go

posted on 2010-03-11 20:33  aparche  阅读(248)  评论(0编辑  收藏  举报