| 1 | create function UpdateLastMaintenance()
|
|---|
| 2 | returns trigger as $$
|
|---|
| 3 | declare
|
|---|
| 4 | LastMaintenanceDate timestamp;
|
|---|
| 5 | begin
|
|---|
| 6 | select m.date into LastMaintenanceDate
|
|---|
| 7 | from maintenance m
|
|---|
| 8 | join airplane a on a.lastmaintenanceid = m.id
|
|---|
| 9 | where a.id = new.airplaneid;
|
|---|
| 10 |
|
|---|
| 11 | if LastMaintenanceDate is null or LastMaintenanceDate <= new.date then
|
|---|
| 12 | update airplane
|
|---|
| 13 | set lastmaintenanceid = new.id
|
|---|
| 14 | where id = new.airplaneid;
|
|---|
| 15 | end if;
|
|---|
| 16 |
|
|---|
| 17 | return new;
|
|---|
| 18 | end;
|
|---|
| 19 | $$ language plpgsql;
|
|---|
| 20 |
|
|---|
| 21 | create trigger UpdateLastMaintenance
|
|---|
| 22 | after insert on maintenance
|
|---|
| 23 | for each row execute function UpdateLastMaintenance(); |
|---|