Oracle触发器 - 检查现有数据

时间:2011-12-02 11:18:48

标签: sql oracle triggers

我想检查一下输入的时间,看看它是否已经存储了任何其他约会的2小时内。

我的表是test(id,number& testdate,timestamp)

我有一个触发器,用于检查新时间是否在我指定的时间的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;

是否可以替换这段时间我从数据库中输入一个时间列表?

在Michał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;

1 个答案:

答案 0 :(得分:1)

create or replace trigger "TEST_T1"
BEFORE
insert on "TEST"
for each row
when (new.testdate is not null)
begin
    declare qty integer := 0;
    begin
        select count(*) into qty from test where testdate between (:new.testdate - 2/24) and (:new.testdate + 2/24) and rownum = 1;
        if qty > 0 then
            raise_application_error(-20634, 'This time clashes with another event.');
        end if;
    end;
end;

PS。请注意,我不是Oracle开发人员,所以可能会有错误,但我认为这是要走的路: - )