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

时间:2016-05-12 14:10:19

标签: sql sql-server sql-server-2005

我有两张桌子:

table1 :(身份证,代码,姓名)
table2 :( ID,代码,名称) 使用相同的列

我想将table1中的数据插入到table2中,或者如果table2中存在更新列(table1.ID = table2.ID)

这样做的简单方法是什么?

WHITOUT MERGE

6 个答案:

答案 0 :(得分:1)

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);

Merge语句存在一些问题,因此应与caution ..

一起使用

此外,我建议使用merge作为两个单独的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

Paul White在其详细answer ..

中陈述的原因

答案 1 :(得分:0)

假设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)
    )

答案 2 :(得分:0)

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);

答案 3 :(得分:0)

获取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
)

更新table2

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

如果您希望手动完成此操作,可能值得查看triggers更新并插入

答案 4 :(得分:0)

源表中更新日期后,将日期插入目标表 这里有一个工作示例:

unsigned int R, G, B, A;
if (FPDFAnnot_GetColor(oAnnot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A))
{
    oColor = QColor(R, G, B, A);
    return true;
}

if (FPDFAnnot_GetColor(oAnnot, FPDFANNOT_COLORTYPE_InteriorColor, &R, &G, &B, &A))
{
    oColor = QColor(R, G, B, A);
    return true;
}

int objects = FPDFAnnot_GetObjectCount(oAnnot);
for (int i = 0; i < objects; i++)
{

    FPDF_PAGEOBJECT object = FPDFAnnot_GetObject(oAnnot, i);
    if (FPDFPageObj_GetFillColor(object, &R, &G, &B, &A))
    {
        oColor = QColor(R, G, B, A);
        return true;
    }

    if (FPDFPageObj_GetStrokeColor(object, &R, &G, &B, &A))
    {
        oColor = QColor(R, G, B, A);
        return true;
    }
}

使用以下查询来验证结果

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;

答案 5 :(得分:-1)

在这里,当我想从table1更新table2时,我正在编写一个使用full的脚本。

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

如果你想插入,那就使用这个脚本。

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

如果在table2中id不是自动递增。 否则,不在table2中插入id列的值。