Let's have some tea ...
CREATE OR REPLACE FUNCTION allRelevantTeas() RETURNS TABLE(tea_id INTEGER) AS $function$
DECLARE
result REFCURSOR;
stmt TEXT;
countries_with_tea TEXT[] := array['england', 'turkey', 'india', 'japan', 'china'];
BEGIN
stmt := '';
FOR tea_drinker in countries_with_tea LOOP
stmt := stmt || format($$(SELECT tea_id FROM %I)$$, tea_drinker);
IF tea_drinker <> 'china' THEN
stmt := stmt || $$ UNION $$;
END IF;
END LOOP;
OPEN result FOR EXECUTE stmt;
RETURN result;
END $function$
LANGUAGE plpgsql STABLE;
SELECT * FROM allRelevantTeas();
Let's test this ...
syntax error at or near "countries_with_tea"
I don't see it. It's probably quite obvious, but I just don't see it.
Does anybody spot what's wrong with this? Or if not, is there any way I can get a more meaningful error from postgres than "there was a syntax error (but I don't tell you what it was)"?
country
should be a mapped superclass, so here we are ...return execute query execute stmt;
and get rid of the refcursorERROR: RETURN cannot have a parameter in function returning set