-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <分页存储过程>
-- =============================================
ALTER PROCEDURE [dbo].[Pro_pagination]
(
@SQL VARCHAR(1000), --不带排序语句的SQL语句或表名
@Page int, --页码
@RecsPerPage int, --每页容纳的记录数
@ID VARCHAR(255), --需要排序的不重复的ID号
@Sort VARCHAR(255), --排序字段及规则
@RowCount int output, --总行数(传出参数)
@PageCount int output --总页数(传出参数)
)
AS
begin
DECLARE @Str VARCHAR(1000)
--执行页数和行数给 @RowCount @PageCount 赋值
select @PageCount=COUNT(*),@RowCount=CEILING((COUNT(*)+0.0)/@RecsPerPage) from ST_news
print @PageCount
--执行主语句
SET @Str='SELECT TOP '+CAST(@RecsPerPage AS VARCHAR(20))+' * FROM
('+@SQL+') T WHERE T.'+@ID+' NOT IN (SELECT TOP '+CAST((@RecsPerPage*(@Page-1))
AS VARCHAR(20))+' '+@ID+' FROM ('+@SQL+') T9 ORDER BY '+@Sort+') ORDER BY '+@Sort
PRINT @Str
EXEC (@Str)
end
--DECLARE @Row int,@Pages int
--exec [Pro_pagination] 'select * from ST_news ',1,3,'ST_n_id','ST_n_id desc',@Row,@Pages
来源:https://www.cnblogs.com/520cc/archive/2012/10/23/2735883.html