您的位置:首页 > 其它

一个简单的分页存储过程

2006-12-11 19:38 381 查看
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注册。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: