[英]SQL Server : copy data from one table to another
I want to update Table2
names with names from Table1
with matching Ids 我想用具有匹配ID的
Table1
名称更新Table2
名称
I have around 100 rows in each table. 每个表中大约有100行。
Here is my sample tables. 这是我的样本表。
Table1 表格1
Table2 表2
Sample data 样本数据
Table1 表格1
ID |Name
--------
1 |abc
2 |bcd
Table2 表2
ID |Name
--------
1 |xyz
2 |OOS
Expected result 预期结果
Table2 表2
ID |Name
--------
1 |abc
2 |bcd
How can I do this? 我怎样才能做到这一点?
You can use an UPDATE
with a JOIN
of the two tables on the id
field: 您可以将
UPDATE
与id
字段中两个表的JOIN
一起使用:
update t2
set t2.name = t1.name
from table2 t2
inner join table1 t1
on t2.id = t1.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.