Skip to content
Advertisement

UPDATE table if data exists in another table

I have two tables. Table A contain this data

column_1  column_2
 A        SAND
 B        Mixed
 C        ROUGH

Table B contain this data

column_1 column_2
         SAND
         Mixed
         Mixed
         SAND
         ROUGH

I’m trying to write a SQL update query that can update data from column_1 in table A into column_1 in table B based on matching value from column_2 in table A and table B

The output should be like this:

    column_1 column_2
     A        SAND
     B        Mixed
     B        Mixed
     A        SAND
     C        ROUGH

I tried many ways but can’t seem to get it right. Can anyone help me how to achieve this? Thanks

Advertisement

Answer

since you are using sqlsrv, i believe this is sql server.

update b
set b.column_1 = a.column_1
from tableB b
inner join tableA a on a.column_2 = b.column_2
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement