set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
CREATE PROCEDURE [dbo].[sp_GetPager]
@TableName varchar(50), --表名
@Fields varchar(5000) = '*', --字段名(全部字段为*)
@OrderField varchar(5000), --排序字段(必须!支持多字段)
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@sqlWhere varchar(5000) = Null, --条件语句(不用加where)
@PageIndex int = 1 , --指定当前为第几页
@PageSize int = 10, --每页多少条记录
@TotalRecord int output,
@TotalPage int output --返回总页数
AS
BEGIN
Begin Tran --开始事务
Declare @sql nvarchar(4000);
if @TotalRecord<=0 begin
--计算总记录数
if (@SqlWhere='' or @sqlWhere=NULL)
set @sql = 'select @TotalRecord = count(*) from ' + @TableName
else
set @sql = 'select @TotalRecord = count(*) from ' + @TableName + ' with(nolock) where ' + @sqlWhere
EXEC sp_executesql @sql,N'@TotalRecord int OUTPUT',@TotalRecord OUTPUT--计算总记录数
end
--计算总页数
select @TotalPage=CEILING((@TotalRecord+0.0)/@PageSize)
if (@SqlWhere='' or @sqlWhere=NULL)
if @OrderType != 0
set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ' DESC) as RowID,' + @Fields + ' from ' + @TableName
else
set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ' ASC) as RowID,' + @Fields + ' from ' + @TableName
else
if @OrderType != 0
set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ' DESC) as RowID,' + @Fields + ' from ' + @TableName + ' with(nolock) where ' + @SqlWhere
else
set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ' ASC) as RowID,' + @Fields + ' from ' + @TableName + ' with(nolock) where ' + @SqlWhere
--处理页数超出范围情况
if @PageIndex<=0
Set @PageIndex = 1
if @PageIndex>@TotalPage
Set @PageIndex = @TotalPage
--处理开始点和结束点
Declare @StartRecord int
Declare @EndRecord int
set @StartRecord = (@PageIndex-1)*@PageSize + 1
set @EndRecord = @StartRecord + @PageSize - 1
--继续合成sql语句
set @Sql = @Sql + ') as t where RowID between ' + Convert(varchar(50),@StartRecord) + ' and ' + Convert(varchar(50),@EndRecord)
print @sql
Exec(@Sql)
---------------------------------------------------
If @@Error <> 0
Begin
RollBack Tran
Return -1
End
Else
Begin
Commit Tran
Return @TotalRecord ---返回记录总数
End
END
set QUOTED_IDENTIFIER ON
go
CREATE PROCEDURE [dbo].[sp_GetPager]
@TableName varchar(50), --表名
@Fields varchar(5000) = '*', --字段名(全部字段为*)
@OrderField varchar(5000), --排序字段(必须!支持多字段)
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@sqlWhere varchar(5000) = Null, --条件语句(不用加where)
@PageIndex int = 1 , --指定当前为第几页
@PageSize int = 10, --每页多少条记录
@TotalRecord int output,
@TotalPage int output --返回总页数
AS
BEGIN
Begin Tran --开始事务
Declare @sql nvarchar(4000);
if @TotalRecord<=0 begin
--计算总记录数
if (@SqlWhere='' or @sqlWhere=NULL)
set @sql = 'select @TotalRecord = count(*) from ' + @TableName
else
set @sql = 'select @TotalRecord = count(*) from ' + @TableName + ' with(nolock) where ' + @sqlWhere
EXEC sp_executesql @sql,N'@TotalRecord int OUTPUT',@TotalRecord OUTPUT--计算总记录数
end
--计算总页数
select @TotalPage=CEILING((@TotalRecord+0.0)/@PageSize)
if (@SqlWhere='' or @sqlWhere=NULL)
if @OrderType != 0
set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ' DESC) as RowID,' + @Fields + ' from ' + @TableName
else
set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ' ASC) as RowID,' + @Fields + ' from ' + @TableName
else
if @OrderType != 0
set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ' DESC) as RowID,' + @Fields + ' from ' + @TableName + ' with(nolock) where ' + @SqlWhere
else
set @sql = 'Select * FROM (select ROW_NUMBER() Over(order by ' + @OrderField + ' ASC) as RowID,' + @Fields + ' from ' + @TableName + ' with(nolock) where ' + @SqlWhere
--处理页数超出范围情况
if @PageIndex<=0
Set @PageIndex = 1
if @PageIndex>@TotalPage
Set @PageIndex = @TotalPage
--处理开始点和结束点
Declare @StartRecord int
Declare @EndRecord int
set @StartRecord = (@PageIndex-1)*@PageSize + 1
set @EndRecord = @StartRecord + @PageSize - 1
--继续合成sql语句
set @Sql = @Sql + ') as t where RowID between ' + Convert(varchar(50),@StartRecord) + ' and ' + Convert(varchar(50),@EndRecord)
print @sql
Exec(@Sql)
---------------------------------------------------
If @@Error <> 0
Begin
RollBack Tran
Return -1
End
Else
Begin
Commit Tran
Return @TotalRecord ---返回记录总数
End
END