L'idea è di guardare da dove iniziano le lacune. Suppongo che tu stia utilizzando SQL Server 2012 e quindi lag() e lead() funzioni. Quanto segue ottiene il successivo id :
select t.*, lead(id) over (order by id) as nextid
from t;
Se c'è una lacuna, allora nextid <> id+1 . Ora puoi caratterizzare gli spazi vuoti usando where :
select id+1 as FirstMissingId, nextid - 1 as LastMissingId
from (select t.*, lead(id) over (order by id) as nextid
from t
) t
where nextid <> id+1;
MODIFICA:
Senza il lead() , farei la stessa cosa con una sottoquery correlata:
select id+1 as FirstMissingId, nextid - 1 as LastMissingId
from (select t.*,
(select top 1 id
from t t2
where t2.id > t.id
order by t2.id
) as nextid
from t
) t
where nextid <> id+1;
Assumendo l'id è una chiave primaria sul tavolo (o anche che ha solo un indice), entrambi i metodi dovrebbero avere prestazioni ragionevoli.