34,576
社区成员
发帖
与我相关
我的任务
分享
create table a
(
id int ,
name varchar(10)
)
insert into a
values(1,'bill')
insert into a
values(1,'bill')
create table b
(
id int ,
name varchar(10)
)
insert into b
values(1,'bill')
insert into b
values(1,'bill')
insert into b
values(2,'amount')
insert into b
values(3,'rose')
select * from a
select * from b
insert a
select *
from b
where checksum(*) not in (select checksum(*) from a)
INSERT a
SELECT * FROM b
EXCEPT SELECT * FROM a
Insert into a
select distinct id,[name]
from b
where not exists (select 1 from a
where a.id=b.id)
-- 如果只要求插入 a 表中不存在的
INSERT a
SELECT
id, name
FROM b
WHERE NOT EXISTS(
SELECT * FROM a
WHERE a.id = b.id
AND a.name = b.name);
INSERT a
SELECT DISTINCT -- 去重复
id, name
FROM b
WHERE NOT EXISTS( -- 如果还要求不包含 a 表中已经存在的
SELECT * FROM a
WHERE a.id = b.id
AND a.name = b.name);
INSERT a
SELECT DISTINCT -- 去重复
id, name
FROM b