Peter Peter - 1 month ago 12
SQL Question

Update Statement

I have two tables called ASI and Call and the data in the tables looks like:

Call

ID Flag Created_ON
12 1 2011-08-05 11:48:09.477
15 1 2011-08-05 11:48:09.477
18 1 2011-08-05 11:48:09.477
20 1 2011-08-05 11:48:09.477
25 1 2010-08-12 14:05:11.000


ASI

ID Open_Date
12 2010-07-02 09:40:00.000
20 2010-08-12 14:05:11.000
18 2010-08-09 15:26:43.000


I want to update the Flag column in Call table with 0 where the ID is not present in ASI Table. In the above example I want ID's 15 and 25 to be 0.

Output:

ID Flag Created_ON
12 1 2011-08-05 11:48:09.477
15 0 2011-08-05 11:48:09.477
18 1 2011-08-05 11:48:09.477
20 1 2011-08-05 11:48:09.477
25 0 2010-08-12 14:05:11.000

Answer Source
UPDATE A
SET A.Flag = 0
FROM Call A
LEFT JOIN ASI B
ON A.Id = B.Id
WHERE B.Id IS NULL