检查SQL CASE语句中是否存在

nea*_*ime 2 sql database db2

我试图根据表中a的不同列是否在表的一组结果中更新表中的列b.目前的变化:

update a
set a.field1 =
 case
 when exists (
   select b.field2
   from b
   where b.field2 = a.field2
 )
 then 'FOO'
 else 'BAR'
 end
Run Code Online (Sandbox Code Playgroud)

没有跑.有关如何为DB2数据库执行此操作的任何想法?

编辑:感谢您的回答,我能做的最好

update a set field1 = 'FOO' where field2 in (select field2 from b);

update a set field1 = 'BAR' where field2 not in (select field2 from b);
Run Code Online (Sandbox Code Playgroud)

但我会保持这个开放,以防有人可以在顶部找到有效的代码版本.

Lyn*_*ffy 6

我在DB2 for iSeries框上工作.试试这个:

update a
set a.field1 =
 Coalesce( ( select 'FOO'
             from b
             where b.field2 = a.field2 ),
                      'BAR' )
Run Code Online (Sandbox Code Playgroud)

Coalesce() 是一个返回列表中第一个非NULL的函数.