如何将数据从某些 table 中的一个列复制到 SQL 服务器中另一个 table 中的同名列

How to copy data from one column in certain table to same named column in another table in SQL Server

我有两个SQL服务器tables sub_aminer_author2papersub_aminer_paper,还有一个共同的列pid

另一列 aid 仅存在于一个 table 中,即 sub_aminer_author2paper

现在我必须将列 aid 从 table sub_aminer_author2paper 复制到 table sub_aminer_paper 中新创建的列 aid,而table sub_aminer_paper 中的 pid 列应与 table sub_aminer_author2paper

中的 pid 匹配

我试过这个查询:

insert into sub_aminer_paper (sub_aminer_paper.aid) 
    (select sub_aminer_author2paper.aid  
     from sub_aminer_author2paper 
     INNER JOIN sub_aminer_paper ON sub_aminer_paper.pid = sub_aminer_author2paper.pid)

但它没有按要求工作,它已将所有 aid 值插入 table 的末尾作为

请帮忙!

我认为你描述的是 UPDATE scenario here, not an INSERT,如果我没理解错的话。

更新语句可以包含一个包含多个表的部分,如下所示:

UPDATE sub_aminer_paper 
SET aid = sub_aminer_author2paper.aid
FROM sub_aminer_author2paper 
INNER JOIN sub_aminer_paper ON sub_aminer_paper.pid = sub_aminer_author2paper.pid

此语句将使用在 sub_aminer_author2paper 中找到的用于匹配 pid 的辅助值更新 sub_aminer_paper 中现有行的辅助列。

在 sub_aminer_paper table 中添加名为 aid 且为空的列。然后简单更新一下。

UPDATE A
SET A.aid = B.aid
FROM sub_aminer_paper A
INNER JOIN sub_aminer_author2paper B ON A.pid = B.pid