多列相关更新中的意外 NULL

Unexpected NULL in multi-column correlated update

我想要运行一个multi-column correlated update这样的:

UPDATE t1 t1_alias
SET (table_name, tablespace_name) = (
    SELECT table_name, tablespace_name
    FROM t2 t2_alias
    WHERE t1_alias.table_name = t2_alias.table_name
);

但我的尝试:

update customer up
set (customer_name, account, active) = (
    select tmp.name, tmp.account, case when tmp.active = 'Yes' then 1 else 0 end
    from customer_temp tmp
    where up.agent = substr(tmp.agent, -4) and up.customer_code = tmp.code
);

...抛出:

ORA-01407: cannot update ("FOO"."CUSTOMER"."CUSTOMER_NAME") to NULL

源 table customer_temp 没有空值,所以我一定是匹配错误。我的错误或误解是什么?

据推测,目标 table 中有一些行在子查询中没有匹配项。

您可以通过添加过滤掉“不匹配”行的 exists 条件来避免这种情况:

update customer up
set (customer_name, account, active) = (
    select tmp.name, tmp.account, case when tmp.active = 'Yes' then 1 else 0 end
    from customer_temp tmp
    where up.agent = substr(tmp.agent, -4) and up.customer_code = tmp.code
)
where exists (
    select 1
    from customer_temp tmp
    where up.agent = substr(tmp.agent, -4) and up.customer_code = tmp.code
);