求一句SQL

ballatong 2019-06-05 11:46:42

有两张表 Table1 如下:

Docnum Itemname Qty
7001 ABC 10
7002 BCD 20
7003 CCC 10
7004 ABC 15
7005 AAA 25
7006 SDF 60


Table2

Docnum Itemname Qty
7001 ABC 10
7002 BCD 20


Docnum是关键字段, 现在想实现把 Table1 里面的记录如果Table2里没有的,就插入到table2里,如果存在就过滤掉
(实际生产上 Table2的记录数量远比Table1 多)






...全文
97 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ballatong 2019-06-05
  • 打赏
  • 举报
回复
有没有效率更高的? Table2的数据会不断增长
卖水果的net 2019-06-05
  • 打赏
  • 举报
回复
-- 这个效果? insert into table2 select * from table1 except select * from table2
insus 2019-06-05
  • 打赏
  • 举报
回复
试这个Merge,如果你的sql版本不高,也许得使用另外办法。
Dear SQL(燊) 2019-06-05
  • 打赏
  • 举报
回复
insert into table2(docnum,itemname,qty)
select a.docnum,a.itemname,a.qty
from table1 a
left join table2 b on a.docnum=b.docnum
where b.docnum is null

6,129

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 新技术前沿
社区管理员
  • 新技术前沿社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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