原文地址:https://blog.csdn.net/marko_zheng/article/details/87279263
方法1 :
top 和order by 实现,当数据表庞大的时候开小会很大
--查询@m 到@n 条数据
declare @m int;
declare @n int;
--- select top @m * from (select top @n * from stu order by id desc) as a -- 语法错误
select top (@m) * from (select top (@n) * from stu order by id desc) as a
方法2:
利用top 和唯一主键
--查询第3条 到 第8条
declare @m int =3;
declare @n int = 8;
select top (@n-@m+1) * from stu where id not in (
select top (@m-1) id from stu
忽略前 m 条数据,从第 (m+1)条数据开始(包括m+1条在内) 往后取n条数据
declare @m int =3;
declare @n int = 5;
select top (@n) * from stu where id not in(
select top (@m) id from stu
)