SqlServer(先更新,受影響條數爲0,則Insert,經過事務):sql
begin tran update table set column=columnvalue where wherestr if @@ROWCOUNT==0 begin insert into table (column) values (columnvalue) end commit tran
Oracle(Merge Into的原理是,從using 搜出來的結果逐條與on條件匹配,而後決定是update仍是Insert):spa
MERGE INTO table t --USING (SELECT whereStrColumn FROM SYS.DUAL) s 大部分狀況下使用SYS.DUAL是個好的選擇,可是若是同時插入或更新經過個主鍵,會致使ORA-00001違反惟一鍵約束 USING (SELECT whereStrColumn FROM table where whereStr) s ON (t.whereStrColumn=s.whereStrColumn) WHEN MATCHED THEN UPDATE SET column=columnValue WHEN NOT MATCHED THEN INSERT (column) VALUES (columnValue)
MySql(Mysql語法提供了):rest
INSERT INTO table (column) VALUES (columnValue) ON DUPLICATE KEY UPDATE column=columnValue