Hey im working in PLpgSQL and i want to work with ...
# sql
s
Hey im working in PLpgSQL and i want to work with IN clause which takes value from an array of strings i pass from my server but i cant get it to work, like idk how to pickup that array in plpgsql and put it in the IN clause
t
Hey Quite sure you can use = ANY( instead of in for arrays and get the same result.
s
how do i pick up array in the function??
i made the data type array but
it says it doesnt exist
t
Is it a array of text?
If so use Text[] instead of ARRAY
s
lemme try
@tourdownundersame error
Copy code
pl
create or replace function test_api(search_country TEXT[])
cant create the function
t
It’s = ANY(search_country) You need both keywords and parentheses.
s
Copy code
json
{
    "success": false,
    "log": "Could not choose the best candidate function between: public.test_api(search_country => character varying), public.test_api(search_country => text[])"
}
stack overflow says i need to typecast
how do i acheive it in array
[ "Korea", "America" ] this is the array im passing
t
Can you force cast it with ::TEXT[]
s
Copy code
sql
-- drop function test_api
create or replace function test_api(search_country TEXT[])
    returns table
            (
                user_id        uuid,
                image_url        varchar,
                name           varchar,
                username       varchar
            )
AS
$$
BEGIN
    return query (
        SELECT U.id user_id,
               U.image_url,
               U.name,
               U.username
        FROM "Users" as U
          WHERE U.country = ANY(search_country::TEXT[])
        );
        END
$$
    language plpgsql;
@tourdownunder
somewhat like this?
not working still same error
t
The cast is on the parameter as you call the function. It’s already typed once inside the function so no need to cast there.
2 Views