How do I UPDATE from a SELECT in SQL Server?

方法1

https://*.com/questions/2334712/how-do-i-update-from-a-select-in-sql-server

UPDATE
Table_A
SET
Table_A.col1 = Table_B.col1,
Table_A.col2 = Table_B.col2
FROM
Some_Table AS Table_A
INNER JOIN Other_Table AS Table_B
ON Table_A.id = Table_B.id
WHERE
Table_A.col3 = 'cool'

方法2

https://*.com/questions/1068447/update-with-two-tables

Your query does not work because you have no FROM clause that specifies the tables you are aliasing via A/B.

Please try using the following:

UPDATE A
SET A.NAME = B.NAME
FROM TableNameA A, TableNameB B
WHERE A.ID = B.ID

Personally I prefer to use more explicit join syntax for clarity i.e.

UPDATE A
SET A.NAME = B.NAME
FROM TableNameA A
INNER JOIN TableName B ON
A.ID = B.ID

总结

  UPDATE a
SET a.CustomTableItemID = b.CustomTableItemID
FROM @TempTable AS a
INNER JOIN dbo.tbm_cti_CustomTableItem AS b ON a.CustomTableItemGUID = b.CustomTableItemGUID;
上一篇:Sql Server cross apply和outer apply


下一篇:SELECT INTO和INSERT INTO SELECT(SQL Server)