Click here to Skip to main content
15,896,153 members

Response to: How to use NOT IN operator

Revision 1
While Shanalal Kasims solution is cool, it doesn't work when you have a larger gap in the sequence.
This is pure SQL and should work for all cases:
SQL
WITH numbers AS
    (
    SELECT 1 AS n
    UNION ALL
    SELECT n + 1
    FROM Numbers
    WHERE n + 1 <= SELECT Max(sequence) FROM table1 WHERE id = 1
)
SELECT  n
FROM    numbers
WHERE   n NOT IN (SELECT SEQUENCE FROM table1 WHERE id = 1)
Posted 13-Nov-12 22:35pm by Jörgen Andersson.
Tags: