如何基于 2 个键创建 JDBC upsert 查询

How to create JDBC upsert query based on 2 keys

我想基于 2 列执行更新插入(更新或创建): 如果 A 列和 B 列存在于 table 中,则更新值,否则使用此键创建一个新行。

//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 服务器。

你可以做类似这样的事情...... 在 oracle 中,dual 就像 dummy table。它没有任何行.. 它有助于创建合并查询所需的临时 table 以下可能不完全 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