declare @search_number int set @search_number = 99 --谿場 螳 99朱.. ;with dummy as ( select 0 seq union all select seq + 1 from dummy where seq + 1 <= 1000 ) select case when sign(seq - @search_number) > 0 then 'next' else 'prev' end as dir, case when sign(seq - @search_number) > 0 then min(seq) else max(seq) end as seq from dummy where seq <> @search_number group by sign(seq - @search_number) order by sign(seq - @search_number) option (maxrecursion 0);