【发布时间】:2023-02-03 14:14:59
【问题描述】:
我试图阻止将记录插入表中以进行调度。如果课程的开始日期介于之前记录的开始日期和结束日期之间,并且该记录与新记录的位置相同,则不应允许。
我编写了以下触发器,它可以编译,但当然会发生变异,因此存在问题。我研究了复合触发器来处理这个问题,但要么无法完成,要么我的理解不好,因为我也无法让它工作。我本以为对于一个复合触发器,我想在 before 语句上做这些事情,但我只得到了错误。
我也在插入/更新之后考虑过,但是在它已经插入之后不适用吗?感觉那是不对的……另外,我相信突变也有同样的问题。
我写的触发器是:
CREATE OR REPLACE TRIGGER PREVENT_INSERTS
before insert or update on tbl_classes
DECLARE
v_count number;
v_start TBL_CLASS_SCHED.start_date%type;
v_end TBL_CLASS_SCHED.end_date%type;
v_half TBL_CLASS_SCHED.day_is_half%type;
BEGIN
select start_date, end_date, day_is_half
into v_start, v_end, v_half
from tbl_classes
where class_id = :NEW.CLASS_ID
and location_id = :NEW.location_id;
select count(*)
into v_count
from TBL_CLASS_SCHED
where :NEW.START_DATE >= (select start_date
from TBL_CLASS_SCHED
where class_id = :NEW.CLASS_ID
and location_id = :NEW.location_id)
and :NEW.START_DATE <= (select end_date
from TBL_CLASS_SCHED
where class_id = :NEW.CLASS_ID
and location_id = :NEW.location_id);
if (v_count = 2) THEN
RAISE_APPLICATION_ERROR(-20001,'You cannot schedule more than 2 classes that are a half day at the same location');
end if;
if (v_count = 1 and :NEW.day_is_half = 1) THEN
if (v_half != 1) THEN
RAISE_APPLICATION_ERROR(-20001,'You cannot schedule a class during another class''s time period of the same type at the same location');
end if;
end if;
EXCEPTION
WHEN NO_DATA_FOUND THEN
null;
END;
end PREVENT_INSERTS ;
也许不能用触发器来完成,我需要通过多种方式来完成?现在我在直接插入或更新之前使用相同的逻辑完成了它,但我想将它作为约束/触发器,以便它始终适用(这样我就可以了解它)。
【问题讨论】:
标签: database oracle plsql triggers constraints