Dimuthu
Dimuthu

Reputation: 150

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.

Upvotes: 6

Views: 29193

Answers (2)

Jasti
Jasti

Reputation: 947

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;

Upvotes: 12

schurik
schurik

Reputation: 7928

try the following

UPDATE SAMPLE_TAB1 t 
   SET t.sample_column1 = NVL( (SELECT sample_column2  FROM ...), 0)
WHERE t.sample_column1 is not null
;

Upvotes: 1

Related Questions