2015-12-10 71 views
1

我需要帮助建立了以下标准的SQL查询单个SQL查询更新多个列:符合条件

有Oracle数据库3列,我需要更新这些列的一个检查,如果第1为空然后更新并且不触及其他两列,如果1st不为空,则更新2nd并且不更新第3列否则如果第3列为空更新第3列。

,我可以构建查询是:

update temp set 
    flg1 = 
     case flg1 
      when null then 'Y' else flg1 end, 
    flg2 = 
     case flg2 
      when null then 'Y' else flg2 end, 
    flg3 = 
     case flg3 
     when null then 'Y' else flg3 end, 
where id = 132 

我知道上面的查询是我想要的,需要帮助不同..

+0

什么错误你收到 – Sachu

+0

它不是错误,查询不正确,我需要建立查询的帮助。 –

+0

如果flag1和flag2不为空,那么flag3会更新? – Sachu

回答

1

试试这个:

update temp set 
     flg1 = 
      case when flag1 is null then 'Y' else flg1 end, 
     flg2 = 
      case when flag1 is not null and flag2 is null then 'Y' else flg2 end, 
     flg3 = 
      case when flag1 is not null and flag2 is not null and flag3 is null then 'Y' else flg3 end 
where id = 132 
+0

是的,这是我想要的,谢谢Muazzam :) –

+0

不,谢谢,只是祈祷。 –

0

你应该使用neste情况条件来达到您的要求 只需尝试下面的代码

update temp set 
      flg1 = 
       case when flag1 is null then 'Y' else flg1 end, 
      flg2 = 
       case when flag1 is not null 
        then case flag2 is null then 'Y' else flg2 end 
       end, 
      flg3 = 
       case when flag1 is not null 
       then case flag2 is not null 
       then case flag3 is null then 'Y' 
       else flg3 end 
       end 
       end 
    where id = 132 
0

也许你想要这样的事情?

UPDATE temp SET 
    flg1 = 
     CASE WHEN flg1 IS NULL THEN 'Y' ELSE flg1 END, 
    flg2 = 
     CASE WHEN flg1 IS NOT NULL AND flg2 IS NULL THEN 'Y' ELSE flg2 END, 
    flg3 = 
     CASE WHEN flg1 IS NOT NULL AND flg2 IS NOT NULL AND flg3 IS NULL THEN 'Y' ELSE flg3 END 

WHERE id = 132