Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. CREATE TABLE #NewCustomers (ID INT);
  2. CREATE TABLE #ExistingCustomers (ID INT);
  3.  
  4. INSERT INTO #NewCustomers
  5. ( ID )
  6. VALUES
  7. (8), (9), (10), (1), (3), (8);
  8.  
  9. INSERT INTO #ExistingCustomers
  10. ( ID )
  11. VALUES
  12. ( 1) , (2), (3), (4), (5);
  13.  
  14.  
  15. -- EXCEPT filters for DISTINCT values
  16. SELECT * FROM #NewCustomers AS nc
  17. EXCEPT
  18. SELECT * FROM #ExistingCustomers AS ec
  19.  
  20. -- NOT IN returns all values without filtering
  21. SELECT * FROM #NewCustomers AS nc
  22. WHERE ID NOT IN (SELECT ID FROM #ExistingCustomers AS ec)
  23.  
  24. INSERT INTO #ExistingCustomers
  25. ( ID )
  26. VALUES
  27. ( NULL );
  28.  
  29. -- With NULL values in the right-hand table, EXCEPT still returns the same results as above
  30. SELECT * FROM #NewCustomers AS nc
  31. EXCEPT
  32. SELECT * FROM #ExistingCustomers AS ec
  33.  
  34. -- NOT IN now returns no results
  35. SELECT * FROM #NewCustomers AS nc
  36. WHERE ID NOT IN (SELECT ID FROM #ExistingCustomers AS ec)
  37.  
  38. DROP TABLE #NewCustomers;
  39. DROP TABLE #ExistingCustomers;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement