I am trying to create a function in PLSQL that does the following:
- Get a list of id's from my BD.
- Iterate over it in a loop and do a SELECT using one of its properties in a the WHERE.
- Aggregate all the loop iterations results in one single variable/whatever and return it as the result of the function. (concatenated string comma separated or anything would work)
what i got so far:
CREATE FUNCTION loop(OUT str text)
RETURNS text AS
$func$
DECLARE
i text;
BEGIN
str := '';
FOR i IN
SELECT id from public.factors
LOOP
str := str || ',' || #query - ex: select name from public.factors where factors.id = i #
-- Here i would like to do a select, return a single value from it and aggregate it to the existing value in str
END LOOP;
END
$func$ LANGUAGE plpgsql;
PS: the logic of the example is a simplified version of what i need to do. so any simplification of its logic in a single query etc its not a valid solution to the original problem.
SELECT id||', '|| name from public.factors. But your ex is just random, then it would be helpful if you changed the table name in your example...