I have the following function, which receives an array of ints, iterates it calling other function:
CREATE FUNCTION calculateAbsencesForIDs(INT[]) RETURNS TEXT AS
'
DECLARE
index integer := 0;
resultArray decimal[];
id int;
result text;
BEGIN
FOR id IN SELECT $1 LOOP
resultArray[index] = calculateAbsenceForID(id);
index := index + 1;
END LOOP;
RETURN array_to_string(resultArray, result);
END;
'
LANGUAGE plpgsql;
I try to call it using:
SELECT calculateAbsencesForIDs(ARRAY[85,74,75,76,77,78,79,80]);
or
SELECT calculateAbsencesForIDs('{85,74,75,76,77,78,79,80}');
or
SELECT calculateAbsencesForIDs('{85,74,75,76,77,78,79,80}'::int[]);
...
But I have always the same error:
[Error Code: 0, SQL State: 22P02] ERROR: invalid input syntax for integer: "{85,74,75,76,77,78,79,80}"
I don't know how I can call this function. I have looked in postgres docs, I think this is correct but it doesn`t work.
This line:
means that you assign
idto each value fromSELECT $1which returns a single record with a single field of typeINT[].As
idis declared asINT, you get the conversion error you're observing.In
8.4and above you could useUNNEST, in8.3replace it withAlternatively, you could just do: