6

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.

2 Answers 2

12

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;
Sign up to request clarification or add additional context in comments.

Comments

1

try the following

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

1 Comment

Hi what about the SAMPLE_TAB3 related logic then

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.