【问题标题】:Unexpected NULL in multi-column correlated update多列相关更新中的意外 NULL
【发布时间】:2020-10-13 16:53:29
【问题描述】:

我想运行这种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

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

【问题讨论】:

  • 似乎foo.customer.customer_name 不是空列。并且您的 select 语句未匹配任何结果或其 tmp.name is null

标签: oracle select oracle11g sql-update subquery


【解决方案1】:

大概是目标表中有一些行在子查询中没有匹配到。

您可以通过添加一个过滤掉“不匹配”行的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
);

【讨论】:

  • 啊...我的误解是假设相关子查询会影响外部查询中的匹配行。但它只是确定要设置的值,DELETE 的行过滤器仍然是外部 WHERE 的责任。
猜你喜欢
  • 2015-12-04
  • 2017-07-25
  • 1970-01-01
  • 1970-01-01
  • 2019-10-16
  • 2015-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多