--分页存储过程
create proc dbo.UP_GetRecordByPage
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 主键字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@OrderName varchar(255), -- 排序字段名
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1000) = '', -- 查询条件 (注意: 不要加 where)
@RowCount int output -- 返回当前表总记录数(count)
AS
begin
declare @strSQL varchar(8000) -- 主语句
declare @strTmp varchar(8000) -- 临时变量
declare @strOrder varchar(8000) -- 排序类型
if @strWhere = ''
set @strWhere = '1=1'
--取总数
declare @sql NVARCHAR(400)
set @sql = N'select @RowCount=count(*) from '+@tblName+' where '+ @strWhere
exec sp_executesql @sql,N'@RowCount int OUT',@RowCount OUT
if @OrderType != 0
set @strOrder = ' order by [' + @OrderName +'] desc'
else
set @strOrder = ' order by [' + @OrderName +']'
if @PageIndex = 1
set @strSQL = 'select top ' + str(@PageSize) + ' * from [' + @tblName + '] where '+ @strWhere + @strOrder
else
begin
set @strTmp = ' and [' + @fldName + '] not in(select top ' + str((@PageIndex-1)*@PageSize) + ' [' + @fldName + '] from [' + @tblName + '] where ' + @strWhere + @strOrder + ')'
set @strSQL = 'select top ' + str(@PageSize) + ' * from [' + @tblName + '] where ' + @strWhere + @strTmp + @strOrder
end
exec (@strSQL)
end
create proc dbo.UP_GetRecordByPage
@tblName varchar(255), -- 表名
@fldName varchar(255), -- 主键字段名
@PageSize int = 10, -- 页尺寸
@PageIndex int = 1, -- 页码
@OrderName varchar(255), -- 排序字段名
@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1000) = '', -- 查询条件 (注意: 不要加 where)
@RowCount int output -- 返回当前表总记录数(count)
AS
begin
declare @strSQL varchar(8000) -- 主语句
declare @strTmp varchar(8000) -- 临时变量
declare @strOrder varchar(8000) -- 排序类型
if @strWhere = ''
set @strWhere = '1=1'
--取总数
declare @sql NVARCHAR(400)
set @sql = N'select @RowCount=count(*) from '+@tblName+' where '+ @strWhere
exec sp_executesql @sql,N'@RowCount int OUT',@RowCount OUT
if @OrderType != 0
set @strOrder = ' order by [' + @OrderName +'] desc'
else
set @strOrder = ' order by [' + @OrderName +']'
if @PageIndex = 1
set @strSQL = 'select top ' + str(@PageSize) + ' * from [' + @tblName + '] where '+ @strWhere + @strOrder
else
begin
set @strTmp = ' and [' + @fldName + '] not in(select top ' + str((@PageIndex-1)*@PageSize) + ' [' + @fldName + '] from [' + @tblName + '] where ' + @strWhere + @strOrder + ')'
set @strSQL = 'select top ' + str(@PageSize) + ' * from [' + @tblName + '] where ' + @strWhere + @strTmp + @strOrder
end
exec (@strSQL)
end