I receive a syntax error for the following:
CREATE OR REPLACE FUNCTION my_function(old_num INTEGER)
returns INTEGER
language plpgsql
AS
$$
DECLARE new_num INTEGER;
BEGIN
CASE
WHEN (old_num IN (1, 2, 3, 4)) THEN new_num = 10
WHEN (old_num IN (5, 6, 7, 8)) THEN new_num = 20
ELSE new_num = 0
END;
RETURN new_num;
END;
$$;
The error points to the second WHEN. I’ve tried using all kinds of combinations of parenthesis. What is wrong with this syntax??
4
Answers
A
CASE
expression results in a value. You want it to result in 10, 20 or 0, which you then want to assign to your variable new_num.Alternate solution:
You can convert your function to a SQL function and reduce it to a single Select statement. See Demo.