How to insert/update a single record using a MERGE statement with Spring JDBC

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to insert/update a single record using a MERGE statement with Spring JDBC



I have an update/insert SQL query that I created using a MERGE statement. Using either JdbcTemplate or NamedParameterJdbcTemplate, does Spring provide a method that I can use to update a single record, as opposed to a Batch Update?



Since this query will be used to persist data from a queue via a JMS listener, I'm only dequeuing one record at a time, and don't have need for the overhead of a batch update.



If a batch is the only way to do it through Spring JDBC, that's fine... I just want to make certain I'm not missing something simpler.





Just use update method, for example this one: JdbcTemplate#update instead of BatchUpdate. Update updates a single record, batchUpdate updates multiple records using JDBC batch.
– krokodilko
Aug 6 at 16:55



update


Update


batchUpdate





@krokodiko : Thank you! That is exactly what I needed to know. You were first in, so if you provide an official answer, I will vote for your answer. Everyone has been helpful. I initially tried Spring's update methods, but due to a problem with my parameterized list, I misinterpreted the exception and determined that UPDATE was not the right method. I had not realized that with Spring JDBC, it is simply a single or batch update. I looked for a more specific method and could not find one, so your short answer gave me the insight I needed to solve my issue. Thank you for your answer!
– Michael M
Aug 9 at 15:36




2 Answers
2



Just use one of update methods, for example this one: JdbcTemplate#update instead of BatchUpdate.
Update updates a single record, batchUpdate updates multiple records using JDBC batch


update


Update


batchUpdate



You can use a SQL MERGE statment using only a one row query containing your parameters.


MERGE



For example if you have a table COMPANYcontaing IDas a key and NAMEas an attribute, the MERGE statement would be:


COMPANY


ID


NAME


MERGE


merge into company c
using (select ? id, ? name from dual) d
on (c.id = d.id)
when matched then update
set c.name = d.name
when not matched then insert (c.id, c.name)
values(d.id, d.name)



If your target table contains the parametrised key, the name will be updated, otherwise a new record will be inserted.


name



With JDBCTemplate you use the update method to call the MERGEstatement, as illustrated below (using Groovy script)


update


MERGE


def id = 1
def name = 'NewName'

String mergeStmt = """merge into company c
using (select ? id, ? name from dual) d
on (c.id = d.id)
when matched then update
set c.name = d.name
when not matched then insert (c.id, c.name)
values(d.id, d.name)""";

def updCnt = jdbcTemplate.update(mergeStmt, id, name);

println "merging $id, name $name, merged rows $updCnt"





I used Java, but your answer is absolutely correct. Thank you for sharing this example. Although I have used the update methods in Spring JDBC for many years, I am new to MERGE statements, and did not realize the update method was generic enough to handle a SQL statement that both updated and inserted. I had actually tried it briefly, but had an issue with my parameterized list. After that, I was looking for a more specific method, perhaps built for executing a MERGE. Thank you for the example!
– Michael M
Aug 9 at 15:26






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard