我有一个像这样的纯SQL函数:
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;
它的行为符合预期,一切正常。但是,我希望它与building_types
的输入数组一起工作,而不是与单个值一起工作。当我尝试这样做时:
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;
我收到语法错误:
ERROR: syntax error at or near "$1"
LINE 15: bldg."LandUse" IN $1
有什么想法吗?
相关的版本为9.6。
答案 0 :(得分:1)
与IN
运算符等效的是any
运算符:
您需要使用:
WHERE
bldg."LandUse" = any($1);