Yogesh Yogesh - 4 months ago 8
SQL Question

updating data to/from two identical tables using SQL

I have two tables, lets say

table_a
and
table_b
, both have same schema/structure.

table_a(
id int,
name nvarchar(50)
)

table_b(
id int,
name nvarchar(50)
)


Now I want to update
table_a(name)
from
table_b(name)
, wherever id matches.

How can I do it?

Answer

You can use join in an update. The specific syntax varies by database. In SQL Server, it looks like:

update a
    set name = b.name
    from table_a a join
         table_b b
         on a.id = b.id;
Comments