Question

I need to insert 3 columns from one table to another, using JOIN by 3 fields: name, surname and age

enter image description here

I need update column status, status1 and status2 in table_2 with values from table_1

IF 

table_1.name = table_2.name
table_1.surname = table_2.surname
table_1.age= table_2.age
Was it helpful?

Solution

UPDATE t2
SET 
 t2.[status]=t1.[status]
,t2.[status1]=t1.[status1]
,t2.[status2]=t1.[status2]
FROM [table_1] t1
INNER JOIN [table_2] t2 
ON (t1.name=t2.name AND t1.surname=t2.surname AND t1.age=t2.age)

As you mentioned in comments that these table from different databases, then please change only the two line like.

 FROM [yourDataBase1Name].[dbo].[table_1] t1
 INNER JOIN [yourDataBase2Name].[dbo].[table_2] t2

OTHER TIPS

Just update the table with Join. it will be something like this:

UPDATE t2
SET t2.status = t1.status,
    t2.status1 = t1.status1,
    t2.status2 = t1.status2
FROM t2 JOIN t1 on (t1.first_name = t2.first_name AND t1.last_name = t2.last_name AND t1.age = t2.age);

Look here for more information:SQL update query using joins

SQL Fiddle: http://sqlfiddle.com/#!3/b3951/1

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top