Hello,

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 
SQL
- 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 
implement.

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

IF (EXISTS 
        (SELECT 1 FROM test_table WHERE PK_INTEGER_BS519=?)
) BEGIN 
        UPDATE test_table SET 
PK_INTEGER_BS519=?,TIME_850T2=?,BIGINT_63J3P=?,REAL_ZAF15=?,SMALLINT_731O9=?,BIT_4027E=?,INTEGER_95FDU=?,BIGINT_K1YYZ=?,DOUBLE_SRS0O=?,VARCHAR_L2NE2=?,VARCHAR_Z0U7L=?,BIT_32CP2=?,VARCHAR_3UD3C=?,BIGINT_1K7S0=?,DOUBLE_7NGCA=?,INTEGER_HMT78=?,DOUBLE_75L55=?,SMALLINT_MP89H=?,REAL_YOVS7=?,INTEGER_371TT=?
 
WHERE PK_INTEGER_BS519=?
END
ELSE
        BEGIN INSERT INTO 
test_table(PK_INTEGER_BS519,TIME_850T2,BIGINT_63J3P,REAL_ZAF15,SMALLINT_731O9,BIT_4027E,INTEGER_95FDU,BIGINT_K1YYZ,DOUBLE_SRS0O,VARCHAR_L2NE2,VARCHAR_Z0U7L,BIT_32CP2,VARCHAR_3UD3C,BIGINT_1K7S0,DOUBLE_7NGCA,INTEGER_HMT78,DOUBLE_75L55,SMALLINT_MP89H,REAL_YOVS7,INTEGER_371TT)
 
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
END

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.

Pavel.




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.

<<image/gif>>

Reply via email to