Let's have some tea ...

```sql
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)"?