SQL UPDATE multiple rows - 如何在SQL Server中从SELECT中进行UPDATE?

SQL Server UPDATE / sql / sql-server / tsql / select

SQL Server中,可以使用 INSERT.. SELECT 语句将行插入表中:

INSERT INTO Table (col1, col2, col3)
SELECT col1, col2, col3 
FROM other_table 
WHERE sql = 'cool'
UPDATE Table SET col1, col2
SELECT col1, col2 
FROM other_table 
WHERE sql = 'cool'
WHERE Table.id = other_table.id

Nicholas Carey



Answer #1

一种方式

UPDATE t 
SET t.col1 = o.col1, 
    t.col2 = o.col2
FROM 
    other_table o 
  JOIN 
    t ON t.id = o.id
WHERE 
    o.sql = '凉爽的'