I am currently working on a project to simulate a Nursing Home database. So what I want is if a new resident is entered into the database, the trigger will fire and loop through the number of rooms until it finds a room where Occupant has a null value. The trigger should then set the new resid.id as the occupant value and the room_num as the room_num value on the new resident row. Of course if the for loop doesn't find a a vacant room it should output a message saying so.
I apologise for my sloopy code but I've been messing around with this one for a while and haven't been getting any results. At the moment my current error is
Error at line 6: PL/SQL: ORA-00923: FROM keyword not found where expected
Would really appreciate any help you lovely people could offer !
Create OR REPLACE TRIGGER Room_Assign
BEFORE INSERT ON Residents
FOR EACH ROW
BEGIN
FOR Room_Num in 1..MAX(Room_Num) loop
if Rooms.Occupant = NULL
THEN
update Rooms Set Occupant = new.Resid_ID where Occupant = NULL;
SELECT Rooms.Room_Num INTO Residents.Room_Num;
Exit;
END IF;
END loop;
IF new.Resid_ID = NULL
DBMS_OUTPUT.PUT_LINE("There are no vacant rooms.")
END;
/