日期:2014-05-17  浏览次数:20432 次

SQL2005数据库量表有条件批量数据更新问题
比如:
A表:含有用户名、用户地址、电话号码等字段。
B表:定期从其他地方导入的资料,也含有用户名、用户地址、电话号码等字段
要求:由于数据量较大,能否不用循环对比的方式,如果B表有变化,批量更新A表的相关数数据,请高手指教。


------解决方案--------------------
如果是SQL2008 用MERGE 关键字
详细:http://www.cnblogs.com/andy_tigger/archive/2012/01/11/2319780.html
如果是SQL2005 以下版本
必须两个语句:
update + insert 
例如:
SQL code

update A set Name = b.name
from A a inner join B b where a.id = b.id

insert into A
select * from B b where not exists(select 1 from A a where a.id = b.id)