I want to create a trigger where when a certain condition for a new table insert is met, both the table being inserted to and a corresponding table are updated. I want these tables to first be cleared and then have them repopulated using the functions I specify. So far I have this
CREATE FUNCTION test_fxn() RETURNS TRIGGER AS $test_fxn$
BEGIN
IF NEW.variable2 > x IS TRUE THEN
RAISE EXCEPTION 'too long';
END IF;
IF NEW.variable2 < x IS TRUE THEN
INSERT INTO summary_table (variable1, variable2) VALUES (NEW.variable1, NEW.variable2);
RAISE EXCEPTION 'correct';
END IF;
END;
$test_fxn$ LANGUAGE plpgsql;
CREATE TRIGGER test_fxn BEFORE INSERT OR UPDATE ON detailed_table
FOR EACH ROW EXECUTE PROCEDURE test_fxn();
Despite the exceptions showing up, the newly inserted row isn't being copied into the summary_table like I'd like it to. In addition it doesn't seem to be showing up in the detailed_table either, however removing the INSERT INTO statement and executing with the trigger will raise the exceptions and execute as normal. Any tips to go about fixing this would be appreciated.
mysql,postgresql,sql-server,oracleordb2- or something else entirely.return new;in your trigger function. An INSERT or UPDATE should result in the error "control reached end of trigger procedure without RETURN"summary_table. You are looking for "autonomous transaction" which aren't really supported in Postgres. Search this site for "autonomous transaction" and postgres - there are workarounds, e.g. using thedblinkmodule