为什么我收到警告:使用编译错误创建触发器?

时间:2017-02-28 01:54:49

标签: triggers oracle-sqldeveloper sqlplus

这是我第一次搞乱SQL * Plus中的触发器,我希望不会超过电影院的最大容量显示空间。

showtimes给出了max_occupancy。这就是我所拥有的:

CREATE OR REPLACE TRIGGER over_capacity
BEFORE INSERT OR UPDATE ON tickets
FOR EACH ROW
WHEN (NEW.SHOWID=OLD.SHOWID)
DECLARE
    nope EXCEPTION;
    ti SYS_REFCURSOR;
    sh SYS_REFCURSOR;
    total INT;
    max_oc INT;
BEGIN
    OPEN sh FOR
         SELECT s.max_occupancy FROM showtimes s WHERE NEW.showid=s.showid;
    FETCH sh INTO max_oc;
    OPEN ti FOR
         SELECT COUNT(t.userid) FROM tickets t WHERE t.showid=NEW.showid;
    FETCH ti INTO total;
    IF total=max_oc THEN
       RAISE nope;
    END IF;
EXCEPTION
    WHEN nope THEN
    raise_application_error (-20500, 'AT CAPACITY');
END;
/

1 个答案:

答案 0 :(得分:0)

你可以使用这样的选择语句:

CREATE OR REPLACE TRIGGER over_capacity
BEFORE INSERT OR UPDATE ON tickets FOR EACH ROW
WHEN (NEW.SHOWID=OLD.SHOWID)
DECLARE
    nope EXCEPTION;
    total INT;
    max_oc INT;
BEGIN
    SELECT max_occupancy INTO total FROM showtimes WHERE showid = :new.showid;
    SELECT COUNT(userid) INTO max_oc FROM tickets WHERE showid=:new.showid;
    IF total=max_oc THEN
       RAISE nope;
    END IF;
EXCEPTION
    WHEN nope THEN
    raise_application_error (-20500, 'AT CAPACITY');
END;
/