我想检查输入的时间,以确定是否在已存储的任何其他约会的2小时内。Oracle触发器 - 检查已有数据
我的表测试(ID,数量& testdate,时间戳)
我有检查,看看新的时间是否为2小时的时间内触发我指定
create or replace trigger "TEST_T1"
BEFORE
insert on "TEST"
for each row
when (new.testdate is not null)
begin
if (:new.testdate BETWEEN (to_timestamp('12-AUG-12 02:00:00PM') - interval '120' minute)
AND (to_timestamp('12-AUG-12 02:00:00PM') + interval '120' minute))
then raise_application_error(-20634, 'This time clashes with another event.');
end if;
end;
是有可能取代这次我从数据库中列出的时间列表?
与米哈尔Powaga帮助 - 下面 - 我设法得到这种使用
create or replace trigger "TEST_T1"
BEFORE
insert on "TEST"
for each row
when (new.testdate is not null)
begin
DECLARE l_exists INTEGER;
BEGIN
SELECT COUNT(*) INTO l_exists FROM test WHERE testdate BETWEEN :new.testdate - interval '120' minute AND :new.testdate + interval '120' minute AND ROWNUM = 1;
IF l_exists = 1 THEN raise_application_error(-20634, 'This time clashes with another event.');
END IF;
END;
END;
此触发器不能防止:a)可能会将TESTDATE更改为时间冲突的更新与另一个或者b)同时插入(在单独的事务中),这些插入彼此冲突。一个更简单和更健壮的方法(不使用触发器)可能会将主键约束TESTDATE与检查约束强制为120分钟的倍数。 –