大家帮帮我,sql语句

xwbxn 2003-12-12 11:17:17
有两个表A和B,结构一样,B表包含A表的部分记录,我想把A表中有而B表没有的记录添加到B表中去,该怎么办啊?
...全文
22 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xwbxn 2003-12-12
  • 打赏
  • 举报
回复
谢谢大家了,请接分!
小子侯 2003-12-12
  • 打赏
  • 举报
回复

同意楼上的
mbzdh 2003-12-12
  • 打赏
  • 举报
回复
insert into B
select * from A where not exists(select id from B where A.id=id)
写的好, 我测试过了
zjcxc 元老 2003-12-12
  • 打赏
  • 举报
回复
--没有主键的话,用临时表处理
select * into #t from(select * from a union select * from b)
truncate table b
insert into b select * from #t
drop table #t
wzh1215 2003-12-12
  • 打赏
  • 举报
回复
如果没有主键:
select * into #cc from
(
select * from A
union select * from B
)tmp
delete from B
insert B
select * from #cc
victorycyz 2003-12-12
  • 打赏
  • 举报
回复
insert into B
select a.*
from a left join b on a.id=b.id
where b.id is null
txlicenhe 2003-12-12
  • 打赏
  • 举报
回复
insert into B表
select * from A表 where 主键 not in (select 主键 from B表 )
wzh1215 2003-12-12
  • 打赏
  • 举报
回复
insert into B
select * from A where not exists(select id from B where A.id=id)

34,590

社区成员

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

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