更新表连接到具有不同条件的另一个表

时间:2013-06-10 12:25:07

标签: sql-server-2008

我想将一个内部联接的表更新到另一个表,但是应该更新的行应该具有特定条件或者不存在于第二个表中:

Update T1
Set STATUS = 'R'
From table1 T1
 inner join table2 T2
   on T1.ID = T2.ID and T2.STATUS = 'F' 

Update T1
Set STATUS = 'R'
From table1 T1
Where T1.ID not exists in(Select T2.ID from table2 T2) 

如何通过一个查询来编写它?

1 个答案:

答案 0 :(得分:0)

您可以在where子句中执行OR。如下所示:

Update T1 Set STATUS = 'R' From table1 T1 
    inner join table2 T2 on T1.ID = T2.ID and T2.STATUS = 'F' or T1.ID not exists in(Select T2.ID from table2 T2)