How to skip update when parameter is null/0 in spring batch with JdbcBatchItemWriter

Viewed 181

We have a scenario wherein while doing a batch update on a table using JdbcBatchItemWriter, We are not finding a way to not update is the attribute is null. We don't want to have too many queries and ItemPreparedStatementSetter so we have a single query to update all fields in the table. Different batch jobs set update different attributes of the table

List<Report> summaryList = getSummaryList()    
JdbcBatchItemWriter<ItemMktDcGpReport> writer1 = new JdbcBatchItemWriter<>();
String sql_update = GenericConstants.UPDATE_QUERY;
writer1.setDataSource(dataSource);
ItemPreparedStatementSetter<Report> updatePreparedStatementSetter = new ItemMergeUpdatePreparedItemSetter();
writer1.setItemPreparedStatementSetter(updatePreparedStatementSetter);
writer1.setSql(sql_update);
writer1.afterPropertiesSet();
writer1.write(summaryList);

Tried the following seeing few examples on conditional update at the query but it doesn't help yet. Below is the query. Any help on this will be very much appreciated.

UPDATE_QUERY = "update  [dbo].[test_tbl]  
SET test_col1 = CASE When ?!=0  then ?
else test_col1 end ,
test_col2 =  CASE When ?!=0  then ? 
else test_col2 WHERE  market=? and country = ?"

I don't want to construct the SQL query based on parameter as I will lose out on the bulk writing feature of JdbcBatchItemWriter. Can someone please suggest the right approach to solve this problem and possibly correct the SQL query I'm writing?

0 Answers
Related