更新声明

时间:2011-08-05 20:12:12

标签: sql sql-server sql-server-2005 tsql sql-server-2008

我有两个名为ASI和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

我想将Call表中的Flag列更新为0,其中ASI表中不存在ID。在上面的例子中,我希望ID 15和25为0。

输出:

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

3 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:0)

update call set flag=0 where id not in (
  select distinct id from asi)

答案 2 :(得分:0)

UPDATE Call
SET Flag = 0
WHERE ID NOT IN (SELECT ID FROM ASI)