You can select the smallest id by the value of @i (if any) and the smallest id, and then get the largest:
select max(id)
from (
select top 1 id
from T
where id > @i
order by id
union all
select top 1 id
from T
order by id
) x
Or perhaps:
select max(id)
from (
select min(id) as id
from T
where id > @i
union all
select min(id)
from T
) x
Guffa source
share