Update query if statement for Oracle

2020-02-11 01:51发布

问题:

I need to update newly created column in my oracle table. To do so I need to use existing values in row to decide how to populate this column, I am getting error:

java.lang.NullPointerException -> See Debug Output for details

This is my query:

UPDATE
    SCHEMA_NAME.TABLE_NAME
SET
    OCO= IF CO= 'Y' AND COM='Y' THEN 
{
    'Y'
} ELSE
{
    'N'
}
END IF;

Any suggestions on syntax?

回答1:

You could use CASE expression in the SET clause.

For example,

UPDATE table
SET schema.column =  CASE
                        WHEN CO= 'Y' AND COM='Y' THEN
                          'Y'
                        ELSE
                          'N'
                     END