Raj*_*lar 2 sql sql-server sql-server-2005
我必须根据条件将数据从一个表插入另一个表.
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语句.请建议替代方案来实现这一目标
要复制SourceTable到DesitinationTable:
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)
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)