CREATE OR REPLACE FUNCTION updatepersonne()
RETURNS trigger AS
$BODY$
DECLARE
IDToInsert numeric;
BEGIN
SELECT "PersonneID" FROM "Personne" p INTO IDToInsert WHERE (p."Prenom"=NEW."Prenom" AND p."Nom"=NEW."Nom" AND p."Adresse"=NEW."Adresse");
IF IDToInsert IS NULL THEN
INSERT INTO "Personne" ("PersonneID","Nom","Prenom","Adresse") VALUES (Default, NEW."Nom",NEW."Prenom",NEW."Adresse") RETURNING "PersonneID" into IDToInsert;
END IF;
NEW."PersonneID":=IDToInsert;
--IF TG_RELNAME="Emprunteur"
--THEN
-- NEW."EmprunteurID":=IDToInsert;
--ELSE
-- NEW."DetenteurID":=IDToInsert;
--END IF;
return new;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION updatepersonne()
OWNER TO postgres;
CREATE TRIGGER updatepersonnefromemprunteur
BEFORE INSERT
ON "Emprunteur"
FOR EACH ROW
EXECUTE PROCEDURE updatepersonne();
I wanted to check the existence of a tuple on another table in order to get the ID and use it for the new tuple or if not existing insert it on this other table. It seems to get the ID or insert it on the other table properly (tested with return null) but the main insert (on the table that triggered the thing) goes on a crazy infinite loop. Sorry for my poor english/ expression skill. Any hint? Thcks in advance.