Here is my code so far:
CREATE OR REPLACE FUNCTION updateDegreeStatus()
RETURNS TRIGGER as $updateDegreeStatus$
BEGIN
IF(new.degreeName = old.degreeName and old.endDate = NULL)
THEN
UPDATE degree
SET old.endDate = CURRENT_DATE
WHERE degree.degreeID = old.degreeID;
END IF;
RETURN NEW;
END;
$updateDegreeStatus$ LANGUAGE plpgsql;
CREATE TRIGGER updateDegreeStatus
BEFORE INSERT ON degree
FOR EACH ROW
EXECUTE PROCEDURE updateDegreeStatus();
What I having trouble with is accessing the current values of the row the trigger is currently on. How my stored procedure should work, is if the new insert has the same degree name as the current row and the current row value for end date is null then do this. But the keyword old is not how you access the current row values. And I have been reading through documentation and I still can not find the answer. If anyone has any input on how to fix this issue, I would love your help.
Here is my current table:
And my insert is this:
INSERT INTO degree(degreeID, degreeName, type, startDate, endDate)
VALUES(3, 'Computer Science with a Concentration in Software Development', 'Concentration', CURRENT_DATE, null)
Now what I want to happen is when the insert command is issued it will fire off my trigger, and will go and check if the New degreeName equals the current row degreeName and if the current row endDate is null. If it is then it will issue the Update on the current row, which just sets the endDate to the CURRENT_Date.