在sql server 2005中的一个查询中插入和更新

Raj*_*lar 2 sql sql-server sql-server-2005

可能重复:
选择/插入Upsert的版本:是否存在高并发的设计模式?

我必须根据条件将数据从一个表插入另一个表.

1.If Key is found update records
2.If key is not found insert the record.
Run Code Online (Sandbox Code Playgroud)

我使用的是sql server 2005.所以不能使用merge语句.请建议替代方案来实现这一目标

And*_*mar 5

要复制SourceTableDesitinationTable:

update  dst
set     col1 = src.col1
from    DestinationTable dst
join    SourceTable src
on      src.Key = dst.Key

insert  DestinationTable 
        (Key, col1)
select  Key
,       col1
from    SourceTable src
where   not exists
        (
        select  *
        from    DestinationTable dst
        where   src.Key = dst.Key
        )
Run Code Online (Sandbox Code Playgroud)


Kaf*_*Kaf 5

IF EXISTS(--Query to check for the existence of your condition here)
BEGIN
  --UPDATE HERE
END ELSE
BEGIN
  --INSERT HERE
END
Run Code Online (Sandbox Code Playgroud)