I need compute age and update column of age with trigger after Insert Or Update.
Here is my code
CREATE OR REPLACE function to_age()
RETURNS TRIGGER AS $$
declare ages int;
BEGIN
select date_part('year', age(old.dob)) into ages from employees;
-- date_part('year', age(old.dob));
new.age_of_person := ages;
raise notice 'success(%)', new.age_of_person;
RETURN new;
end;
$$
LANGUAGE plpgsql;
CREATE TRIGGER ages
AFTER INSERT OR UPDATE ON employees
FOR EACH STATEMENT
EXECUTE FUNCTION to_age();
DROP TRIGGER ages on employees;
And here is my table that I need update.
CREATE TABLE employees(
id INT GENERATED ALWAYS AS IDENTITY,
first_name VARCHAR(40) NOT NULL,
last_name VARCHAR(40) NOT NULL,
dob timestamp,
age_of_person int,
PRIMARY KEY(id)
);
I have mistake and I did not find where. I can not compute age, it raised null in the function.