Upserting a row into an hsqldb table using the MERGE SQL statement

浪尽此生 提交于 2019-12-12 11:41:38

问题


Did not understand from the documentation how am I supposed to use it.

Say I have a row I wish to upsert into the collection, overriding any existing values, if the row is already present. I do not have the row primary key, but I do have a unique key.

Can any one show me the MERGE statement that upserts such a row into an HSQLDB table?


回答1:


A simple example for HSQLDB is as follows:

CREATE TABLE B(ID INT UNIQUE, A_ID INT);

MERGE INTO B 
  USING (VALUES 2, 3) I (ID, A_ID) 
  ON (B.ID=I.ID)
  WHEN MATCHED THEN UPDATE SET B.A_ID = I.A_ID
  WHEN NOT MATCHED THEN INSERT (ID, A_ID) VALUES (I.ID, I.A_ID)

The USING clause contains the new data. The ON clause is the match condition. Note there is no requirement for a primary key or unique constraint to be used here. Any match condition will do. The WHEN MATCHED and WHEN NOT MATCHED clauses are used for UPDATE and INSERT respectively. In this simple example, the data from the USING clause is referenced, but you can insert or update with any other value.



来源:https://stackoverflow.com/questions/15074694/upserting-a-row-into-an-hsqldb-table-using-the-merge-sql-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!