select top 11 * from A where id < = @id union all select top 10 * from A where id > @id
不合适的 返回学号等于50的前10和后10条记录 包含等于50的 select * from A where 学号 =50
select top 10 * from A desc union all select top 10 * from A asc
DECLARE @ID SET @ID=11 SELECT TOP 10 ID FROM TB WHERE ID<@ID ORDER BY ID DESC UNION ALL SELECT TOP 10 ID FROM TB WHERE ID>@ID ORDER BY ID ASC
create procedure proc_select @id int as select top 10 ID from tb1 where id <@id order by id desc select top 10 ID from tb1 where id >@id order by id asc go--测试数据 exec proc_select 11
create procedure proc_select @id int as print '前10条记录' select top 10 ID from tb1 where id <=@id order by id desc print '后10条记录' select top 10 ID from tb1 where id >=@id order by id asc goexec proc_select 11 开始没有包括本条记录
create table a(id int,age int) insert into a select '1','12' union all select '2','11' union all select '3','10' union all select '4','15' union all select '5','13' union all select '6','10' union all select '7','14' union all select '8','13' union all select '9','15' union all select '10','11'select top 6 * from (select top 3 * from a union all select top 3 * from (select top 10 * from a order by id desc) b) c order by id 和这个差不多了!
select top 11 * from A where id < = @id
union all
select top 10 * from A where id > @id
返回学号等于50的前10和后10条记录 包含等于50的
select * from A where 学号 =50
select top 10 * from A desc union all
select top 10 * from A asc
SET @ID=11
SELECT TOP 10 ID FROM TB WHERE ID<@ID ORDER BY ID DESC
UNION ALL
SELECT TOP 10 ID FROM TB WHERE ID>@ID ORDER BY ID ASC
@id int
as
select top 10 ID from tb1 where id <@id order by id desc
select top 10 ID from tb1 where id >@id order by id asc
go--测试数据
exec proc_select 11
@id int
as
print '前10条记录'
select top 10 ID from tb1 where id <=@id order by id desc
print '后10条记录'
select top 10 ID from tb1 where id >=@id order by id asc
goexec proc_select 11
开始没有包括本条记录
insert into a
select '1','12' union all
select '2','11' union all
select '3','10' union all
select '4','15' union all
select '5','13' union all
select '6','10' union all
select '7','14' union all
select '8','13' union all
select '9','15' union all
select '10','11'select top 6 * from (select top 3 * from a
union all
select top 3 * from (select top 10 * from a order by id desc) b) c order by id 和这个差不多了!