I am trying to implement a BEFORE trigger, which updates a field to current date if another field is updated. Even though the updated value gets assigned to NEW.zuletzt, whenever I call Select * from hoert, I still see the older zuletzt value. Here is the trigger function:
CREATE OR REPLACE FUNCTION changedAnzhoert() RETURNS TRIGGER AS $$
DECLARE
currDate DATE:=CURRENT_DATE;
BEGIN
IF OLD.anzahl<NEW.anzahl THEN
NEW.zuletzt:=currDate;
raise notice 'Neues Datum: %', NEW.zuletzt;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
And the trigger:
CREATE TRIGGER trhoertChange BEFORE UPDATE ON hoert
FOR EACH ROW EXECUTE PROCEDURE changedAnzhoert();
When I run:
UPDATE hoert SET anzahl=anzahl+1 WHERE id=1;
I see:
NOTICE: Neues Datum: 2019-01-03
UPDATE 1