假设表名为table1
select * from (select top 3 u1.id,u1.name from table1 u1
where u1.name < (select t1.name from table1 t1 where t1.id = 3 )
order by u1.name desc) v1
union
select * from ( select top 3 u2.id ,u2.name from table1 u2
where u2.name > (select t1.name from table1 t1 where t1.id = 3 )
order by u2.name ) v2
union
select * from table1 where id = 3
DECLARE T_cursor CURSOR FOR
SELECT corpID,Corpname FROM corp
where corpid > 7-4
OPEN T_cursor
FETCH FROM T_cursor
into @T_corpID,@T_corpname
-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
-- This is executed as long as the previous fetch succeeds.
print convert(char,@T_corpID)+' '+ @T_corpname
fetch next from T_cursor
into @T_corpID,@T_corpname
建议用临时表,其中临时表加一自动加1的int 字段recordID
create table #temptabel (recordID int identity (1, 1),ID int ,name char(10))
insert into #temptable
select a.ID,a.name from yourtable order by name
然后
select ID,name from #temptable a ,(select recordID from #temptable where name=@inputname) b
where a.recordID between b.recordID-3 and b.recordID+3
//求出编号
select count(*) into @kkk from tablename a,(select name as iname from tablename whereid=@id) b where a.name<=b.iname
//去值
select c.id,c.name from (select (count(*) from tablename a where a.name<b.name) as iid,
b.id,b.name from tablename b where tablename) c where
c.iid>=@kkk-3 and c.iid<@kkk+3
select * from tablename where id=@input
union
select top 1 tablename.id,tablename.name from tablename,(select name from tablename where id=@input) as b where
a.name<b.name
union
select top 1 tablename.id,tablename.name from tablename,(select name from tablename where id=@input) as b where
a.name<b.name order by tablename.name ASC