I have many trigger functions written in PL/pgSQL that follow the below logic:
SELECT my_name
INTO l_my_name
FROM data
WHERE data.my_number = NEW.test_num;
IF NOT FOUND THEN
INSERT INTO test_event
VALUES (NEW.test_num, NULL, NEW.event_one, NEW.event_two);
ELSE
INSERT INTO test_event
VALUES (NEW.test_num, l_my_name, NEW.event_one, NEW.event_two);
END IF;
Some of the inserts have upwards of 15+ values and I am trying to simplify them down to just 1 insert as the above code repeats over and over for different checks in the stored procedure needlessly making the procedure hundreds of lines of code long. My thought was something along the lines of:
INSERT INTO test_event
VALUES (NEW.test_num, NULLIF(l_my_name,''), NEW.event_one, NEW.event_two);
I am unsure if l_my_name
would be ''
if not found and the NULLIF
would work properly or if I would have to do something different.
insert on conflict
instead?