I pass an array of strings to the procedure, inside I need to create a new array with queries and return it. But how to do that?

DROP FUNCTION selectnamedate(text[]); CREATE OR REPLACE FUNCTION selectnamedate(Names text[]) RETURNS text[] AS $$ DECLARE name varchar; count text[]; BEGIN FOR name IN SELECT * FROM Names LOOP count END LOOP; return count; END; $$ LANGUAGE plpgsql; 

Here is where just count I can not find how to do it? Also I generally correctly declare function that?

    1 answer 1

     CREATE OR REPLACE FUNCTION selectnamedate(names text[]) RETURNS text[] AS $$ DECLARE name text; res text ARRAY; BEGIN FOR name IN SELECT * FROM unnest(names) LOOP -- Разбираем массив на элементы res := res || name; -- Добавляем элемент к результирующему массиву END LOOP; RETURN res; END; $$ LANGUAGE plpgsql; 
    • || concatenation, and how straight to add to the array then? - Oma
    • In the declare block, you declare an empty array, and then add an element to it at each iteration. It is possible, of course, and so array_append(res, name) . - Sergey Gornostaev
    • Please tell me more how to modify the code to return several arrays (RETURNS text [], text2 []). - Oma
    • You can declare an OUT parameter if the returned parameters are few, or you can return a record or table with fields of type text []. - Sergey Gornostaev