内部联接的SQL更新

时间:2013-06-21 20:59:00

标签: sql-server-2005

假设我有两个表,并希望仅为t2中存在的那些记录更新t1中的记录。

ID = int, all others varchar

t1
id<PK>,sample,variable,status,notes

t2
sample,variable

我可以通过加入t2

从t1中选择子集
Select Sample FROM t1 INNER JOIN t2 ON t1.Sample = t2.Sample AND t1.Variable = t2.Variable

但是将此转换为不从t2提取更新值的更新语句的语法是什么? (我的值不是来自t2; t2只是限制应该更新哪些记录。)

UPDATE t1
SET Status=N'Complete',Notes=N'Lots of notes here'
INNER JOIN t2 ON t1.Sample = t2.Sample AND t1.Variable = t2.Variable  

1 个答案:

答案 0 :(得分:3)

你几乎得到了它:

UPDATE t1Aliased
SET Status=N'Complete',Notes=N'Lots of notes here'
from t1 t1Aliased 
INNER JOIN t2 ON t1Aliased.Sample = t2.Sample 
AND t1Aliased.Variable = t2.Variable