如果sql server 2005中的原始表不存在,则从一个表更新到另一个表

发布于 2024-10-23 14:27:03 字数 390 浏览 4 评论 0原文

我有 2 个具有相同列的表 - T1、T2。我想根据键列从 T2 中存在的列更新表 T1 列:如果键列存在,则从 T2 更新 T1 的其余列,如果不存在,则插入从 T2 到 T1 的整行。

这个查询不起作用:

IF EXISTS (SELECT keyC FROM T2 WHERE keyC in (select keyC from T1)) 
UPDATE T1 SET T1.c1 = T2.c1,
T1.c2 = T2.c2,
from  T2 WHERE  T2.keyC in (select keyC from T1)
ELSE (INSERT INTO T1 select * from T2)

知道如何修复它吗?

提前致谢,

格雷格

I have 2 tables with the same columns - T1, T2. I want to update table T1 columns from the columns that exist in T2 based on the key column: if the key column exists then update the rest of the columns of T1 from T2, if doesnt exist, then insert the whole row from T2 to T1.

This query doesnt do the job:

IF EXISTS (SELECT keyC FROM T2 WHERE keyC in (select keyC from T1)) 
UPDATE T1 SET T1.c1 = T2.c1,
T1.c2 = T2.c2,
from  T2 WHERE  T2.keyC in (select keyC from T1)
ELSE (INSERT INTO T1 select * from T2)

Any idea how to fix it?

Thanks in advance,

Greg

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空城仅有旧梦在 2024-10-30 14:27:03

由于您使用 SQL Server 2005,因此无法使用合并并且必须执行两条语句。一项更新,一项插入。

declare @T1 table (keyC int, c1 int)
declare @T2 table (keyC int, c1 int)

insert into @T1 values (1, 1)

insert into @T2 values (1, 10)
insert into @T2 values (2, 20)

-- Update all rows
update @T1 set
  c1 = T2.c1
from @T1 as T1
  inner join @T2 as T2
    on T1.keyC = T2.keyC

-- Insert new rows
insert into @T1 (keyC, c1)
select keyC, c1
from @T2 as T2
where not exists (select *
                  from @T1 as T1
                  where T1.keyC = T2.keyC)

Since you use SQL Server 2005 you can't use merge and have to do two statements. One update and one insert.

declare @T1 table (keyC int, c1 int)
declare @T2 table (keyC int, c1 int)

insert into @T1 values (1, 1)

insert into @T2 values (1, 10)
insert into @T2 values (2, 20)

-- Update all rows
update @T1 set
  c1 = T2.c1
from @T1 as T1
  inner join @T2 as T2
    on T1.keyC = T2.keyC

-- Insert new rows
insert into @T1 (keyC, c1)
select keyC, c1
from @T2 as T2
where not exists (select *
                  from @T1 as T1
                  where T1.keyC = T2.keyC)
£冰雨忧蓝° 2024-10-30 14:27:03

在SQL2008中你可以使用优雅的MERGE语句:

MERGE T1 AS target
USING T2 AS source ON (target.KeyC = source.KeyC)
WHEN MATCHED THEN 
    UPDATE SET  c1 = source.c1,
                c2 = source.c2
WHEN NOT MATCHED THEN   
    INSERT (c1, c2)
    VALUES (source.c1, source.c2);

In SQL2008 you can use the elegant MERGE statement:

MERGE T1 AS target
USING T2 AS source ON (target.KeyC = source.KeyC)
WHEN MATCHED THEN 
    UPDATE SET  c1 = source.c1,
                c2 = source.c2
WHEN NOT MATCHED THEN   
    INSERT (c1, c2)
    VALUES (source.c1, source.c2);
呢古 2024-10-30 14:27:03

对于 SQL Server 2008,请查看如何使用 MERGE 语句。

MERGE T1 AS target
USING T2 AS source
ON (target.keyC = source.keyC)
WHEN MATCHED THEN 
    UPDATE SET c1 = source.c1, c2 = source.c2
WHEN NOT MATCHED THEN   
    INSERT (keyC, c1, c2)
    VALUES (source.keyC, source.c1, source.c2)

编辑:OP将问题从2008年更改为2005年

对于早期版本,您需要2次操作:

UPDATE target
    SET c1 = source.c1, c2 = source.c2
    FROM T1 AS target
        INNER JOIN T2 as source
            ON target.keyC = source.keyC

INSERT INTO T1
    (keyC, c1, c2)
    SELECT keyC, c1, c2
        FROM T2
        WHERE NOT EXISTS(SELECT NULL FROM T1 WHERE keyC = T2.keyC)

For SQL Server 2008, take a look at using the MERGE statement.

MERGE T1 AS target
USING T2 AS source
ON (target.keyC = source.keyC)
WHEN MATCHED THEN 
    UPDATE SET c1 = source.c1, c2 = source.c2
WHEN NOT MATCHED THEN   
    INSERT (keyC, c1, c2)
    VALUES (source.keyC, source.c1, source.c2)

EDIT: OP changed question from 2008 to 2005

For earlier versions, you need 2 operations:

UPDATE target
    SET c1 = source.c1, c2 = source.c2
    FROM T1 AS target
        INNER JOIN T2 as source
            ON target.keyC = source.keyC

INSERT INTO T1
    (keyC, c1, c2)
    SELECT keyC, c1, c2
        FROM T2
        WHERE NOT EXISTS(SELECT NULL FROM T1 WHERE keyC = T2.keyC)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文