根据 AS400 SQL 中的不同条件更新 SQL 中的相同字段

Update same field in SQL based on different criteria in AS400 SQL

最近提出了一个问题,即是否可以根据 2 个不同的条件对文件中的同一字段执行更新。我们在一天结束时对 运行 2 个不同的 SQL 引入了一个步骤,因此最初的问题已经得到缓解。但是,就我自己对这个主题的了解,我很好奇是否可以做一个类似于下面的 SQL 语句?

UPDATE MYFILE 
SET FIELD1 = 'ABC' where FIELD2 = '123' and FIELD3 = '456',
SET FIELD1 = 'XYZ' where FIELD2 = '789' and FIELD3 = '123'

是否可以在一个语句中进行类似的操作,或者您是否必须像我们最终做的那样将其分解为 2 个语句?

提前致谢!

您可以在 update 语句中使用 case 表达式:

update myfile
set field1 = case
    when field2 = '123' and field3 = '456' then 'abc'
    when field2 = '789' and field3 = '123' then 'xyz'
end
where 
    (field2 = '123' and field3 = '456')
    or (field2 = '789' and field3 = '123')