SQL SERVER存储过程实现分页

Create PROCEDURE [dbo].[sp_PageList]--创建存储过程
(
@StationID nvarchar(32),--工位ID
@PageSize INT, --每页显示的项数
@PageIndex INT--当前页数
)
AS
BEGIN

--获取总行数,用count(*)
declare @Sqlselect nvarchar(max) 
declare @RecordCount INT 
set @Sqlselect='select @RowCount=count(*) from Station_'+@StationID
PRINT @Sqlselect
exec sys.sp_executesql @Sqlselect,N'@RowCount INT output',@RecordCount output;--将总页数赋给@RecordCount output
--查询总页数(CEILING()向上舍入)
DECLARE @PageCount int --声明
set @PageCount=CEILING(CONVERT(float,@RecordCount/@PageSize))--获取页数

--判断当查询的页数小于1时则查询首页,大于最大值则为最后一页
if(@PageIndex<1)
begin
 set @PageIndex=1;
end
if(@PageIndex>@PageCount)
 begin
 set @PageIndex=@PageCount;
 end

--查询分页数据(CONVERT),防止序号出现断层

 set @Sqlselect= 'select * from(select ROW_NUMBER() over(order by [Id]) as rowindex ,* From  Station_'+ @StationID+') a where a.rowindex>'+convert(nvarchar(32),(@PageSize*(@PageIndex-1)))+' and a.rowindex<='+CONVERT(nvarchar(32),(@PageSize*@PageIndex))+''
exec sp_executesql @Sqlselect
Print @Sqlselect
END
GO

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值