-- 处理页数超出范围情况 if @pageIndex<=0 Set @pageIndex = 1 if @pageIndex>@totalPage Set @pageIndex = @totalPage
select @totalPage=@totalRecord
-- 处理开始点和结束点 Declare @startRecord int Declare @endRecord int set @startRecord = (@pageIndex-1)*@pageSize + 1 set @endRecord = @startRecord + @pageSize - 1
-- 合成sql语句 if (@sqlWhere = '' or @sqlWhere = NULL) set @sql = 'select ' + @tableFields + ',identity(int,1,1) rowId into #t from (select top 100 percent * from ' + @tableName + ') t ' else set @sql = 'select ' + @tableFields + ',identity(int,1,1) rowId into #t from (select top 100 percent * from ' + @tableName + ') t where ' + @sqlWhere
set @Sql = @Sql + '; select * from #t where rowId between ' + Convert(varchar(50),@startRecord) + ' and ' + Convert(varchar(50),@endRecord)
另一种是把 identity(int,1,1)和表的其他数据插入一张临时表,然后再通过临时表进行查询
@tableName varchar(500), -- 表名
@tableFields varchar(5000) = '*', -- 字段名(全部字段为*)
@sqlWhere varchar(5000) = NULL, -- 条件语句(不加 where,可带 group by 分组条件,分组条件需要带 group by 关键字)
@orderFields varchar(5000), -- 排序字段(必须,支持多字段,不加 order by)
@pageSize int, -- 页大小(每页多少条记录)
@pageIndex int = 1 , -- 指定当前为第几页
@totalPage int output -- 返回总页数
AS
BEGIN
Declare @sql nvarchar(4000);
Declare @totalRecord int;
-- 计算总记录数
if (@sqlWhere = '' or @sqlWhere = NULL)
set @sql = 'select @totalRecord = count(0) from ' + @tableName
else
BEGIN
if(CHARINDEX('group by', LOWER(@sqlWhere)) > 0)
set @sql = 'select @totalRecord = count(0) from (select ' + @tableFields + ' from ' + @tableName + ' where ' + @sqlWhere + ') as Tab_GroupTable'
else
set @sql = 'select @totalRecord = count(0) from ' + @tableName + ' where ' + @sqlWhere
END
EXEC sp_executesql @sql,N'@totalRecord int OUTPUT',@totalRecord OUTPUT
-- 计算总页数
select @totalPage=CEILING((@totalRecord+0.0)/@pageSize)
-- 处理页数超出范围情况
if @pageIndex<=0
Set @pageIndex = 1
if @pageIndex>@totalPage
Set @pageIndex = @totalPage
select @totalPage=@totalRecord
-- 处理开始点和结束点
Declare @startRecord int
Declare @endRecord int
set @startRecord = (@pageIndex-1)*@pageSize + 1
set @endRecord = @startRecord + @pageSize - 1
-- 合成sql语句
if (@sqlWhere = '' or @sqlWhere = NULL)
set @sql = 'select ' + @tableFields + ',identity(int,1,1) rowId into #t from ' + @tableName
else
set @sql = 'select ' + @tableFields + ',identity(int,1,1) rowId into #t from ' + @tableName + ' where ' + @sqlWhere
set @Sql = @Sql + '; select * from #t where rowId between ' + Convert(varchar(50),@startRecord) + ' and ' + Convert(varchar(50),@endRecord)
Exec(@Sql)
end
我传入的表 都是有自增ID。
========
谢谢楼上两位的热情回答!
GODECLARE @return_value int,
@totalPage intSELECT @totalPage = 1EXEC @return_value = [waqi_f].[PF_Sys_PageControl]
@tableName = N' t_news',
@tableFields = N' * ',
@sqlWhere = N' ',
@orderFields = N' id desc',
@pageSize = 2,
@pageIndex = 1,
@totalPage = @totalPage OUTPUTSELECT @totalPage as N'@totalPage'SELECT 'Return Value' = @return_valueGO
@tableName varchar(500), -- 表名
@tableFields varchar(5000) = '*', -- 字段名(全部字段为*)
@sqlWhere varchar(5000) = NULL, -- 条件语句(不加 where,可带 group by 分组条件,分组条件需要带 group by 关键字)
@orderFields varchar(5000), -- 排序字段(必须,支持多字段,不加 order by)
@pageSize int, -- 页大小(每页多少条记录)
@pageIndex int = 1 , -- 指定当前为第几页
@totalPage int output -- 返回总页数
AS
BEGIN
Declare @sql nvarchar(4000);
Declare @totalRecord int;
-- 计算总记录数
if (@sqlWhere = '' or @sqlWhere = NULL)
set @sql = 'select @totalRecord = count(0) from ' + @tableName
else
BEGIN
if(CHARINDEX('group by', LOWER(@sqlWhere)) > 0)
set @sql = 'select @totalRecord = count(0) from (select ' + @tableFields + ' from ' + @tableName + ' where ' + @sqlWhere + ') as Tab_GroupTable'
else
set @sql = 'select @totalRecord = count(0) from ' + @tableName + ' where ' + @sqlWhere
END
EXEC sp_executesql @sql,N'@totalRecord int OUTPUT',@totalRecord OUTPUT
-- 计算总页数
select @totalPage=CEILING((@totalRecord+0.0)/@pageSize)
-- 处理页数超出范围情况
if @pageIndex<=0
Set @pageIndex = 1
if @pageIndex>@totalPage
Set @pageIndex = @totalPage
select @totalPage=@totalRecord
-- 处理开始点和结束点
Declare @startRecord int
Declare @endRecord int
set @startRecord = (@pageIndex-1)*@pageSize + 1
set @endRecord = @startRecord + @pageSize - 1
-- 合成sql语句
if (@sqlWhere = '' or @sqlWhere = NULL)
set @sql = 'select ' + @tableFields + ',identity(int,1,1) rowId into #t from (select top 100 percent * from ' + @tableName + ') t '
else
set @sql = 'select ' + @tableFields + ',identity(int,1,1) rowId into #t from (select top 100 percent * from ' + @tableName + ') t where ' + @sqlWhere
set @Sql = @Sql + '; select * from #t where rowId between ' + Convert(varchar(50),@startRecord) + ' and ' + Convert(varchar(50),@endRecord)
Exec(@Sql)
end