0

I want to update Table2 names with names from Table1 with matching Ids

I have around 100 rows in each table.

Here is my sample tables.

Table1

  • ID
  • Name

Table2

  • ID
  • Name

Sample data

Table1

    ID |Name
    --------
     1 |abc
     2 |bcd

Table2

    ID |Name
    --------
     1 |xyz
     2 |OOS

Expected result

Table2

    ID |Name
    --------        
     1 |abc
     2 |bcd

How can I do this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Gladdy
  • 13
  • 1
  • 5
  • 2
    Show us [what you have tried](http://www.whathaveyoutried.com). – Kermit Oct 25 '12 at 21:41
  • possible duplicate of [Move SQL data from one table to another](http://stackoverflow.com/questions/1612267/move-sql-data-from-one-table-to-another) – Dan J Oct 25 '12 at 21:44

1 Answers1

4

You can use an UPDATE with a JOIN of the two tables on the id field:

update t2
set t2.name = t1.name
from table2 t2
inner join table1 t1
  on t2.id = t1.id

See SQL Fiddle with Demo

Taryn
  • 242,637
  • 56
  • 362
  • 405