[英]Copy one column from a database table to another column in another database table sql server
I had two databases (A and B), both were identical until I messed up one column in Table T1 on database A... basically set all records in that column to NULL. 我有两个数据库(A和B),直到我弄乱了数据库A的表T1中的一列为止,两个数据库都是相同的...基本上将该列中的所有记录都设置为NULL。 How do I update that incorrect column in T1 on database A with the correct data from its equivalent column from database B? 如何用数据库B中等效列中的正确数据更新数据库A中T1中的不正确列?
My insert command did not work as there are more columns in T1 on database A which do not allow null values to be inserted. 我的插入命令不起作用,因为数据库A的T1中有更多列,不允许插入空值。
insert into A.dbo.finance (finishdate)
select finishdate
from B.dbo.finance
I tired to do an inner join, but it says "multi-part identifier could not be bound". 我厌倦了进行内部联接,但是它说“无法绑定多部分标识符”。 Unfortunately the data in the other tables in database A has since changed hence simply using database B is not an option :-) 不幸的是,数据库A中其他表中的数据此后发生了变化,因此仅使用数据库B是不可行的:-)
Could someone please assist? 有人可以帮忙吗?
If ID is the id column of both tables, you should join them and update the one you want: 如果ID是两个表的id列,则应将它们加入并更新所需的表:
UPDATE A.dbo.finance
SET finishdate = B.finishdate
FROM A.dbo.finance A
JOIN B.dbo.finance B
ON A.ID = B.ID
DATABASE B 数据库B
Select finishdate into ##temp from dbo.finance B where 1 = 1
DataBase A 数据库A
insert into dbo.finance(finishdate)
select * from ##temp t where NOT EXISTS
(Select 1 from dbo.finance WHERE finishdate <> t.finishdate )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.