[英]Inserting data from one database to another conditionally
In my MSQL server I have a first database with a table that contains ID
and Data
columns, a second database, which is a copy of the first, except the Data
column is empty.在我的 MSQL 服务器中,我有第一个数据库,其中包含一个包含
ID
和Data
列的表,第二个数据库是第一个数据库的副本,除了Data
列是空的。
I would like to transfer data from the first database into the second, inserting into the row with the corresponding ID
.我想将数据从第一个数据库传输到第二个数据库,插入到具有相应
ID
的行中。 I would like the query to look something like this, but I'm not sure about the syntax我希望查询看起来像这样,但我不确定语法
INSERT INTO db1.dbo.Table (Data)
SELECT (Data)
FROM db2.dbo.Table
WHERE db1.dbo.Table(ID) = db2.dbo.Table(ID)
You can do an update on your second table like that:您可以像这样对第二张桌子进行更新:
UPDATE
t2
SET
t2.Data = t1.Data,
FROM
dbone.t1 AS t1
INNER JOIN dbtwo.t2 AS t2 ON t1.ID = t2.ID
If your second table is empty you can "copy" your data like that:如果您的第二个表是空的,您可以像这样“复制”您的数据:
INSERT INTO dbone.t1 (ID, Data)
SELECT ID, Data
FROM dbtwo.t2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.