SQL触发器在满足条件时停止更新

编程入门 行业动态 更新时间:2024-10-22 08:16:18
本文介绍了SQL触发器在满足条件时停止更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有3个表:Projects,Components和Suppliers.

我想做的是编写一个触发器,如果​​组件和项目与供应商所在的城市相同,则不允许修改city的值.

What I am trying to do is writing a trigger that doesn't allow the value of city to be modified if the component and the project have the same city as the supplier.

到目前为止我尝试过的事情:

What I have tried so far:

create or replace TRIGGER Supplier_control BEFORE UPDATE of city ON Suppliers BEGIN DECLARE v_counter NUMBER := 0; SELECT COUNT(*) FROM (SELECT * FROM Suppliers s JOIN Projects p ON (s.city=p.city) JOIN Components c ON (c.city=s.city)) INTO v_counter; IF (v_counter != 0) THEN raise_application_error(-20111,'Can't change the city for this supplier!'); END IF; END;

尝试运行此命令后,出现以下错误:

After trying to run this, I am getting the following error:

Error at line 3: PLS-00103: Encountered the symbol "JOIN" when expecting one of the following: ) , with group having intersect minus order start union where connect

请注意,行号是指BEGIN之后的行号!

Please note that the line number refers to the number of the line after BEGIN!

我也尝试过在BEGIN之前编写声明部分,但出现以下错误:

I have also tried writing the declare part before BEGIN, I am getting the following error:

Error at line 3: PL/SQL: SQL Statement ignored

要摆脱这些错误需要做什么?

What needs to be done in order to get rid of these errors?

推荐答案

存在一些语法错误.

  • DECLARE在BEGIN语句之前.
  • INTO在SELECT之后且在FROM之前.
  • 在raise_application_error(-20111,'Can't change the city for this supplier!');,您不能编写Can't,因为第一个单引号将以Can't的引号结束,从而导致字符串在此结束.因此,您应该删除它或执行以下操作:raise_application_error(-20111,'Can''t change the city for this supplier!');
  • DECLARE goes before the BEGIN statement.
  • INTO goes after SELECT and before FROM.
  • At raise_application_error(-20111,'Can't change the city for this supplier!'); you cannot write Can't because the first single quote will end at the quote of Can't causing the string to end there. So you should remove it or do: raise_application_error(-20111,'Can''t change the city for this supplier!');
  • 话虽如此,完整的代码应如下所示:

    With all that being said, the full code should look like:

    CREATE OR REPLACE TRIGGER Supplier_control BEFORE UPDATE of city ON Suppliers DECLARE v_counter NUMBER := 0; BEGIN SELECT COUNT(*) INTO v_counter FROM (SELECT * FROM Suppliers s JOIN Projects p ON s.city=p.city JOIN Components c ON c.city=s.city); IF v_counter != 0 THEN raise_application_error(-20111,'Can''t change the city for this supplier!'); END IF; END;

    希望这会有所帮助.

    更多推荐

    SQL触发器在满足条件时停止更新

    本文发布于:2023-10-24 14:00:42,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1524120.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:触发器   条件   SQL

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!