简体   繁体   English

触发器中的“如果”比较来自 2 个不同表的两列 - 错误

[英]"If" in a trigger comparing two columns from 2 different tables- error

Im trying to create a trigger when updating table 'test' to make sure a value in a column is not greater than another one from a different table.我试图在更新表“测试”时创建触发器以确保列中的值不大于来自不同表的另一个值。 But I get this error on Oracle Apex: ORA-24344: success with compilation error但我在 Oracle Apex 上收到此错误: ORA-24344:编译错误成功

'test' is a table and 'chestionar' is a second one, so I want to launch that error when I insert a value in 'punctaj' which is greater than the 'punctaj_max'. 'test' 是一个表,'chestionar' 是第二个表,所以当我在 'punctaj' 中插入一个大于 'punctaj_max' 的值时,我想启动该错误。 And the id of the both tables must be the same.并且两个表的 id 必须相同。 What should I modify?我应该修改什么?

here is my code:这是我的代码:

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;

I think the logic you want is:我认为你想要的逻辑是:

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;
/

The idea is to recover the value of punctaj_max in table chestionar for the id of the row that is being updated in test (note that this implicitely assumes that there cannot be multiple matching rows in chestionar ).这个想法是恢复表chestionarpunctaj_max的值,以获取在test中更新的行的id (请注意,这隐含地假设chestionar中不能有多个匹配行)。 We can then compare that to the value being updated, and raise the error if needed.然后我们可以将其与正在更新的值进行比较,并在需要时引发错误。

You have three (I think) issue in your code:您的代码中有三个(我认为)问题:

  • You should apply trigger at time of insert also.您还应该在插入时应用触发器。
  • you need to query the table chestionar and then compare the value.您需要查询表 chestionar,然后比较值。
  • the error number in raise_application_error should be negative and between -20999 and -20000. raise_application_error 中的错误号应该是负数,在-20999 和-20000 之间。

So, I would rewrite your code as follows:所以,我会重写你的代码如下:

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;
/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM