H2 Database - MERGE INTOが使える 2008/05/24

SQLについてあまり知らないので、よく悩むのは、レコードがなければinsert、あればupdateと処理するとき。
まずselectして調べて、手続き型言語(SQLを操作する方の側)で、if~else~するっていうのしか思いつかなかったのだけど、oracle 9iというものは、どうもMERGEというものが使えるらしい。

参考



それで、このMERGEって何気に、H2 Databaseで、使えることがわかって、得した気分。
upsert = (update or insert)ってことなのでしょうかね。
対象の行はユニークになる行でないとだめなようです。

Updates the row if it exists, and if the row does not exist, inserts a new row.
If the key columns are not specified, the primary key columns are used to find the row.
This command is sometimes called 'UPSERT' as it updates a row if it exists,
or inserts the row if it does not yet exist.
If more than one row per new row is affected, an exception is thrown.H2 Database Engine

: