I have 2 tables (I am using T-SQL)
Table1
ID First_Name Last_Name Phone
-- ---------- --------- --------------
1 John Smith (643) 434-4343
2 Dave Miller (543) 344-3432
3 Tiffany Ovally (434) 343-6598
4 Dan Davis (534) 342-9876
5 Mike Kolis (454) 345-3434
Table2
Iden FirstN LastN PhoneN
---- ------ ----- ------
J-09 Tiffany Ovally (434) 343-6598
K-98 Dan Davis (534) 342-9876
W-03 Dave Miller (543) 344-3432
C-34 Mike Kolis (454) 345-3434
I need to check that key values from Table1 DO NOT exist in table2
I am doing the following
IF NOT EXISTS(SELECT * FROM Table2 t2
WHERE t2.FirstN = (Select First_Name from Table1 where ID = @ID)
AND t2.LastN = (Select Last_Name from Table1 where ID = @ID)
AND t2.PhoneN = (Select Phone from Table1 where ID = @ID)
)
BEGIN
....
END
Not sure if there is a more efficient way to do this as I am doing a subquery for each field..
Thanks
Your query is looking for records in Table2 that do not match the record in Table1 with an ID of @ID. Using an INNER JOIN here would be better than using the multiple subqueries: