DatabaseProgramming: ChangeFlightStatus.sql

File ChangeFlightStatus.sql, 853 bytes (added by 231044, 6 days ago)
Line 
1create or replace procedure ChangeFlightStatus(
2 FlightID_P int,
3 StatusID_P int
4) as $$
5declare
6 CurrentStatusID int;
7begin
8 select statusid
9 into CurrentStatusID
10 from flight
11 where id = FlightID_P;
12
13 if not FOUND then
14 raise exception 'Flight does not exist!';
15 end if;
16
17 if not exists(select * from flightstatus where id = StatusID_P) then
18 raise exception 'Status does not exist!';
19 end if;
20
21 if (select name from flightstatus where id = CurrentStatusID) = 'Arrived' then
22 raise exception 'Flight has already arrived!';
23 end if;
24
25 if (select name from flightstatus where id = CurrentStatusID) = 'Cancelled' then
26 raise exception 'Flight has been canceled!';
27 end if;
28
29 update flight
30 set statusid = StatusID_P
31 where flight.id = FlightID_P;
32end;
33$$ language plpgsql;