Replace a column values from 'male' to 'female' and 'female' to 'male'

oracle, oracle10g, plsql, sql

Solution

update Table1 set "col" = (case "col" when 'male' then 'female'
else 'male' end);

fiddle

Problem

I have one table, gender, in which only two entries are there, 'male' and 'female' number of rows with these two entries only. Now I want to write a procedure which replaces these two values and the output should be as below, ``` Input Output sex sex ------------------------ male female female male female male male female ``` I am creating a procedure, which gives me an error. I am using a cursor to fetch more than one row. I know that we can do it by using only one update statement, but I want to try like this. ``` declare v_gen gender%rowtype; cursor cur_gender is select * from gender; begin open cur_gender; loop fetch cur_gender into v_gen; select * from gender; if v_gen='male' then update gender set sex='female'; else update gender set sex='male'; end if; exit when v_gen%notfound; end loop; close cur_gender; end; ```

Original source