使用 SQL Server 2008 中另一个数据库中的值更新一个数据库中的记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5735123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UPDATE record in one database with values from another in SQL Server 2008?
提问by Slee
I need to update my new database with data from 1 column in my old database. Basically based on matching ItemID's I need to set the Description column in my new DB with the values in the old DB. I can see what needs to be updated when I do a join but I am not sure how to handle this update properly.
我需要用旧数据库中 1 列的数据更新我的新数据库。基本上基于匹配的 ItemID,我需要使用旧数据库中的值设置新数据库中的描述列。我可以在加入时看到需要更新的内容,但我不确定如何正确处理此更新。
回答by Martin Smith
BEGIN TRANSACTION
UPDATE t1
SET Description = t2.Description
FROM db1.dbo.foo t1
JOIN db2.dbo.foo t2
ON t1.ItemID = t2.ItemID
SELECT * FROM db1.dbo.foo
--prevents changes from being committed
ROLLBACK