0

I have a pure SQL function like this:

CREATE OR REPLACE FUNCTION get_buildings_by_type(
    building_types TEXT
)

RETURNS TABLE (bldg_id TEXT, "SurfArea" FLOAT, geom GEOMETRY) AS

$$
    SELECT
        bldg."OBJECTID"::TEXT AS bldg_id,
        bldg."SurfArea"::FLOAT,
        bldg.geom
    FROM
        static.buildings AS bldg
    WHERE
        bldg."LandUse" = $1
$$

LANGUAGE SQL;

And it behaves as expected, everything is working. However, I would like to have it work with an input array of building_types, rather than a single values. When I try this instead:

CREATE OR REPLACE FUNCTION get_buildings_by_type(
    building_types TEXT[]
)

RETURNS TABLE (bldg_id TEXT, "SurfArea" FLOAT, geom GEOMETRY) AS

$$
    SELECT
        bldg."OBJECTID"::TEXT AS bldg_id,
        bldg."SurfArea"::FLOAT,
        bldg.geom
    FROM
        static.buildings AS bldg
    WHERE
        bldg."LandUse" IN $1
$$

LANGUAGE SQL;

I get a syntax error:

ERROR:  syntax error at or near "$1"
LINE 15:   bldg."LandUse" IN $1

Any ideas?

The version is 9.6 if that is relevant.

3
  • @a_horse_with_no_name: Still the same syntax error. I put the version in too since it might matter. Commented Nov 19, 2018 at 13:43
  • in operator is not for arrays, @a_horse_with_no_name posted the correct answer Commented Nov 19, 2018 at 13:45
  • @a_horse_with_no_name answer is indeed correct, I think I was executing in the wrong window before. Commented Nov 19, 2018 at 13:48

1 Answer 1

1

The equivalent of an IN operator for arrays is the any operator:

You need to use:

WHERE
    bldg."LandUse" = any($1);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.