【问题标题】:"If" in a trigger comparing two columns from 2 different tables- error触发器中的“如果”比较来自 2 个不同表的两列 - 错误
【发布时间】:2021-03-13 17:07:22
【问题描述】:

我尝试在更新表“测试”时创建触发器,以确保列中的值不大于来自不同表的另一个值。但我在 Oracle Apex 上收到此错误:ORA-24344: success with compilation error

'test' 是一个表,'chestionar' 是第二个表,所以当我在 'punctaj' 中插入一个大于 'punctaj_max' 的值时,我想启动该错误。并且两个表的 id 必须相同。我应该修改什么?

这是我的代码:

CREATE OR REPLACE trigger trg_a
BEFORE UPDATE on test 
begin
if test.punctaj > chestionar.punctaj_max and test.id=chestionar.id then 
 raise_application_error(234,'error, the value is grater than maximum of that id');
end if;
end;

【问题讨论】:

  • if test.punctaj > chestionar.punctaj_max 你觉得,服务器应该从testchestionar 表的哪一行取值?使用 SELECT INTO 然后检查输出。

标签: sql oracle triggers sql-update oracle-apex


【解决方案1】:

我认为你想要的逻辑是:

create or replace trigger trg_a
before update on test
for each row
declare 
    p_punctaj_max chestionar.punctaj_max%type;
begin
    select punctaj_max into p_punctaj_max from chestionar c where c.id = :new.id;
    if :new.punctaj > p_punctaj_max then 
        raise_application_error(234, 'error, the value is grater than maximum of that id');
    end if;
end;
/

这个想法是为test中正在更新的行的id恢复表chestionarpunctaj_max的值(注意,这隐含地假设@中不能有多个匹配的行987654326@)。然后我们可以将其与正在更新的值进行比较,并在需要时引发错误。

【讨论】:

    【解决方案2】:

    您的代码中有三个(我认为)问题:

    • 您也应该在插入时应用触发器。
    • 您需要查询表chesionar,然后比较值。
    • raise_application_error 中的错误编号应为负数,介于 -20999 和 -20000 之间。

    所以,我会重写你的代码如下:

    create or replace trigger trg_a
    before update or insert on test
    for each row
    declare 
        lv_cnt number := 0;
    begin
        select count(1) into lv_cnt 
          from chestionar c 
         where c.id = :new.id
           and :new.punctaj > c.punctaj_max;
    
        if lv_cnt > 0 then 
            raise_application_error(-20234, 'error, the value is grater than maximum of that id');
        end if;
    end;
    /
    

    【讨论】:

      猜你喜欢
      • 2019-04-24
      • 1970-01-01
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-22
      相关资源
      最近更新 更多