首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >批量升级SQL Server 2005

批量升级SQL Server 2005
EN

Stack Overflow用户
提问于 2012-05-29 23:23:38
回答 1查看 1.8K关注 0票数 2

在SQL Server2005中有没有批量升级的方法?类似于2008年的合并将是完美的。我有一个作为临时工作区的表,它需要在会话完成时与主表进行协调。在2008年,merge在这方面做得很好,但我见过的唯一2005年的方法是针对单个upsert的,而不是批量的。想法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-29 23:52:15

首先执行更新,然后执行插入。就像这样。

代码语言:javascript
复制
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

代码语言:javascript
复制
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)
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10802089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档