简体   繁体   English

如何在 SQL 服务器的两个表中从另一个表中添加一个表中的列?

[英]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.

相关问题 两个共有2列的SQL表需要在表A中但不在表B中的记录(列1相同,但列2不同) - two SQL tables with 2 columns in common, need records that are in table A but not in table B (column 1 is same in both but different column 2) 如何在三个表中没有公共列的情况下联接三个表? - How to join three tables without having a common column in Third table? 如何将列从一个表复制到 SQL 服务器中的另一个表 - How to copy column from one table into another table in SQL Server 连接表A和表B,两者均具有column1 - Joining table A and table B, both having column1 as common 如何将2个表与另一个3rd表中的公共列联接? - How to join 2 tables with a common column in another 3rd table? SQL:将表中的列添加到另一个 - SQL: Add Column from table to another SQL Server 2008,在表中添加有关另一列的描述的列 - SQL server 2008 , add a column about the description of another column in a Table 如何基于另一个SQL Server表的单列中的更改添加一个SQL Server表的行和时间戳 - How to add a row and timestamp one SQL Server table based on a change in a single column of another SQL Server table 如何根据SQL Server中的条件从另一个表添加新列 - How to add new column from another table based on a condition in sql server 如何从 SQL Server 中的另一个表中获取列? - How can I get a column from another table in SQL Server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM