Identifying which values do NOT match a table row
Use EXCEPT
:
SELECT * FROM
(values (1),(2),(3),(4)) as T(ID)
EXCEPT
SELECT ID
FROM [TABLE];
See SqlFiddle.
The values
constructor will only work on SQL Server 2008 or later. For 2005, use
SELECT 'value'
UNION SELECT 'value'
as detailed in this SO answer.
I would build up a table variable or temp table containing the IDs that you're searching for... then use Remus's solution, minus the 2008 syntactic sugar:
declare @t table (ID int)
insert into @t values (1)
insert into @t values (2)
insert into @t values (3)
insert into @t values (4)
insert into @t values (5)
select ID from @t
except
select ID
from [Table];
I'm now a couple years wiser (and have a newer SQL Server) than when I asked this question, so to celebrate the Famous Question badge I got for asking this, here's what I would do now. (I don't think I've ever used the EXCEPT
operator since.)
I would say the LEFT JOIN
method below is more useful than EXCEPT
since you can compose it with other joins without needing a CTE.
SELECT v.val
FROM (VALUES (1), (2), (3), (4), (5)) v (val)
LEFT JOIN dbo.SomeTable t
ON t.id = v.val
WHERE t.id IS NULL;