有一个表,我们需要从里面选出M 到 N 的纪录.
思路为:
1.子查询a,从表里选出 1 至N 条纪录 .
2.子查询b,从表里选出1至 M-1条纪录。
3.从b 里选出最大的 id ,设为 maxid.
4.从a 里选出id <maxid 的行
这种方法的实质是利用top关键字截取纪录集的功能。
代码如下:
Code
declare @M int
declare @N int
set @M=3 --纪录集开始index
set @N=5 --纪录集结束index
select top (@N-@M+1) * from
(select top (@N-0) id from table1)
b
where b.id>
(
select max(id) from
(select top (@M-1) id from table1
)c
)
declare @M int
declare @N int
set @M=3 --纪录集开始index
set @N=5 --纪录集结束index
select top (@N-@M+1) * from
(select top (@N-0) id from table1)
b
where b.id>
(
select max(id) from
(select top (@M-1) id from table1
)c
)