CREATE FUNCTION nocon(
@oid int
)RETURNS int
AS
BEGIN
declare @id int
set @id=0
select @id=count(1) from t where oid=@oid+1
if @id=0
set @id=1
RETURN @id
END
go
create table t
(oid int)
insert into t(oid)
select 1
union all
select 2
union all
select 3
union all
select 5
union all
select 6
union all
select 8
union all
select 10
select oid+1
from t
where dbo.nocon(oid)=1 and oid+1 not in(select oid from t) and oid+1<(select max(oid) from t)
order by oid