Oracle sql,使用CASE更新多行

时间:2017-10-25 12:16:55

标签: sql oracle sql-update

我正在尝试使用CASE更新多行。

下面的代码
UPDATE GRIDCOLUMNS  
SET TYPE= CASE  
  WHEN (Fieldname = 'AccountNum' AND ID = 19337) THEN 6
  WHEN (Fieldname = 'AccountNum' AND ID = 19339) THEN 6 
  WHEN (Fieldname = 'AccountNum' AND ID = 19380) THEN 6
  WHEN (Fieldname = 'AccountNum' AND ID = 19419) THEN 6
  END;

当我运行它时,我收到以下错误:

Error starting at line 1 in command:
UPDATE GRIDCOLUMNS  
SET TYPE= CASE  
  WHEN (Fieldname = 'AccountNum' AND ID = 19337) THEN 6
  WHEN (Fieldname = 'AccountNum' AND ID = 19339) THEN 6 
  WHEN (Fieldname = 'AccountNum' AND ID = 19380) THEN 6
  WHEN (Fieldname = 'AccountNum' AND ID = 19419) THEN 6
  END
Error report:
SQL Error: ORA-01407: cannot update ("WEB"."GRIDCOLUMNS"."TYPE") to NULL
01407. 00000 -  "cannot update (%s) to NULL"
*Cause:    
*Action:

可能是什么问题?

1 个答案:

答案 0 :(得分:2)

不要使用case。代替:

UPDATE GRIDCOLUMNS  
    SET TYPE = 6
    WHERE Fieldname = 'AccountNum' AND ID IN (19337, 19339, 19380, 19419);

这样效率更高。如果您确实需要使用CASE,那么您需要ELSE

UPDATE GRIDCOLUMNS  
    SET TYPE = (CASE WHEN Fieldname = 'AccountNum' AND ID IN (19337, 19339, 19380, 19419) THEN 6
                     ELSE TYPE
                END);

否则,不匹配的行将设置为NULL。但实际上,请使用WHERE