分页存储过程-- 分页存储过程
-- 分页存储过程
ALTER proc [dbo].[p_pageShow](
@pageSize int, --每页大小
@currentPage int out, --当前页
@houseName nvarchar(50), --房产名称
@totalCount int out, --总记录数
@totalPage int out --总页数
)
as
begin
--纠正当前页<1的情况
if @currentPage < 1
set @currentPage = 1
--查出总记录数
select @totalCount = count(*) from House where HouseName like '%' + @houseName + '%'
--查出总页数
if @totalCount % @pageSize = 0
set @totalPage = @totalCount / @pageSize
else
set @totalPage = @totalCount / @pageSize + 1
--纠正当前页 > 总页数的情况
if @currentPage > @totalPage
set @currentPage = @totalPage
--分页查询
select * from
(select h.HouseId, h.HouseName, h.HouseType, h.Leixing, h.Floor, h.TotalFloor, h.Rent, h.Addr, a.AreaName,
ROW_NUMBER() over (order by houseId) rn from house h left join area a on h.AreaId = a.AreaId
where h.HouseName like '%' + @houseName + '%' ) t1
where rn between (@currentPage - 1) * @pageSize + 1 and @currentPage * @pageSize
end