0

I have two tables on a Microsoft SQL Server 2016 Instance.

Table#1

deviceid string1 Date
1 1234ALT 30.06.24
2 1234ALT 30.06.24

Table#2

deviceid string1 Date
1
2

Table#2 has a lot more columns but for my usecase I only need these 3

Now I need a query that matches the deviceid from table1 to table2 and inserts the values for string1 and date from table1 into table2

The origin of table1 is an Excel file I already inserted in to a new table to make it easier to import. If I would try

update table2
set string1 = (select string1 
               from table1
               where table2.deviceid = table1.deviceid)
where table2.deviceid = table1.deviceid

and do the same for the date column would this work?

Dale K
  • 25,246
  • 15
  • 42
  • 71
pmbaa
  • 11
  • 3

2 Answers2

0

You need to simply join the tables and update:

update t2 set
  string1 = t1.string1,
    Date = t1.Date
from table#1 t1 
join table#2 t2 on t2.deviceid = t1.deviceid;
Stu
  • 30,392
  • 6
  • 14
  • 33
0
UPDATE t2
SET t2.string1 = t1.string1,
t2.Date = t1.Date
FROM Table2 t2
JOIN Table1 t1 ON t2.deviceid = t1.deviceid;
William
  • 942
  • 2
  • 12
  • 25