I have a function which takes a parameter, and I want to only insert that param into a table if it's not null. I'm trying something like this:
CREATE OR REPLACE FUNCTION app.some_function(
my_param integer)
RETURNS void
LANGUAGE 'sql'
COST 100
VOLATILE
AS $BODY$
INSERT INTO app.myTable(something)
VALUES(my_param)
ON CONFLICT(something) DO
UPDATE
SET --someConflictLogic
WHERE my_param <> '' AND my_param IS NOT NULL;
$BODY$;
I was hoping the WHERE clause here would cover me, but it's not the case. When I call this function with my_param as NULL, I get:
null value in column "something" violates not-null constraint
So it looks like it's still trying to insert it. How can I correct this condition?