2013-08-30 72 views
1

我在Oracle中構建條件更新語句時遇到問題。對於易懂,我會簡化問題,基本上我的更新語句應該是這樣的:Oracle中的條件更新聲明

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

***SOME LOGIC***部分應該是這樣的:(請考慮這只是一個僞代碼)

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; 

任何關於這個問題的想法都會受到歡迎。謝謝。

回答

7

試試下面的代碼

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; 
+0

感謝它爲我............ 。 – Dimuthu

1

嘗試以下

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

喜怎麼樣SAMPLE_TAB3相關的邏輯則 – Dimuthu