日期:2014-05-19  浏览次数:20428 次

关于两个表的问题
有表A中一个字段DWDM(值:12   10   45   20)
有表B中一个字段DWDM(值:12   10   45   20   23   25)
DWDM是两表的主健


怎么样把B表的数据覆盖的A表中


------解决方案--------------------
insert A
select * from B where DWDM not in (select DWDM from A)
------解决方案--------------------
--maybe
update a set dwdm=b.dwdm from tablea a join tableb b on charindex(a.dwdm,b.dwdm)=1
------解决方案--------------------
delete A
where DWDM not in (select DWDM from B)

------解决方案--------------------
TRUNCATE table A
insert A select * from B
也可以..`
------解决方案--------------------
你到现在还没有描述清楚两个表主键是关系?
------解决方案--------------------
insert A
select * from B where DWDM not in (select DWDM from A)

------解决方案--------------------
insert into A
select * from B where DWDM not in (select DWDM from A)

------解决方案--------------------


create table A(DWDM int)
insert A select 12
union all select 10
union all select 45
union all select 20
go
create table B(DWDM int)
insert B select 12
union all select 10
union all select 45
union all select 20
union all select 23
union all select 25
go

insert A
select DWDM from B as tmp
where not exists(select 1 from A where DWDM=tmp.DWDM)

select * from A