DatabaseProgramming: UpdateAirplaneFlightHours.sql

File UpdateAirplaneFlightHours.sql, 632 bytes (added by 231044, 6 days ago)
Line 
1create function UpdateAirplaneFlightHours()
2returns trigger as $$
3declare
4 StatusID int;
5begin
6 select id
7 into StatusID
8 from flightstatus
9 where name like '%Arrived%';
10
11 if new.statusid = StatusID
12 and (old.statusid is distinct from StatusID) then
13 update airplane
14 set totalflighthours = totalflighthours + extract(epoch from (new.arrival - new.departure)) / 3600::int
15 where id = new.airplaneid;
16 end if;
17
18 return new;
19end;
20$$ language plpgsql;
21
22create trigger UpdateAirplaneFlightHours
23after update of statusid on flight
24for each row execute function UpdateAirplaneFlightHours();