Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use DECODE in a UPDATE statement?

Tags:

plsql

Can I use DECODE in a UPDATE statement on the left hand side of SET?

UPDATE temp SET DECODE(update_var, 1, col1, 2, col2) = update_value;

This is giving me error as eual sign ignored..

like image 934
Avi Avatar asked Dec 03 '22 03:12

Avi


1 Answers

How about this? If the update flag is set to 1, col1 gets updated, if set to 2, then col2 gets updated.

UPDATE temp
   SET col1 = DECODE(update_var, 1, update_value, col1),
       col2 = DECODE(update_var, 2, update_value, col2)

Also, as a bonus, it'll handle the possible situation where the update variable is set to something other than one or two!

like image 182
Mark Bowytz Avatar answered Jun 04 '23 08:06

Mark Bowytz