Does DB2 have an “insert or update” statement?

后端 未结 4 1710
春和景丽
春和景丽 2020-11-28 07:22

From my code (Java) I want to ensure that a row exists in the database (DB2) after my code is executed.

My code now does a select and if no result is re

4条回答
  •  被撕碎了的回忆
    2020-11-28 07:29

    Another way is to execute this 2 queries. It's simpler than create a MERGE statement:

    update TABLE_NAME set FIELD_NAME=xxxxx where MyID=XXX;
    
    INSERT INTO TABLE_NAME values (MyField1,MyField2) 
    WHERE NOT EXISTS(select 1 from TABLE_NAME where MyId=xxxx);
    

    The first query just updateS the field you need, if the MyId exists. The second insertS the row into db if MyId does not exist.

    The result is that only one of the queries is executed in your db.

提交回复
热议问题