diff --git a/db/procedures/add_person_to_lease.sql b/db/procedures/add_person_to_lease.sql new file mode 100644 index 0000000..83ed58c --- /dev/null +++ b/db/procedures/add_person_to_lease.sql @@ -0,0 +1,22 @@ +CREATE OR REPLACE PROCEDURE add_person_to_lease ( + pid IN person.id%type, + lid IN lease.id%type, + success OUT number +) IS + valid number; +BEGIN + -- Check if lease is valid + SELECT COUNT(*) INTO valid + FROM lease join person_on_lease on lease.id = person_on_lease.person_id + WHERE + lease.id = lid AND person_id <> pid AND + CURRENT_TIMESTAMP >= ADD_MONTHS(start_date, term_length); + + IF valid <> 0 THEN + success := 1; + return; + END IF; + + INSERT INTO person_on_lease VALUES(lid, pid); + success := 0; +END;