我创建了一个函数,并在函数内部使用了预定义的函数array_to_string
。尝试执行功能时,出现以下错误。
ERROR: function array_to_string(integer, unknown) does not exist
LINE 1: SELECT array_to_string(id, ',') FROM ame.stops where custome...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
QUERY: SELECT array_to_string(id, ',') FROM ame.stops where customer_id=customer_id and driver_id=driver_id
CONTEXT: PL/pgSQL function ame.fn_get_stopids(integer,integer) line 4 at SQL statement
SQL state: 42883
当我在array_to_string
语句中使用select
时,效果很好。
select array_to_string(array(SELECT id FROM ame.stops where customer_id='31' and driver_id='770'), ', ')
功能代码
CREATE OR REPLACE FUNCTION ame.fn_Get_StopIds(customer_id integer, driver_id integer)
RETURNS text AS $$
DECLARE StopIds text;
BEGIN
SELECT array_to_string(id, ',') FROM ame.stops where customer_id=customer_id and driver_id=driver_id into StopIds;
RETURN StopIds;
END;
$$ LANGUAGE plpgsql;
答案 0 :(得分:0)
该函数需要一个数组,但是您正在传递一个整数(id
)。
在尝试工作时,您首先要将id转换为数组。
您可能想先使用array_agg
SELECT array_to_string(array_agg(id), ',')
FROM ame.stops
where customer_id=customer_id and driver_id=driver_id
into StopIds;
但是您不需要构建和连接数组,只需执行
SELECT string_agg(id::text, ',')
FROM ame.stops
where customer_id=customer_id and driver_id=driver_id
into StopIds;
您可能想按ID订购
答案 1 :(得分:0)
CREATE OR REPLACE FUNCTION ame.fn_get_stopids
(_customer_id integer,
_driver_id integer)
RETURNS text AS
$$
DECLARE
stopids text;
BEGIN
SELECT string_agg(id::text, ',') INTO stopids;
FROM ame.stops
WHERE customer_id = _customer_id
AND driver_id = _driver_id
RETURN stopids;
END;
$$
LANGUAGE plpgsql;