Click here to Skip to main content
15,886,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to write SQL trigger to change the rental_rate of every new film inserted into the database on the basis of the following price chart.

special feature 'trailers' 10c increase special feature 'commentaries' 50c increase special feature 'Deleted Scenes' 20c increase special feature 'Behind the Scenes' 20c decrease

This is what I have so far:

CREATE OR REPLACE TRIGGER CHArraa
AFTER
INSERT ON film
DECLARE special_features varchar2(50);
rental_rate number;
IF (special_features = 'Trailers') THEN
           rental_rate := rental_rate + 0.10;
IF (special_features = 'Commentaries') THEN
           rental_rate:= rental_rate+ 0.50;
IF (special_features = 'Deleted Scenes') THEN
           rental_rate := rental_rate + 0.20;
IF (special_features = 'Behind the Scenes') THEN
          rental_rate := rental_rate - 0.20;
END IF;
END IF;
END IF;
END IF;
END;


Can I get some get help to fix and make this work
Posted

1 solution

Create INSTEAD OF INSERT trigger. Here is script

SQL
CREATE TRIGGER CHArraa ON film
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON
    INSERT INTO dbo.film
    SELECT 
      special_features,
	CASE WHEN special_features = 'Trailers' THEN rental_rate + 0.10 
             WHEN special_features = 'Commentaries' THEN rental_rate+ 0.50
     	     WHEN special_features = 'Deleted Scenes' THEN rental_rate + 0.20
    	     WHEN special_features = 'Behind the Scenes' THEN rental_rate - 0.20
	END  AS rental_rate
    FROM INSERTED
END
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900