Reputation: 11
I am working on creating a trigger, but still stuck and not sure what is wrong with the code. Any help is appreciated.
The trigger BI_FILM_DESP appends text to the description of every new film inserted into the db. Output should be something like this [description].[rating] : Originally in <original_langauge_id>. Re-released in <language_id>. If rating, language id, or original language is null, the film would use the original description.
CREATE OR REPLACE TRIGGER "BI_FILM_DESP"
AFTER INSERT ON "FILM"
FOR EACH ROW
DECLARE
DESCRIPTION VARCHAR2 (255);
BEGIN
INSERT INTO FILM
(TITLE, DESCRIPTION, LANGUAGE_ID, ORIGINAL_LANGUAGE_ID, RATING) VALUES (:new.TITLE, :new.DESCRIPTION, :new.LANGUAGE_ID, :new.ORIGINAL_LANGUAGE_ID, :new.RATING)
UPDATE FILM
SET DESCRIPTION = DESCRIPTION '. ' RATING ': Originally in ' LANGUAGE_ID '. RE-released in ' ORIGINAL_LANGUAGE_ID
WHERE RATING IS NOT NULL
OR LANGUAGE_ID IS NOT NULL
OR ORIGINAL_LANGUAGE_ID IS NOT NULL;
END;
/
Upvotes: 0
Views: 2114
Reputation: 18705
That is not how a trigger works. DML statements on the table that the trigger is created on are not possible. Instead do something like this:
CREATE OR REPLACE TRIGGER "BI_FILM_DESP" BEFORE
INSERT ON "FILM"
FOR EACH ROW
DECLARE
l_description VARCHAR2(255);
BEGIN
IF ( :new.rating IS NOT NULL OR :new.language_id IS NOT NULL OR :new.original_language_id IS NOT NULL ) THEN
:new.description := :new.description
|| '. '
|| :new.rating
|| ': Originally in '
|| :new.language_id
|| '. RE-released in '
|| :new.original_language_id;
END IF;
END;
/
BEFORE INSERT
trigger is what you want, because you're modifying a column before it is inserted||
symbol.description
to l_description
Upvotes: 1