Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Function is not working with varchar records

I have a function that in theory will give the country name as output when the input is the city name:

CREATE OR REPLACE FUNCTION public.country_by_city(city varchar, OUT Country varchar)  
RETURNS varchar  
LANGUAGE SQL AS 
$function$   
    SELECT c.country     
    FROM country c   
    JOIN city c2 ON c2.country_id = c.country_id     
    WHERE c2.city = $1   
$function$ 
; 

And it’s working if I will change c2.city to c2.city_id but when I am trying to call a function with a varchar column in it, for example :

SELECT public.country_by_city(Abha);

I get this error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

SQL Error [42703]: ERROR: column "abha" does not exist

I tried to use UPPER() or INITCAP(), and it didn’t help. It’s worth mentioning that city name records in the city table have capitalized first letters.

>Solution :

You need to call this parameter using simple quotes, for example:

SELECT public.country_by_city('Abha');

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading