0

Looking for best practices to build an update query with not null values.

Below the my implementation where passed parameter firstName, middleName, lastName, address can be null. I want to build an update query with not null values only. I have used multiple if condition for not null and comma separator.

public void updatePerson(String id, String firstName, String middleName, String lastName, String address) {
    StringBuilder query = new StringBuilder("UPDATE PERSON SET ");
    MapSqlParameterSource mapSqlParameterSource = new MapSqlParameterSource();
    mapSqlParameterSource.addValue("id", id);
    boolean columnSepartor = false;

    if (firstName != null) {
        query.append(" FIRST_NAME =:firstName");
        mapSqlParameterSource.addValue("firstName", firstName);
        columnSepartor = true;
    }

    if (middleName != null) {
        query = columnSepartor ? query.append(", MIDDLE_NAME =:middleName") : query.append(" MIDDLE_NAME =:middleName");
        mapSqlParameterSource.addValue("middleName", middleName);
        columnSepartor = true;
    }

    if (lastName != null) {
        query = columnSepartor ? query.append(", LAST_NAME =:lastName") : query.append(" LAST_NAME =:lastName");
        mapSqlParameterSource.addValue("lastName", lastName);
        columnSepartor = true;
    }

    if (address != null) {
        query = columnSepartor ? query.append(", ADDRESS =:address") : query.append(" ADDRESS =:address");
        mapSqlParameterSource.addValue("address", address);
    }

    query.append(" WHERE ID  =:id");
    namedParameterJdbcTemplate.update(query.toString(), mapSqlParameterSource);
}

Please suggest best practices to target such use case. Thanks in advance.

3
  • So, you want to update only those columns in a table that are currently not null for a given id? Is that what you mean? Commented Apr 13, 2018 at 6:51
  • @KaushikNayak Yes.. Commented Apr 13, 2018 at 7:09
  • To "build an update query" in oracle, especially if we don't know the column names, would require dynamic sql. But if you just want to run an update statement, the method in the answers here will definitely be efficient. Commented Apr 13, 2018 at 7:55

2 Answers 2

1

This would be my first choice of implementation for that requirement. It's small, it's easily understood and it will perform just as well for the vast majority of applications.

The coalesce expression returns the first non-null value. In this case it returns the new value (if there is one), else it returns the existing value from the column.

update person
   set first_name  = coalesce(:firstName,  first_name)
      ,middle_name = coalesce(:middleName, middle_name)
      ,last_name   = coalesce(:lastName,   last_name)
      ,address     = coalesce(:address,    address)
 where id = :id
Sign up to request clarification or add additional context in comments.

Comments

0

You can build an SQL command as follows:

UPDATE <your table>
  SET FirstName = NVL(PassedFirstName , FirstName) ,
      MidName   = NVL(PassedMidName   , MidName  ) , 
      LastName  = NVL(PassedLastName  , LastName ) ...
 WHERE...

which means, for any passed null value, update the record with the original value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.