44

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 returned it does an insert. I really don't like this code since it exposes me to concurrency issues when running in a multi-threaded environment.

What I would like to do is to put this logic in DB2 instead of in my Java code. Does DB2 have an insert-or-update statement? Or anything like it that I can use?

For example:

insertupdate into mytable values ('myid')

Another way of doing it would probably be to always do the insert and catch "SQL-code -803 primary key already exists", but I would like to avoid that if possible.

2
  • Wouldn't it be better to have one transaction per thread? DBMS are good with multiple threads, that's why transactions were invented. They would solve the concurrency issue.
    – bortzmeyer
    Dec 1, 2008 at 10:58
  • That won't solve the concurrency issue because transactions only work by blocking on an already existing row. If the row does not exist, the transaction cannot block on it and so this kind of operation is still open to concurrency issues
    – Richard
    Nov 30, 2011 at 15:57

5 Answers 5

44

Yes, DB2 has the MERGE statement, which will do an UPSERT (update or insert).

MERGE INTO target_table USING source_table ON match-condition
{WHEN [NOT] MATCHED 
          THEN [UPDATE SET ...|DELETE|INSERT VALUES ....|SIGNAL ...]}
[ELSE IGNORE]

See:

http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=/com.ibm.db2.udb.admin.doc/doc/r0010873.htm

https://www.ibm.com/support/knowledgecenter/en/SS6NHC/com.ibm.swg.im.dashdb.sql.ref.doc/doc/r0010873.html

https://www.ibm.com/developerworks/community/blogs/SQLTips4DB2LUW/entry/merge?lang=en

2
  • 4
    Example woud be useful: merge into mytable using (values('col1', 'col2')) as tmp (code, name) on mytable.code = tmp.code when matched then update set name= col2'
    – agad
    Jan 3, 2017 at 13:05
  • 3
    DB2 has some dated limitations with the merge statement. #1 the stats about number of inserts/updates/deletes done with the merge are garbage and this is documented. #2. More importantly if you need to return an identity value from a column that auto creates an ID on insert you cannot do this with a Merge. the IDENTITY_VAL_LOCAL does not work to grab the new id associated with the insert. I've worked as a DBA's and programmed against so many databases and DB2 is the quirkiest, most difficult, strangest, dated feature database i've ever experienced in my 25+ years of doing this.
    – Tim Wiley
    Apr 19, 2019 at 12:42
19

I found this thread because I really needed a one-liner for DB2 INSERT OR UPDATE.

The following syntax seems to work, without requiring a separate temp table.

It works by using VALUES() to create a table structure . The SELECT * seems surplus IMHO but without it I get syntax errors.

MERGE INTO mytable AS mt USING (
    SELECT * FROM TABLE (
        VALUES 
            (123, 'text')
    )
) AS vt(id, val) ON (mt.id = vt.id)
WHEN MATCHED THEN
    UPDATE SET val = vt.val
WHEN NOT MATCHED THEN
    INSERT (id, val) VALUES (vt.id, vt.val)
;

if you have to insert more than one row, the VALUES part can be repeated without having to duplicate the rest.

VALUES 
    (123, 'text'),
    (456, 'more')

The result is a single statement that can INSERT OR UPDATE one or many rows presumably as an atomic operation.

3
  • There is a chance you want to use SELECT 'value' name FROM SYSIBM.SYSDUMMY1 instead of the SELECT * FROM TABLE VALUES Jul 19, 2017 at 8:07
  • 1
    "presumably as an atomic operation" - this depends on your transaction isolation level, and at least for me, resulted in errors in concurrent use case with default settings. The solution was to explicitly set isolation level "repeatable read" with keyword WITH RR. Isolation levels in db2: ibm.com/support/knowledgecenter/en/SSEPGG_10.5.0/…
    – eis
    Sep 19, 2018 at 8:35
  • I am so sad that we can't use prepared statement with this request. Sep 8, 2020 at 7:08
11

This response is to hopefully fully answer the query MrSimpleMind had in use-update-and-insert-in-same-query and to provide a working simple example of the DB2 MERGE statement with a scenario of inserting AND updating in one go (record with ID 2 is updated and record ID 3 inserted).

CREATE TABLE STAGE.TEST_TAB (  ID INTEGER,  DATE DATE,  STATUS VARCHAR(10)  );
COMMIT;

INSERT INTO TEST_TAB VALUES (1, '2013-04-14', NULL), (2, '2013-04-15', NULL); COMMIT;

MERGE INTO TEST_TAB T USING (
  SELECT
    3 NEW_ID,
    CURRENT_DATE NEW_DATE,
    'NEW' NEW_STATUS
  FROM
    SYSIBM.DUAL
UNION ALL
  SELECT
    2 NEW_ID,
    NULL NEW_DATE,
    'OLD' NEW_STATUS
  FROM
    SYSIBM.DUAL 
) AS S
  ON
    S.NEW_ID = T.ID
  WHEN MATCHED THEN
    UPDATE SET
      (T.STATUS) = (S.NEW_STATUS)
  WHEN NOT MATCHED THEN
    INSERT
    (T.ID, T.DATE, T.STATUS) VALUES (S.NEW_ID, S.NEW_DATE, S.NEW_STATUS);
COMMIT;
1
  • 2
    I would like to point out that some ibm databases use SYSIBM.SYSDUMMY1 as dummy table instead of SYSIBM.DUAL Jul 19, 2017 at 8:05
3

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 (MyField1,MyField2) values (xxx,xxxxx) 
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.

2
  • 2
    If your version of DB2 has MERGE, prefer that - it'll help eliminate certain potential concurrency issues. I wouldn't automatically label this "simpler", though; you have extra overhead from dealing with two statements (that must be maintained in sync!), and it doesn't really solve the possibility of a different process inserting the row between the UPDATE and your own INSERT... Apr 23, 2014 at 9:19
  • 2
    That insert statement is syntactically incorrect.
    – mustaccio
    Jun 5, 2014 at 19:29
-3

I started with hibernate project where hibernate allows you to saveOrUpdate(). I converted that project into JDBC project the problem was with save and update. I wanted to save and update at the same time using JDBC. So, I did some research and I came accross ON DUPLICATE KEY UPDATE :

String sql="Insert into tblstudent (firstName,lastName,gender) values (?,?,?) 
ON DUPLICATE KEY UPDATE 
firstName= VALUES(firstName),
lastName= VALUES(lastName),
gender= VALUES(gender)";

The issue with the above code was that it updated primary key twice which is true as per mysql documentation: The affected rows is just a return code. 1 row means you inserted, 2 means you updated, 0 means nothing happend.

I introduced id and increment it to 1. Now I was incrementing the value of id and not mysql.

String sql="Insert into tblstudent (id,firstName,lastName,gender) values (?,?,?) 
ON DUPLICATE KEY UPDATE 
id=id+1,
firstName= VALUES(firstName),
lastName= VALUES(lastName),
gender= VALUES(gender)";

The above code worked for me for both insert and update.

Hope it works for you as well.

1
  • 2
    The question was about DB/2. You're talking about MySQL. Did you actually check if your proposed Syntax is available in DB/2?
    – PoC
    Mar 23, 2021 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.