一个简单的分页存储过程

create proc Pager
@PageIndex int,
@PageSize int,
@PageCount int out,
@RecordCount int out
as
select @RecordCount= count(*) from film_type
set @PageCount = CEILING(@RecordCount * 1.0 / @PageSize)
declare @topCount int
SET @topCount = @RecordCount - @PageSize * @PageIndex
DECLARE @SQLSTR NVARCHAR(1000)
if @PageIndex=0 or @RecordCount <=0
begin
    set @SQLSTR=N'select top '+str(@PageSize) +' film_id,film_name from film_type order by film_id desc'
end
else
begin
   if @PageIndex=@PageCount-1
   begin
      set @SQLSTR=N'select * from (select top '+str(@topCount)+' film_id,film_name from film_type order by film_id asc)T order by film_id desc'
   end
   else
   begin
     set @SQLSTR=N'select top '+str(@PageSize)+' * from (select top '+str(@topCount)+' film_id,film_name from film_type order by film_id asc)T order by film_id desc '
   end
end
print @SQLSTR
EXEC (@SQLSTR)

drop proc Pager

declare @pageCount int
declare @recordCount int
exec Pager 0,5,@pageCount out,@recordCount out

本文使用Blog_Backup未注册版本导出,请到soft.pt42.com注册。

原文地址:https://www.cnblogs.com/zjypp/p/2319496.html