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());