简体   繁体   English

有条件地将数据从一个数据库插入另一个数据库

[英]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 服务器中,我有第一个数据库,其中包含一个包含IDData列的表,第二个数据库是第一个数据库的副本,除了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.

相关问题 将已解析的数据从一个数据库插入另一个数据库 - Inserting parsed data from one database into another 将数据从一个表插入到另一个表中但在另一个数据库中 - Inserting Data from one table into another table but in a different database 将数据从一个表插入数据库中的另一个表(不同的服务器) - Inserting data from one table to another in a database(different servers) 将数据从一台服务器上的一个数据库插入另一台服务器上的另一数据库 - inserting data from one database on one server to another database on other server SQL Server:按特定日期将数据从一个数据库插入到另一个数据库结构 - SQL Server: inserting data from one database to another database structure by specific date 将数据从一个表插入到另一个表? - Inserting data from one table to another? 从另一个数据库更新/插入一个数据库中的表 - Updating/Inserting tables in one database from another database SQL:在插入另一个表时有条件地从表中插入/选择 - SQL: Conditionally inserting/selecting from a table while inserting into another 如果用户选择了一个字段,则将数据从一个表插入到另一个表 - Inserting data from one table to another if one field is chosen by user 通过将Oracle中的一列分组将数据从一个表插入到另一个表 - Inserting the data from one table to another by grouping one column in Oracle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM