SqlServer 分页存储过程

WBOY
发布: 2016-06-07 16:21:37
原创
845 人浏览过

SqlServer 分页存储过程 create proc [dbo].[proc_Opinion_BaseInfo] @TableName varchar(4000), @PkField varchar(100), @PageIndex int=1, @PageSize int=10, @SqlWhere nvarchar(4000), @RowCount bigint output, @PageCount bigint output as if(@Sql

   SqlServer 分页存储过程

  create proc [dbo].[proc_Opinion_BaseInfo]

  @TableName varchar(4000),

  @PkField varchar(100),

  @PageIndex int=1,

  @PageSize int=10,

  @SqlWhere nvarchar(4000),

  @RowCount bigint output,

  @PageCount bigint output

  as

  if(@SqlWhere='1')

  set @SqlWhere = '1=1'

  declare @sql nvarchar(4000),,@start int,@end int

  set @sql='select * from (select Row_NUMBER() OVER(order by '+@PkField+' desc) rowId,* from '+@TableName+' where '+@SqlWhere

  set @start = (@PageIndex-1)*@PageSize+1

  set @end = @start+@PageSize-1

  set @sql = @sql + ') t where rowId between '+CAST(@start as varchar(20))+' and ' +CAST(@end as varchar(20))

  exec (@sql)

  set @sql = 'select @RowCount=count(1) from '+@TableName+' where '+@SqlWhere

  exec sp_executesql @sql,N'@RowCount bigint OUTPUT',@RowCount OUTPUT

  if(@RowCount%@PageSize=0)

  begin

  set @PageCount = @RowCount / @PageSize

  end

  else

  begin

  set @PageCount = @RowCount / @PageSize +1

  end

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板