I am having some troubles setting a trigger in PostgreSQL. I have a table Employees with columns such as employee_id, salary and hourly_pay. The salary is annual, so I'd like to change the salary when I change the hourly pay.
For instance, by setting hourly pay a value, the salary must be updated to 2080 times that value. But it seems that I can't make the trigger at all. What is wrong with the following code?
create or replace function salary_update_function()
returns trigger as $salary_update$
begin
update employees
set new.salary = new.hourly_pay*2080;
return new;
end;
$salary_update$ language plpgsql;
create trigger salary_update_trigger
before update of hourly_pay on employees
for each row execute function salary_update_function();
column "new" of relation "employee" does not exist. If you where to remove theNEW's you will get a recursion as the UPDATE in the function will cause the UPDATE trigger to fire which will cause an UPDATE which cause the trigger to fire and on and on.GENERATED AScolumn instead?