This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing a demonstration How to pass the string array as an input parameter in stored function of PostgreSQL.
We already used a different approach to pass multiple values as a single input parameter like comma separated, XML Parameter, Table Type.
You can also find one more type which is an array of PostgreSQL.
I created a stored function for this demonstration.
In the below, the stored function I declared one input parameter as a string array and using for..loop I am fetching record from this array and storing into one temporary table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
CREATE OR REPLACE FUNCTION fn_PostgresArray ( ArrayText character varying[] ) RETURNS TABLE (outArrayTextData CHARACTER VARYING(250)) AS $BODY$ DECLARE Counter INT = 0 ; BEGIN CREATE TEMP TABLE ArrayData(ArrayTextData CHARACTER VARYING(250)); FOR Counter in array_lower(ArrayText, 1) .. array_upper(ArrayText, 1) LOOP INSERT INTO ArrayData VALUES(ArrayText[Counter]); END LOOP; RETURN QUERY SELECT ArrayTextData FROM ArrayData; DISCARD TEMP; END; $BODY$ LANGUAGE plpgsql VOLATILE COST 100; |
1 |
SELECT *FROM fn_PostgresArray(ARRAY['Apple', 'Orange', 'Mango']); |