使用新插入触发创建触发器

时间:2014-05-25 07:10:25

标签: sql oracle

我想在将新影片插入FILM表时创建一个触发器。

  • 如果一部电影有SPECIAL_FEATURES'预告片'我想增加RENTAL_RATE 10c,
  • 如果一部电影有SPECIAL_FEATURES'评论',我想增加RENTAL_RATE 20c,
  • 如果电影有SPECIAL_FEATURES'已删除的场景',我想增加RENTAL_RATE 20c,
  • 如果一部电影有SPECIAL_FEATURES'幕后花絮'我想减少RENTAL_RATE 20c。

我所做的是,

    CREATE OR REPLACE TRIGGER RENTALRATE_TRIGGER
BEFORE INSERT OR UPDATE ON FILM
REFERENCING OLD AS OLD NEW AS NEW
FOR EACH ROW
BEGIN
    IF :NEW.SPECIAL_FEATURES LIKE '%Trailers%' THEN
        UPDATE FILM SET RENTAL_RATE = :NEW.RENTAL_RATE + .10;
    IF :NEW.SPECIAL_FEATURES LIKE '%Commentaries%' THEN
        UPDATE FILM SET RENTAL_RATE = :NEW.RENTAL_RATE + .50;
    IF :NEW.SPECIAL_FEATURES LIKE '%Deleted Scenes%' THEN
        UPDATE FILM SET RENTAL_RATE = :NEW.RENTAL_RATE + .20;
    IF :NEW.SPECIAL_FEATURES LIKE '%Behind the Scenes%' THEN
        UPDATE FILM SET RENTAL_RATE = :NEW.RENTAL_RATE - .20;
    END IF;
END RENTALRATE_TRIGGER;
/

创建触发器后,我收到此错误。

LINE/COL ERROR
-------- -----------------------------------------------------------------
11/5     PLS-00103: Encountered the symbol "RENTALRATE_TRIGGER" when
         expecting one of the following:
         if

SPECIAL_FEATURES列可以是这样的,“预告片,幕后花絮”

感谢。

1 个答案:

答案 0 :(得分:0)

您不应该在触发器中更新同一个表:

    CREATE OR REPLACE TRIGGER RENTALRATE_TRIGGER
        BEFORE INSERT OR UPDATE
        ON FILM
        REFERENCING OLD AS OLD NEW AS NEW
        FOR EACH ROW
    BEGIN
        IF :NEW.SPECIAL_FEATURES LIKE '%Trailers%'
        THEN
            :NEW.RENTAL_RATE := :NEW.RENTAL_RATE + .10;
        ELSIF :NEW.SPECIAL_FEATURES LIKE '%Commentaries%'
        THEN
            :NEW.RENTAL_RATE := :NEW.RENTAL_RATE + .50;
        ELSIF :NEW.SPECIAL_FEATURES LIKE '%Deleted Scenes%'
        THEN
            :NEW.RENTAL_RATE := :NEW.RENTAL_RATE + .20;
        ELSIF :NEW.SPECIAL_FEATURES LIKE '%Behind the Scenes%'
        THEN
            :NEW.RENTAL_RATE := :NEW.RENTAL_RATE - .20;
        END IF;
    END RENTALRATE_TRIGGER;