In my PostgreSQL 9.6 database, I am trying to update 2 fields in table2 with a trigger when table1 is modified.
Function:
CREATE OR REPLACE FUNCTION schema.maj_tbl2()
RETURNS trigger AS
$BODY$
begin
UPDATE schema.table2
SET (contributor, code_tranche) = (user, table1.code_tranche) from schema.table1
where NEW.gid = table2.gid;
return NEW;
END;
$BODY$
Trigger:
CREATE TRIGGER trg_maj_table2
BEFORE UPDATE
ON schema.table1
FOR EACH ROW
EXECUTE PROCEDURE schema.maj_tbl2();
The 2 fields in table2 are modified the same way but for the entire table with the sames values. I can't update just the row in table2 concerned by the update in table1. I feel something is wrong with the where condition but can't fix it.
UPDATE...just to see if you receive theNEW.gidvalue? – John K. N. Aug 02 '18 at 13:46useris a reserved work in PostgreSQL, your unquoted use of it in the function does not refer to the column, but rather the name of the currently logged on user. Either quote it with double quotes, or better yet don't use that as the name of a column at all. – jjanes Aug 02 '18 at 13:50