简体   繁体   English

从另一个表更新并插入到一个表

[英]Update and insert to one table from another

I have two tables:我有两个表:

table1 : (ID, Code, Name) table1 :(ID,代码,名称)
table2 : (ID, Code, Name) with same columns table2 : (ID, Code, Name) 具有相同的列

I want to to insert data from table1 to table2 or update columns if that exists in table2 (table1.ID = table2.ID)我想将数据从 table1 插入到 table2 或更新 table2 中存在的列(table1.ID = table2.ID)

What is the simple way to do this?这样做的简单方法是什么?

WITH OUT MERGE合并

Merge table2 as target
using table1  as source
on
target.id=source.id
When matched 
Then
update 
set target.id=source.id,
    target.name=source.name
When not matched by Target Then
INSERT (id, name) VALUES (id, name);

There are some issues with Merge statement,so it should be used with caution .. Merge 语句存在一些问题,因此应谨慎使用..

Further i recommend ,using merge as two seperate DML statements like below..此外,我建议使用合并作为两个单独的 DML 语句,如下所示..

insert into table2
select * from table1 t1 where not exists (select 1 from table2 t2 where t2.id=t1.id)

update t2
set 
t2.id=t1.id,
t2.name=t1.name
from 
table1 t1
join
table2 t2
on t1.id=t2.id

Reasons being stated by Paul White here in his detailed answer ..保罗怀特在他的详细回答中陈述的原因..

MERGE table2 t2
USING table1 t1
ON t1.ID = t2.ID
WHEN MATCHED THEN
  UPDATE
  SET t2.Code = t1.Code, t2.Name = t1.Name 
WHEN NOT MATCHED BY TARGET THEN
  INSERT (ID, Name, Code)
  VALUES (t1.ID, t1.Name, t1.Code);

Assuming the ID column is unique and should not be set, it seems you could do it in two SQL Statements.假设 ID 列是唯一的并且不应设置,似乎您可以在两个 SQL 语句中完成。

/* UPDATE the rows in TABLE2 */
UPDATE TABLE2
    SET NAME = (SELECT NAME FROM TABLE1 WHERE TABLE1.CODE = TABLE2.CODE)
WHERE CODE IN (SELECT CODE FROM TABLE1)

/* INSERT the rows that are missing */
INSERT INTO TABLE2
    (CODE, NAME)
    (
         SELECT CODE, NAME
         FROM TABLE1
         WHERE CODE NOT IN (SELECT CODE FROM TABLE2)
    )

get all rows that are in table1 but not in table2获取在 table1 但不在 table2 中的所有行

insert into table2(id, code, name)(
SELECT table1.*
FROM table1
    LEFT JOIN table2 ON (table1.id = table2.id)
WHERE table2.C IS NULL
)

update table2更新表2

update table2 set name = (select name from table1 where table1.code = table2.code and table1.id = table2.id)

It might be worth looking at triggers on update and insert if you'd like to have this done manually如果您想手动完成此操作,可能值得查看更新和插入时的触发器

Inserting Date into target table once date is updated in source table Here there is a working example:源表中更新日期后将日期插入目标表这里有一个工作示例:

create table Table1(id int,name varchar(100));
create table Table2(id int,name varchar(100));
create trigger Table1Trigger after insert on Table1 for each row begin
    insert into Table2(id, name) values (new.id, new.name);
end;

Use the following queries to validate the results使用以下查询来验证结果

insert into Table1 values(1,'John'),(2,'Smith'),(3,'Carol');

Here I am writing a script that use full when you want to update table2 from table1.在这里,我正在编写一个脚本,当您想从 table1 更新 table2 时使用 full。

    Update table2 set table2.code = table1.code, table2.name=table1.name from table1 where table2.id=table1.id

And if you want to insert then use this script.如果你想插入然后使用这个脚本。

Insert into table2  (id,code,name) select id,code,name from table1

If in table2 id is not auto increment.如果在 table2 中 id 不是自动递增的。 Otherwise not insert the value of id column in table2.否则不在 table2 中插入 id 列的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM