多列更新

时间:2014-03-06 12:00:06

标签: oracle

我必须在一个过程中执行以下更新行。我可以在一行中更新这个。这里只有一张桌子。

    UPDATE magic_table SET first_col=0,last_col=my_value where first_col is not null;
    UPDATE magic_table SET second_col=0,last_col=my_value where second_col is not null;
    UPDATE magic_table SET third_col=null,last_col=my_value where third_col is not null;

如果是这样,它会给我们带来任何性能改进吗?

2 个答案:

答案 0 :(得分:2)

update magig_table
    set first_col  = nvl2(first_col , 0, first_col ),
        second_col = nvl2(second_col, 0, second_col),
        third_col  = nvl2(third_col , 0, third_col ),
        last_col   = my_value
  where first_col    is not null or
        second_col   is not null or
        third_col    is not null;

答案 1 :(得分:0)

试试这个:

update magig_table
set first_col = case when first_col is not null then 0 end,
    second_col = case when second_col is not null then 0 end,
    third_col = null,
    last_col = my_value
where coalesce(first_col,second_col,third_col) is not null;

这种方式比nvl2更有效。第三列无论如何都是null,所以不需要检查它。