Oracle Apex 触发器问题

问题描述 投票:0回答:1
Error at line 3: PLS-00103: Encountered the symbol "" when expecting one of the following: . ( * @ % & = - + ; < / > at in is mod remainder not rem <an exponent (**)> <> or != or ~= >= <= <> and or like like2 like4 likec between || indicator multiset member submultiset

我希望代码能够自动更新我创建的录取状态表上的矩阵号。

create or replace trigger "TRIG_MATRIC"
after 
insert or update of STUDENT_ID, YEAR_ADMITTED, Matric_Number on "ADMISSION_STATUS"
for each row
begin
    :OLD.Student_id := :New.Student_id
    :OLD.Year_Admitted := :New.Year_Admitted
    :NEW.MATRIC_NUMBER := CONCAT(:New.Year_admitted,"/",:New.Student_id)
    Insert into Admission_Status(Matric_Number)
    values(:NEW.MATRIC_NUMBER)
end;
/
oracle triggers
1个回答
0
投票
  • 您无法修改
    AFTER
    触发器中的值,您需要
    BEFORE
    触发器。
  • 您需要在每个语句的末尾使用语句终止符
    ;
  • 您无法修改
    :OLD
    值。
  • CONCAT
    仅接受两个参数。
  • 您不想插入触发器所在的同一个表,否则您的代码将进入无限循环。

它可以简单地是:

create or replace trigger TRIG_MATRIC
  BEFORE insert or update of STUDENT_ID, YEAR_ADMITTED, Matric_Number on ADMISSION_STATUS
  for each row
begin
    :NEW.MATRIC_NUMBER := :New.Year_admitted || '/' || :New.Student_id;
end;
/

但是,请勿使用触发器。如果您的 Oracle 版本支持,则使用虚拟列:

CREATE TABLE admission_status (
  student_id    NUMBER(8,0),
  year_admitted NUMBER(4,0),
  matric_number VARCHAR2(13) GENERATED ALWAYS AS (
    CAST(
      TO_CHAR(year_admitted, 'YYYY') || '/' || TO_CHAR(student_id, 'FM99999990')
      AS VARCHAR2(13)
    )
  )
);
© www.soinside.com 2019 - 2024. All rights reserved.