如何用另一个数据库中另一表的数据更新一个表? [英] How to update one table with data from another table in a separate database?
问题描述
可能重复:
使用另一个表中的数据更新一个表
极端SQL noob在这里。我有两个数据库(Database1,Database2)。每个数据库都有相同的表。我想用来自Database1(Database1,TableA-> Database2,TableA)中TableA中的数据更新Database2中的TableA。
Extreme SQL noob here. I have two databases (Database1, Database2). Each db has the same tables. I want to update TableA in Database2 with the data in TableA from Database1 (Database1,TableA -> Database2,TableA).
执行此操作的最佳方法是什么?
What's the best way to do this?
推荐答案
像这样,假设您使用的是 dbo
模式。
Like this, assuming you use a dbo
schema.
insert into Database2.dbo.TableA (column1, column2, etc)
select column1, column2, etc
from Database1.dbo.TableA
您可能想截断Database2.dbo.TableA
如果输入的数据应该覆盖旧的数据,或者您使用的是身份字段,请首先使用。
You might want to truncate Database2.dbo.TableA
first if the incoming data should overwrite the old or you are using an identity field.
如果您需要在整个ID上使ID完全相同表,则应在运行脚本之前禁用 Database2.dbo.Table
上的identity属性,然后再重新启用它。
If you need the ids to be the exact same across the tables, you should disable the identity property on Database2.dbo.Table
before running the script and re-enable it afterward.
这篇关于如何用另一个数据库中另一表的数据更新一个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!