[英]How to add a column in a table from another table having common id column in both the tables in SQL Server?
I am trying to update a table1 with a Column2 from table2 where id should match while inserting the Column2 values from table2 in table1 for SQL Server.我正在尝试使用 table2 中的 Column2 更新 table1,其中 id 应该匹配,同时在 SQL 服务器的 table1 中插入来自 table2 的 Column2 值。
I tried with the below 2 sets of code, but it is not working.我尝试使用以下 2 组代码,但它不起作用。 Can anyone help me out with this?
谁能帮我解决这个问题?
alter table table1
add Column2 varchar(20)
insert into table1
Select t2.Column2
from table1 as t1
inner join table2 as t2
on t1.id = t2.id
And, below also:而且,下面还有:
Update table1
Set Column2 = (Select t2.Column2
from table1 as t1
inner join table2 as t2
on t1.id = t2.id)
Update t1
Set Column2 = t2.Column2
from table1 as t1
inner join table2 as t2
on t1.id = t2.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.