这句SQL怎么写?

ItOldHorse 2003-12-03 08:45:08
有一个数据表


bianhao

1
2
3
4
5
6
7
8
9
10
11
12
13

...


如何用SQL语句实现从这表的第M条记录开始连续显示N条记录


如我想从第2条记录开始连续显示3条记录即是

2
3
4


如我想从第5条记录开始连续显示3条记录即是

5
6
7
...全文
116 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
dlpseeyou 2003-12-03
  • 打赏
  • 举报
回复
来迟了
伍子V5 2003-12-03
  • 打赏
  • 举报
回复
贴一个分页显示的存储过程

/*
用存储过程实现的分页程序
*/
CREATE procedure Department_pagination
@SelectStr nvarchar(1000),
@ColumnStr nvarchar (1000),
@OrderStr nvarchar (1000),
@CurrentPage int,
@PageCount int
as
declare @TimeName nvarchar(25)
declare @TableStr nvarchar(1000)

select @TimeName = convert(nvarchar(23), getdate(), 121)
set @TimeName = REPLACE(@TimeName, '.', '')
set @TimeName = REPLACE(@TimeName, ':', '')
set @TimeName = REPLACE(@TimeName, '-', '')
set @TimeName = REPLACE(@TimeName, ' ', '')

select @TableStr='create table ##Tab' + @TimeName + '(wb int identity,'
exec(@TableStr+@ColumnStr+')')
exec('insert into ##Tab' + @TimeName + ' ' + @SelectStr + ' order by ' + @OrderStr)
exec('select * from ##Tab' + @TimeName + ' where wb between ((' + @CurrentPage + '-1)*' + @PageCount + '+1) and ' + @CurrentPage + '*' + @PageCount + ' order by wb')
exec('drop table ##Tab' + @TimeName)
GO



参数1:select语句。2:字段列表。3:排序字段。4:当前页。5每页数目
zjcxc 元老 2003-12-03
  • 打赏
  • 举报
回复
解释一下上面的:

--从第6条记录,连续取3条
select top 3 * from 数据表 where bianhao not in(select top 5 bianhao from 数据表)

第一个top 3中,3是指连续要取的记录条数
第二个top 5中,5是指从第5+1条记录开始数
zjcxc 元老 2003-12-03
  • 打赏
  • 举报
回复
select top 3 * from 数据表 where bianhao not in(select top 5 bianhao from 数据表)
pengdali 2003-12-03
  • 打赏
  • 举报
回复
如我想从第2条记录开始连续显示3条记录即是

2
3
4
select top 3 * from 表 where bianhao not in (select top 1 bianhao from 表)

如我想从第5条记录开始连续显示3条记录即是

5
6
7

select top 3 * from 表 where bianhao not in (select top 4 bianhao from 表)

34,874

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧