需要将A表的3条数据查询出来插入B表,插入之前需要判断B表是否已经存在该条数据,存在则过滤掉,不存在则插入,需要涉及到判断,遍历等技术点,感谢大家帮忙解答!
使用游标进行插入,批量操作,先把表A的数据查询出来,再进行插入。可以参考一下我写的游标 https://ask.csdn.net/questions/7526044?spm=1001.2014.3001.5505
INSERT INTO B
SELECT CODE
,NAME
FROM A
WHERE NOT EXISTS (SELECT 1 FROM B WHERE A.CODE = B.CODE )
按code和name同时过滤
USE [test]
GO
insert into dbo.test_b
select code,name from dbo.test_a where not exists (select 1 from dbo.test_b where test_b.code=test_a.code and test_b.name=test_a.name)
只按code过滤
USE [test]
GO
insert into dbo.test_b
select code,name from dbo.test_a where not exists (select 1 from dbo.test_b where test_b.code=test_a.code)