Oracle中的条件更新语句

时间:2013-08-30 15:43:54

标签: sql oracle sql-update

我在 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;

欢迎任何关于这个问题的想法。谢谢。

2 个答案:

答案 0 :(得分:10)

尝试以下代码

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;

答案 1 :(得分:1)

尝试以下

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