db-derby-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Pavel Bortnovskiy <PBortnovs...@Jefferies.com>
Subject upsert/merge?
Date Wed, 21 Sep 2011 17:54:30 GMT

Let me immediately state that I am aware that many people have 
requested/suggested to have the merge (Oracle) or upsert implemented and 
that there is a an issue created in Jira.

My question, although related, is a bit different.

In short, in my Java program I need to be able to:
- support INSERT, UPDATE, DELETE and MERGE/UPSERT on a variety of 
databases (thus the solution must be standard) - Derby, Oracle, Sybase, MS 
- avoid stored procedures
- be able to addBatch() all those JDBC PreparedStatements and invoke 
executeBatch() to efficiently process them

Currently, MERGE/UPSERT has been implemented by executing two statements - 
first a JDBC PreparedStatement UPDATE would be executed, and if it returns 
0 count, then the INSERT PreparedStatement. This is very inefficient and 
cannot be easily refactored to run as a batch. To batch it now I would 
have to implement a two-stage approach: to batch first all UPDATE 
statements with addBatch(), then invoke executeBatch(), analyze int[] that 
it returns and repeat those actions for the batch of the UPDATE 
statements. This is complicated, convoluted, difficult to support and 

I tried to create a Prepared Statement for something like this:

        (SELECT 1 FROM test_table WHERE PK_INTEGER_BS519=?)
        UPDATE test_table SET 



Just to find out that Derby doesn't support IF:

So, is there a better and more efficient way to do so in one SQL statement 
which can also be (1) batched and (2) standard (so to be supported by 
other databases, such as Sybase, Oracle, MS SQL, so that the same code can 
be used with all of them)?

Any other suggestions or thoughts would be greatly appreciated.


Jefferies archives and monitors outgoing and incoming e-mail. The contents of this email,
including any attachments, are confidential to the ordinary user of the email address to which
it was addressed. If you are not the addressee of this email you may not copy, forward, disclose
or otherwise use it or any part of it in any form whatsoever. This email may be produced at
the request of regulators or in connection with civil litigation. Jefferies accepts no liability
for any errors or omissions arising as a result of transmission. Use by other than intended
recipients is prohibited.  In the United Kingdom, Jefferies operates as Jefferies International
Limited; registered in England: no. 1978621; registered office: Vintners Place, 68 Upper Thames
Street, London EC4V 3BJ.  Jefferies International Limited is authorised and regulated by the
Financial Services Authority.
View raw message