I'm having a lot of trouble trying to make a trigger on Oracle SQL...
Here is my problem :
- I have a table X that contains values 0-10 and a table Y that contains the Average of this values for a group of X IDs. So I have to make a trigger that recalculate the average of the ID I'm inserting deleting or updating.
What basically im doing and failling... :
CREATE or REPLACE trigger Update_Average
after insert or update or delete on X
DECLARE
holdID X.ID%type;
Avrg Y.Average%type;
BEGIN
holdID := X.ID;
select avg(value)
into Avrg
from X
where X.id = holdID;
update Y
set Average = Avrg
where Y.id = holdID;
END;
I think you can just do:
EDIT:
Oracle is fussy about updating tables that are being modified in a trigger. Personally, I think I would avoid this problem by storing two values in
Y
-- the sum and the count. Then, the trigger would look like:Note: for the ease of putting this in one statement, this checks the values for
NULL
, rather than usingif DELETING
. I'm pretty sure Oracle returnsNULL
for things like the:NEW
record in a delete trigger.