I have the following column:
...
some_column NUMBER(1, 0) DEFAULT NULL NULL,
...
which is used to hold a nullable Integer value.
Now I have a row with that column filled. I'm performing a patch using Spring's JdbcTemplate, meaning that I want to update the column only if the new value is not null:
UPDATE my_table SET some_column = COALESCE(?, some_column) WHERE...
This fails with:
Caused by: java.sql.SQLSyntaxErrorException: ORA-00932: inconsistent datatypes: expected CHAR got NUMBER
when I pass a Java null to ?. This means that COALESCE is not happy, since I passed two different types into it. My initial assumption is that Spring/JdbcTemplate somehow does not pass SQL null to the database, because a direct update to the database with:
UPDATE my_table SET some_column = COALESCE(null, some_column) WHERE...
works OK. However, when I replace the query with:
UPDATE my_table SET some_column = NVL(?, some_column) WHERE...
I get what I want with JdbcTemplate. What's happening, where's the difference?
Update:
Java code I'm using is as follows:
I have:
public class MyClass {
private MyEnum enum;
// Getters and setters
}
and MyEnum:
public enum MyEnum implements Serializable {
SOME_VAL (0),
SOME_OTHER_VAL (1),
...
private final int status;
MyEnum (int status) {
this.status = status;
}
public int getStatus() {
return status;
}
getJdbcTemplate().update("UPDATE my_table SET some_column = COALESCE(?, some_column) WHERE...", myClass.getMyEnum() == null ? null : myClass.getMyEnum().getStatus());
From the
NVLdocumentation:From the
COALESCEdocumentation:You will notice that
NVLexplicitly states it will perform an implicit conversion so thatexpr2is the same data type asexpr1whereasCOALESCE(although slightly confusingly worded) does not mention performing an implicit conversion (except for numeric data types) and will expect that all expressions in its argument list are the same data type.Your query for
NVLis effectively converted to:but your
COALESCEfunction is:and
expr1andexpr2have different data types and the query raises an exception.Assuming that there are no other columns being modified, you do not need to perform an
UPDATEif the value isNULLas it is not going to change anything and could re-write your Java code as: