I am trying to write a function with an argument which is supposed to return one of two SELECTs depending on the argument of this fucntion:
CREATE FUNCTION fx(m integer)
RETURNS SETOF AS $$
BEGIN
CASE m
WHEN 1 then return query select * from pg_catalog.pg_roles
WHEN 2 then return query select * from pg_catalog.pg_auth_members
END
END
$$ LANGUAGE plpgsql;
SELECT fx(1);
Sorry I am completely new to sql functions so what am I doing wrong exactly? I am getting this error while executing:
"SQL Error [42883]: ERROR: function fx(integer) does not exist Hint: No function matchesthe given name and argument types. You might need to add explicit type casts"
Thanks beforehand!
returns setof as $$is invalid to begin with. But you will need to usereturns table()and specify the columns you want. You can't return a different set of columns depending on the input parameters. Additionally: each statement inside a CASE statement also needs to be terminated with a;and the CASE needs to be ended with aEND CASE;