en-UShe-IL
You are here:  Blog > new Forums FAQ > Forums FAQ 355
Register   |  Login
Minimize

Enter Title

-- original table
declare @Tbl as table (ID int)
insert @Tbl
select 1 union
select 2 union
select 3 union
select 4 union
select 6 union
select 7 union
select 12 union
select 22
 
declare
    @fromNumber int,
    @toNumber int,
    @byStep int = 1
select @fromNumber = min(ID),@toNumber = max(ID) from @Tbl
;WITH CTE_NumbersTable AS (
    SELECT @fromNumber AS i
    UNION ALL
    SELECT i + @byStep
    FROM CTE_NumbersTable
    WHERE
    (i + @byStep) <= @toNumber
)
SELECT CTE_NumbersTable.i
FROM CTE_NumbersTable
left join @Tbl OriginalTable on CTE_NumbersTable.i = OriginalTable.ID
where OriginalTable.ID is null
GO