lowdegeneration
lowdegeneration

Reputation: 379

error when using Union clause postgresql 9.5

CREATE OR REPLACE FUNCTION public.get_locations(
    location_word varchar(50)
    ) 
RETURNS TABLE
(
    country varchar(50),
    city varchar(50)
)
AS $$
DECLARE
location_word_ varchar(50);
BEGIN
location_word_:=concat(location_word, '%');
RETURN QUERY EXECUTE format('   (SELECT c.country, ''::varchar(50) as city FROM webuser.country c 
                                    WHERE lower(c.country)  LIKE %L  LIMIT 1)
                                UNION
                                (SELECT c.country,ci.city FROM webuser.country c
                                    JOIN webuser.city ci ON c.country_id=ci.country_id
                                    WHERE lower(ci.city) LIKE %L LIMIT 4)', 
                                    location_word_,
                                    location_word_ )    ;

END
$$ language PLPGSQL STABLE;

This is the error that i get ;

Why do i get this error?

EDIT

When I just replaced ''::varchar(50) with ''''::varchar(50) it worked!

Upvotes: 0

Views: 285

Answers (1)

Kostya Zhevlakov
Kostya Zhevlakov

Reputation: 267

Try next corrected function:

CREATE OR REPLACE FUNCTION public.get_locations(
    location_word varchar(50)
    ) 
RETURNS TABLE
(
    country varchar(50),
    city varchar(50)
)
AS $$
DECLARE
location_word_ varchar(50);
BEGIN
location_word_:=concat(location_word, '%');
RETURN QUERY EXECUTE format('   (SELECT c.country, ''''::varchar(50) as city FROM webuser.country c 
                                    WHERE c.country ILIKE ''%L'' LIMIT 1)
                                UNION
                                (SELECT c.country,ci.city FROM webuser.country c
                                    JOIN webuser.city ci ON c.country_id=ci.country_id
                                    WHERE ci.city ILIKE ''%L'' LIMIT 4)', 
                                    location_word_,
                                    location_word_ )    ;

END
$$ language PLPGSQL STABLE;

Upvotes: 1

Related Questions