为什么这个 AS400 DB2 select 案例出人意料地解析为空?

Why is this AS400 DB2 select case surprisingly resolving to null?

我在查询中有以下 case 表达式,在 per_pack_qty 列上产生 null:

select
    global_override.SHIS8,
    case    
        when sbsize = 8 and 
             global_override.SHIS8 is not null and 
              trim(global_override.SHIS8) not in ('', '0') 
        then global_override.SHIS8
        else sbsizu
    end as per_pack_qty

from 
    order s
    left join order_override on order_override.orfile = s.sbfile
    left join global_override on global_override.shfile = s.sbfile

where
    sbfile in (5859480, 5859490)
    and SBBX0 = 343
limit 1

运行 产生 null 的 per_pack_qty 和“0”的 SHIS8。

但是,如果我在 global_override.SHIS8 周围放置一个连接,例如:

[...] then concat(global_override.SHIS8, '') [...]

那么案例解析为sbsizu

作为 then 子表达式的 null 值是否会以某种方式导致整个 case 表达式的计算结果为 null? AS400 DB2 是否对 case 表达式上产生相同类型的所有分支挑剔?为什么 concat wrapper 使它起作用?

据推测,这是你的问题:

where sbfile in (5859480, 5859490) and SBBX0 = 343
limit 1

limit 1 表明查询的其余部分将 return 多行。没有order by。因此,被 returned 的特定行是不确定的。它甚至可以在同一硬件上对同一数据多次执行同一查询时发生变化。

因此,我认为您的问题是在您 运行.

的查询中 return 编辑了不同的行

是的。 AS400 (IBM i) 期望 case 表达式上的所有分支产生相同的类型。结果的类型由第一个分支决定。它 returns null 当结果的数据类型与第一个分支的数据类型不同时。通过使用 concat 包装器,结果类型更改为字符串。