In this post, I demonstrate to pass string array as an input parameter in stored function of PostgreSQL.
We already used a different approach to pass multiple value as a one single input parameter like, comma separated, XML Parameter, Table Type.
You can also find one more type which is an array of PostgreSQL.
I have created one stored function to demonstrate the string array.
In below, stored function I have declared one input parameter as string array and using for..loop I am fetching record from this array and storing into one temporary table.
CREATE OR REPLACE FUNCTION fn_PostgresArray
ArrayText character varying
RETURNS TABLE (outArrayTextData CHARACTER VARYING(250)) AS
Counter INT = 0 ;
CREATE TEMP TABLE ArrayData(ArrayTextData CHARACTER VARYING(250));
FOR Counter in array_lower(ArrayText, 1) .. array_upper(ArrayText, 1)
INSERT INTO ArrayData VALUES(ArrayText[Counter]);
LANGUAGE plpgsql VOLATILE
Now let’s call this function by passing a string array.
SELECT *FROM fn_PostgresArray(ARRAY['Apple', 'Orange', 'Mango']);
The result is:
Please share your ideas and opinions about this topic with me, your contribution will add true value to this topic.
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.
If you like this post, then please share it with others.
Please follow dbrnd.com, I will share my experience towards the success of Database Research and Development Activity.
I put up a post every day, please keep reading and learning.
Discover Yourself, Happy Blogging !
Anvesh M. Patel.