在SQL Server2005中有没有批量升级的方法?类似于2008年的合并将是完美的。我有一个作为临时工作区的表,它需要在会话完成时与主表进行协调。在2008年,merge在这方面做得很好,但我见过的唯一2005年的方法是针对单个upsert的,而不是批量的。想法?
发布于 2012-05-29 23:52:15
首先执行更新,然后执行插入。就像这样。
update TargetTable
set Col1 = SourceTable.Col1,
Col2 = SourceTable.Col2
from SourceTable
where TargetTable.ID = SourceTable.ID
insert into TargetTable(Col1, Col2)
select Col1, Col2
from SourceTable
where SourceTable.ID not in (select ID from TargetTable)更新:
如果主键中有多个列,则可以改用not exists。
update TargetTable
set Col1 = SourceTable.Col1,
Col2 = SourceTable.Col2
from SourceTable
where TargetTable.ID1 = SourceTable.ID1 and
TargetTable.ID2 = SourceTable.ID2
insert into TargetTable(Col1, Col2)
select Col1, Col2
from SourceTable
where not exists (select *
from TargetTable
where TargetTable.ID1 = SourceTable.ID1 and
TargetTable.ID2 = SourceTable.ID2)https://stackoverflow.com/questions/10802089
复制相似问题