2016-03-06 55 views
0

我想基於2列大跳UPSERT(更新或創建)如何創建JDBC UPSERT查詢: 如果列A和B列在表中exsists然後更新值否則創建一個新行這個關鍵。基於2個鍵

//pasdo code for my query 
if(table.key1 == firstKey && table.key2 == secKey){ 
//update values for the row with key1, key2 
} else { 
//create a row with firstKey, secKey as keys 
} 

我有在後端的Oracle SQL服務器。

+0

所以,問題是... –

+0

如何建立這樣的查詢.... –

+0

只是谷歌的更新和插入查詢,你會得到很多的例子。 –

回答

1

你可以做類似這樣的東西... 在Oracle中,雙就像是虛表。它沒有任何行..它有助於創造需要以下可能不準確的SQL語法合併查詢 臨時表..

merge into table m using (select firstKey,secKey from dual d) on 
(m.key1 = d.firstKey and m.key2 = d.secKey) 
     when not matched then insert... -- put insert statement here 
      when matched then update .. -- put statemenet here