How can I UPDATE from a SELECT in SQL Server?

  • Jan 09, 2023
  • 1
  • 641

We can insert rows into a table with an INSERT.. SELECT a statement like the one below,

INSERT INTO Table (column1, column2, column3, column4)
SELECT column1, column2, column3, column4
FROM table1
WHERE field = 'xyz'

Is it possible to update a table with SELECT

Answers (1)
Answer Accepted

We will use the below query to UPDATE the values from another table using the UPDATE query with the INNER JOIN.

UPDATE
    Table1
SET
    Table1.column1 = Table2.column1,
    Table1.column2 = Table2.column2,
    Table1.column3 = Table2.column3,
FROM
    TableX AS Table1
    INNER JOIN TableX  AS Table2
        ON Table1.id = Table2.id
WHERE
    Table1.column4 = 'xyz'
Submit your answer