Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional update statement in Oracle

I have a problem with building a conditional update statement in Oracle. For understandability, I'll simplify the problem and basically my update statement should look like this:

UPDATE SAMPLE_TAB1 t 
   SET t.sample_column1 =NVL(t.sample_column1, **SOME LOGIC**);

The ***SOME LOGIC*** part should look like this: (Please consider this is just a pseudo code)

IF ((SELECT sample_column2 FROM SAMPLE_TAB2 
       WHERE sample_column2= sample_value2
       AND sample_column3  = sample_value3 )='FALSE' THEN 
   t.sample_column1 =0;
ELSE

   t.sample_column1 =(SELECT sample_column1 FROM SAMPLE_TAB3 
                         WHERE sample_column4= sample_value4
                         AND sample_column5  = sample_value5 )

END IF;

Any kind of thoughts on this problem would be welcome. Thank you.

like image 701
Dimuthu Avatar asked Aug 30 '13 15:08

Dimuthu


2 Answers

Try the following code

UPDATE SAMPLE_TAB1 t 
    SET t.sample_column1 = (
      case when ((SELECT sample_column2 FROM SAMPLE_TAB2 
                          WHERE sample_column2= sample_value2 
                          AND sample_column3  = sample_value3 ) = 'FALSE' )
            then 0
      else
          (SELECT sample_column1 FROM SAMPLE_TAB3 
                         WHERE sample_column4= sample_value4
                         AND sample_column5  = sample_value5 )
      end

    )
  WHERE t.sample_column1 is not null;
like image 185
Jasti Avatar answered Sep 30 '22 10:09

Jasti


try the following

UPDATE SAMPLE_TAB1 t 
   SET t.sample_column1 = NVL( (SELECT sample_column2  FROM ...), 0)
WHERE t.sample_column1 is not null
;
like image 20
schurik Avatar answered Sep 30 '22 08:09

schurik