sql sqlserver實現類似mysql中replace into功能


/*
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);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM