相同表数据复制的问题。

散步的阿木 2011-08-10 11:32:07
A、B数据库有两个一模一样的表table_C

A、B的table_C都有数据。现在要将A的table_C中的数据全部复制到B库中,主键存在的update,主键不存在的行就insert。

各位大大,这个该如何写效率高呢?
...全文
56 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
AcHerat 元老 2011-08-10
  • 打赏
  • 举报
回复
存在的就update,那么update 多少列?

update a
set a.col_1 = b.col_1,a.col_2 = b.col_2,...
from B..table_C a join A..table_C b on a.主键 = b.主键

insert into B..table_C
select *
from A..table_C t
where not exists (select 1 from B..table_C where 主键 = t.主键)
chuanzhang5687 2011-08-10
  • 打赏
  • 举报
回复
inert into A库.dbo.表1
select * from B库.dbo.表1
快溜 2011-08-10
  • 打赏
  • 举报
回复
insert into B.dbo.table_c
select * from A.dbo.table_c a
where not exists(select * from B.dbo.table_c where id=a.id)

update a set ..
from B.dbo.table_c a,A.dbo.table_c b where a.id=b.id

xuam 2011-08-10
  • 打赏
  • 举报
回复
哪个是主键?
huanzhou217 2011-08-10
  • 打赏
  • 举报
回复
学习了

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧