I've been looking for a while, try a lot of codes found in SO, but none of them worked. So I'm asking a new question.
In a PostgreSQL function, I'm trying to return a SETOF bigint (bunch of ids from a table), but depending on first input ($1) the query would be diferent. So I have also a SELECT CASE in that function.
Currently, the function looks like this:
CREATE OR REPLACE FUNCTION get_employee_ids(int, int, int, int, text, int)
RETURNS SETOF bigint AS
$BODY$
SELECT CASE
WHEN $1 = 1 THEN
SELECT employee_id FROM employee WHERE period = $2 AND payment >= $3 AND operation = $4
INTERSECT
SELECT employee_id FROM employee WHERE period IN $5
WHEN $1 = 2 THEN
SELECT employee_id FROM employee WHERE period BETWEEN $1 AND $6 AND payment >= $2 AND operation = $3
INTERSECT
SELECT employee_id FROM employee WHERE period IN $5
WHEN $1 = 3 THEN
SELECT employee_id FROM employee WHERE period BETWEEN $1 AND $6 AND payment >= $2 AND operation != $3
INTERSECT
SELECT employee_id FROM employee WHERE period IN $5
WHEN $1 = 4 THEN
SELECT employee_id FROM employee WHERE period BETWEEN $1 AND $6 AND payment < $2 AND operation = $3
INTERSECT
SELECT employee_id FROM employee WHERE period IN $5
END
$BODY$
LANGUAGE sql VOLATILE;
In this case, the error is a syntax error near SELECT.
The problem seems to be that the SELECT is not executing. But I tried also with RETURN QUERY, EXECUTE, RETURN QUERY EXECUTE and lot of other things from other answers here.
How can I make this work?
Edit:
Useful info, this is how I use the function, with these parameters: get_employee_ids(4, 1108, 250, 97, "(1109,1110)", 0808)
syntax error near $5when surronding the wholeSELECTandsyntax error near INTERSECTwhen surronding eachSELECT.