简体   繁体   中英

How to update a single column with multiple data in oracle 12c

I am updating an oracle table, same column with multiple data. How do i write the code?

I run this code but had error..

I am replacing Boy with Girl, Black with White and Green with Color.

UPDATE BIG
SET
    FNAME = CASE
        WHEN FNAME = 'BOY' THEN 'GIRL'
        WHEN FNAME = 'BLACK' THEN 'WHITE'
        WHEN FNAME = 'GREEN' THEN 'COLOR'
    END;

I need to be able to write a single statement that can update same column with different and multiple data.

You need to handle ELSE (all rows will be updated some using A -> A (identity):

UPDATE BIG
SET
    FNAME = CASE FNAME
        WHEN 'BOY' THEN 'GIRL'
        WHEN 'BLACK' THEN 'WHITE'
        WHEN 'GREEN' THEN 'COLOR'
        ELSE FNAME
    END;

or better filter rows you want to update:

UPDATE BIG
SET
    FNAME = CASE
        WHEN FNAME = 'BOY' THEN 'GIRL'
        WHEN FNAME = 'BLACK' THEN 'WHITE'
        WHEN FNAME = 'GREEN' THEN 'COLOR'
    END;
WHERE FNAME IN('BOY', 'BLACK', 'GREEN');

<=>

UPDATE BIG
SET FNAME = DECODE(FNAME, 'BOY', 'GIRL', 'BLACK', 'WHITE', 'GREEN', 'COLOR')
WHERE FNAME IN('BOY', 'BLACK', 'GREEN');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM