【问题标题】:Update with Join PL/SQL with on/and condition - it tries to update all rows使用带有 on/and 条件的 Join PL/SQL 进行更新 - 它尝试更新所有行
【发布时间】:2019-11-27 10:20:37
【问题描述】:

我查看了其他答案,但看不到这个特定问题;我想更新一个使用内部代码编号的字段,以确保两个客户相同,并且是特定订单的客户。

当我运行以下代码时,我收到警告“您确定要更新所有记录吗?” - 不,我不想!


UPDATE CustomerTable a
SET a.Text = 'text'
from a
join OrderTable b
on a.Customer = b.Customer
and b.Order = '10';

如何让它更新该客户记录并且仅更新该客户记录?

谢谢!

【问题讨论】:

  • 您使用的是什么 dbms?
  • 您在标题中提到了 PL/SQL,这是 Oracle 用于编写存储过程的语言(您的问题仅包含纯 SQL,但没有 PL/SQL)。但是您不能使用 Oracle,因为该语句对 Oracle 无效并且会导致错误消息。

标签: sql join sql-update


【解决方案1】:

您正在重复update 中的表名。您想将其表述为:

update CustomerTable ct
    set Text = 'text'
from OrderTable ot
where ot.Customer = ct.Customer and ot.Order = '10';

或者,在几乎可以在任何数据库中使用的结构中:

update CustomerTable ct
    set Text = 'text'
where exists (select 1
              from OrderTable ot
              where ot.Customer = ct.Customer and
                    ot.Order = '10'
             );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多