SQL Server:将数据从一列复制到另一列?

时间:2021-01-19 10:20:41

I have two tables with the same column anomaly_id. I want to copy the row of anomaly_id from the first table to the second table using this code

我有两个具有相同列anomaly_id的表。我想使用此代码将anomaly_id行从第一个表复制到第二个表

UPDATE amb.anamoly_log_update
SET anamoly_id = t2.anomaly_id
FROM amb.anamoly_log_update t1 
INNER JOIN amb.anomaly_fee t2 ON t1.anamoly_id=t2.anomaly_id

Even after I did that it shows 0 rows affected, when there is data in amb.anomaly.fee (source table)

即使在我这样做之后它显示0行受影响,当amb.anomaly.fee中存在数据时(源表)

Please help

Edit: Comment from post: I just want to copy all the anamoly_id from amb.anamoly_fee to amb.anamoly_log_update. My code might be nonsensical. Please do review it.

编辑:来自帖子的评论:我只想将amb.anamoly_fee中的所有anamoly_id复制到amb.anamoly_log_update。我的代码可能是荒谬的。请检查一下。

5 个解决方案

#1


3  

To copy the id from anomaly_fee to anamoly_log_update use :

要将id从anomaly_fee复制到anamoly_log_update,请使用:

INSERT INTO anamoly_log_update (anamoly_id)
      SELECT anamoly_id FROM anomaly_fee

with both columns it looks like that:

两列都看起来像这样:

INSERT INTO anamoly_log_update (anamoly_id,PID)
      SELECT anamoly_id,PID FROM anomaly_fee

#2


0  

You only would copy the data if they where in both tables .. and then there is nothing update because you do not change the data => 0 rows affected

你只会复制数据,如果它们在两个表中的位置..然后没有任何更新,因为你没有更改数据=> 0行受影响

ON t1.anamoly_id=t2.anomaly_id

please think about what you really want to do and change your description ..

请考虑一下你真正想做的事情并改变你的描述..

#3


0  

Does amb.anamoly_log_update contain at least one row corresponding to the anamoly_id that's present in amb.anamoly_fee? You are trying to join on two tables on anamoly_id.

amb.anamoly_log_update是否包含至少一行与amb.anamoly_fee中存在的anamoly_id相对应的行?您正尝试在anamoly_id上加入两个表。

#4


0  

You need to provide other linkage between tables than t1.anamoly_id=t2.anomaly_id or the query will do nothing

您需要在表之间提供除t1.anamoly_id = t2.anomaly_id之外的其他链接,否则查询将不执行任何操作

#5


0  

merge into amb.anamoly_log_update as t1
    using amb.anomaly_fee as t2
       on  t1.anamoly_id=t2.anomaly_id
when matched then
    update set t1.anamoly_id = t2.anomaly_id

#1


3  

To copy the id from anomaly_fee to anamoly_log_update use :

要将id从anomaly_fee复制到anamoly_log_update,请使用:

INSERT INTO anamoly_log_update (anamoly_id)
      SELECT anamoly_id FROM anomaly_fee

with both columns it looks like that:

两列都看起来像这样:

INSERT INTO anamoly_log_update (anamoly_id,PID)
      SELECT anamoly_id,PID FROM anomaly_fee

#2


0  

You only would copy the data if they where in both tables .. and then there is nothing update because you do not change the data => 0 rows affected

你只会复制数据,如果它们在两个表中的位置..然后没有任何更新,因为你没有更改数据=> 0行受影响

ON t1.anamoly_id=t2.anomaly_id

please think about what you really want to do and change your description ..

请考虑一下你真正想做的事情并改变你的描述..

#3


0  

Does amb.anamoly_log_update contain at least one row corresponding to the anamoly_id that's present in amb.anamoly_fee? You are trying to join on two tables on anamoly_id.

amb.anamoly_log_update是否包含至少一行与amb.anamoly_fee中存在的anamoly_id相对应的行?您正尝试在anamoly_id上加入两个表。

#4


0  

You need to provide other linkage between tables than t1.anamoly_id=t2.anomaly_id or the query will do nothing

您需要在表之间提供除t1.anamoly_id = t2.anomaly_id之外的其他链接,否则查询将不执行任何操作

#5


0  

merge into amb.anamoly_log_update as t1
    using amb.anomaly_fee as t2
       on  t1.anamoly_id=t2.anomaly_id
when matched then
    update set t1.anamoly_id = t2.anomaly_id