/* MySQL replace into 跟 insert 功能類似,不同點在於:replace into 首先嘗試插入數據到表中 1、如果發現表中已經有此行數據(根據主鍵或者唯一索引判斷)則先刪除此行數據,然后插入新的數據。 2、 否則,直接插入新數據。 */ Replace into table(id, update_time) values(1, now()) /* MSSQL(2008+) 判斷t_B記錄在T_A就更新,否則就插入 */ Merge Into table As a Using (select 1 As id,getdate() As update_time) as b on a.id=b.id When Matched then update set a.update_time=b.update_time When Not Matched then Insert(id,update_time) values(b.id,b.update_time); --MySQL的 Replace into Table(id, update_time) Select id,update_time From Other --MSSQL的 Merge Into Table As a Using (Select id,update_time From Other) as b on a.id=b.id When Matched then update set a.update_time=b.update_time When Not Matched then Insert(id,update_time) values(b.id,b.update_time);