创建分页存储过程

本文介绍了一种使用SQL存储过程实现分页查询的方法。通过动态SQL的方式,根据指定的每页最大记录数、当前页码及已显示记录数来获取相应页面的数据。此方法适用于需要分页展示大量数据的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 创建分页存储过程
use pubs

go
if exists(select name from sysobjects where name='Title_pubs' and type='p')
drop procedure Title_pubs

go


create procedure
Title_pubs
 @pagemax int=5,--每页返回的记录数

 @pagesize int=1,--当前要求要显示的页的页码
 @cells int  --已经显示过的记录数


as
declare @sqlstr varchar(200)

if @pagesize<>1


 begin  
  
set @sqlstr='select top '+ convert(varchar,@pagemax)+' * from titles
where title_id not in(select top '+convert(varchar,@cells)+' title_id from titles order by title_id asc)order by title_id asc '
  print @sqlstr
  execute(@sqlstr)
  --select top @pagemax * from titles where title_id not in(select top @cells title_id from titles order by title_id asc) order by title_id asc
 end
else
 begin
  set @sqlstr='select top '+ convert(varchar,@pagemax)+' * from titles'
  --select top @pagemax * from titles
  execute(@sqlstr)
 end
 

execute Title_pubs @pagemax=5,@pagesize=4,@cells=15 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值