Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Table
ID
1234
3456
7890

SQL
SELECT *
FROM Table
WHERE ID IN (1234,9999,8888)

If ID is in where clause are not in table, then show those ids.
Ouput
9999,8888
Posted
Updated 28-Jul-15 23:22pm
v3

A simple way would by to use EXCEPT to remove existing values from all values. Consider the following example
SQL
SELECT all_values.Id
FROM (SELECT 1234 AS Id UNION ALL
      SELECT 9999 AS Id UNION ALL
      SELECT 8888 AS Id ) all_values
EXCEPT
SELECT Id
FROM MyTable

For more information, see: EXCEPT and INTERSECT (Transact-SQL)[^]
 
Share this answer
 
Comments
Divs from Bangalore 29-Jul-15 6:46am    
Thanks :)
Wendelius 29-Jul-15 6:47am    
You're welcome
Animesh Datta 29-Jul-15 9:01am    
Perfect one . My 5!
Wendelius 29-Jul-15 10:17am    
Thank you.
I'd use a cte:

SQL
with mycte as (
  select 1234 as id,
  union select 9999,
  union select 8888)

select m.id 
from mycte m
where not exists (select * from table t where m.id = t.id)


it seems a long way around but it's VERY scaleable.

Hopefully it will at least push you in a new direction
 
Share this answer
 
There can be better ways to do this but check this and also wait for some better answers.
I have used EXCEPT
SQL
SELECT * FROM 
(
	SELECT 1234 AS ID
	UNION 
	SELECT 9999
	UNION
	SELECT 8888
) T1
EXCEPT
SELECT * FROM
(
	SELECT 1234 AS ID
	UNION
	SELECT 3456
	UNION
	SELECT 7890
) AS T2


Hope, it helps :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900