Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using WHERE ... IN in a Postgres Function

I want to create a function in Postgres along the lines of this:

CREATE OR REPLACE FUNCTION public.getAvailableForms (
    get_form_names  TEXT[]
) RETURNS TABLE (
    id          INTEGER,
    name        TEXT,
    location    TEXT,
    created     TIMESTAMP
) AS $$
BEGIN
    RETURN QUERY
        SELECT *
          FROM form
         WHERE form.name IN get_form_names;
END;
$$  LANGUAGE plpgsql
    SECURITY DEFINER;

However, it tells me that WHERE form.name IN get_form_names is syntactically incorrect.

I can't find any documentation on how to use array variables in a postgres function call.

Anyway, is it possible to use an array value passed as a function argument in a WHERE ... IN?

like image 433
Algy Taylor Avatar asked Aug 27 '26 22:08

Algy Taylor


1 Answers

Use any:

where form.name = any (get_form_names)

in would be for a discrete list of items, but any is used for an array.

like image 191
Hambone Avatar answered Aug 29 '26 12:08

Hambone



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!