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.
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;
try the following
UPDATE SAMPLE_TAB1 t
SET t.sample_column1 = NVL( (SELECT sample_column2 FROM ...), 0)
WHERE t.sample_column1 is not null
;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With