I am working on PostgreSQL trigger and I am new to it, I want to develop a Postgres trigger that on insert of the date of birth (DOB), calculates the age, and inserts that date with the corresponding DOB field in the same table.
below is my work:
create or replace function Age()
returns trigger as $body$
declare
dob1 date;
age1 integer;
BEGIN
select dob into dob1 from dateage;
SELECT date_part('year', AGE(dob)) as "age", * FROM dateage into age1;
update dateage set age=age1;
return new;
END; $body$ language plpgsql;
And here goes my function call
create trigger datea after insert on dateage
for each row execute procedure age();
My table goes like this: name DATEAGE with attributes ID| DOB | AGE
When I insert into my table values, The first row is calculated fine, but the age in subsequent rows is just copied from the first row's age value.
thankyou for any contribution in advance.