Does DB2 have an "insert or update" statement?

Posted by Mikael Eriksson on Stack Overflow See other posts from Stack Overflow or by Mikael Eriksson
Published on 2008-12-01T07:48:38Z Indexed on 2010/05/12 12:34 UTC
Read the original article Hit count: 341

Filed under:
|
|
|
|

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 issuses 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 allways do the insert and catch "SQL-code -803 primary key already exists", but I would like to avoid that if possible.

© Stack Overflow or respective owner

Related posts about db2

Related posts about database