表A 中有如下记录
F_ID F_Code F_Name F_Address F_phone
1 001 小明 河南 111111
2 002 小黄 山东 222222
3 003 小白 浙江省 333333
4 004 小胡 江西省 444444
5 005 小徐 浙江省 555555
6 006 小丰 河北省 666666
表B 中有如下记录
F_ID F_Code F_Name F_Address F_phone
1 001 小明 河南 111111
2 002 小黄 山东 222222
3 003 小白 浙江省 000000
4 004 小胡 江西省 444444
5 005 小徐 山东省 555555
7 007 小李 江西省 777777要把表A中的记录插入到表B中,但是如果发现表B中的记录与表A中相同的保持不变;如果表B中没有的记录而表A中有则把这条记录插入到表B中,如果表B中有的记录而表A中没的记录则把这条记录删除。请问大家这语句该怎么写?
F_ID F_Code F_Name F_Address F_phone
1 001 小明 河南 111111
2 002 小黄 山东 222222
3 003 小白 浙江省 333333
4 004 小胡 江西省 444444
5 005 小徐 浙江省 555555
6 006 小丰 河北省 666666
表B 中有如下记录
F_ID F_Code F_Name F_Address F_phone
1 001 小明 河南 111111
2 002 小黄 山东 222222
3 003 小白 浙江省 000000
4 004 小胡 江西省 444444
5 005 小徐 山东省 555555
7 007 小李 江西省 777777要把表A中的记录插入到表B中,但是如果发现表B中的记录与表A中相同的保持不变;如果表B中没有的记录而表A中有则把这条记录插入到表B中,如果表B中有的记录而表A中没的记录则把这条记录删除。请问大家这语句该怎么写?
select a.F_Code,a.F_Name,a.F_Address,a.F_phone from 表A a where
not exists (
select 1 from 表B where
F_Code=a.F_Code,
F_Name=a.F_Name,
F_Address=a.F_Address,
F_phone =a.F_phone )delete from 表B b where
not exists (
select 1 from 表A where
F_Code=b.F_Code,
F_Name=b.F_Name,
F_Address=b.F_Address,
F_phone =b.F_phone )
insert into t1 select 1,'001','小明','河南','111111'
union all select 2,'002','小黄','山东','222222'
union all select 3,'003','小白','浙江','333333'
union all select 4,'004','小胡','江西','444444'
union all select 5,'005','小徐','浙江','555555'
union all select 6,'006','小丰','河北','666666'create table t2(f_id int,f_code nvarchar(10),f_name nvarchar(10),f_address nvarchar(20),f_phone nvarchar(15))
insert into t2 select 1,'001','小明','河南','111111'
union all select 2,'002','小黄','山东','222222'
union all select 3,'003','小白','浙江','000000'
union all select 4,'004','小胡','江西','444444'
union all select 5,'005','小徐','浙江','555555'
union all select 6,'006','小丰','河北','666666'
union all select 7,'006','小安','河北','777777'
insert into t2
select * from t1 where f_id not in
(
select t1.f_id from t1
inner join t2 on t1.f_id=t2.f_id
and t1.f_name=t2.f_name
and t1.f_code=t2.f_code
and t1.f_address=t2.f_address
and t1.f_phone=t2.f_phone
)
select * from t2delete from t1 where f_id not in
(
select t2.f_id from t2
inner join t2 on t1.f_id=t2.f_id
and t1.f_name=t2.f_name
and t1.f_code=t2.f_code
and t1.f_address=t2.f_address
and t1.f_phone=t2.f_phone)drop table t1
drop table t2