一般情况下博主已经对在SELECT语句当中使用INNER JOIN非常娴熟,但在UPDATE当中使用INNER JOIN的场景就为数不多了。以下博主将为你介绍在UPDATE场景当中使用INNER JOIN的例子!(事先声明,本文仅针对入门者!大牛请绕道。)
T-SQL脚本的内容不多跟SELECT的INNER JOIN用法几乎一致:
UPDATE table1 SET
table1.ColA = t2.ColB
FROM
table1 as t1 INNER JOIN table2 AS t2
ON t1.Id = t2.Id --------------------------------------
--使用表别名的UPDATE
UPDATE t1 SET
t1.ColA = t2.ColB
FROM
table1 as t1 INNER JOIN table2 AS t2
ON t1.Id = t2.Id
--------------------------------------
/*
table1
-------------
Id ColA
1 A1
2 A2
3 A3
4 A4
5 A5 table2
-------------
Id ColB
1 B1
2 B2
3 B3 ###########
执行后
########### table1
-------------
1 B1
2 B2
3 B3
4 A4
5 A5 table2
-------------
1 B1
2 B2
3 B3
*/