How to access to fields of a record type in postgresql without knowing their names?
-
16-09-2019 - |
Question
I'm programming a PL/pgSQL function with this:
cols_to_select := according to a set of rules, we get the name of the columns
FOR I IN EXECUTE 'SELECT '||cols_to_select||' FROM tabl' LOOP
-- how to access to fields of record I without knowing their names?
--
END LOOP;
Solution
This is one way I figured out:
cols_to_select := according to a set of rules, we get the name of the columns
FOR I IN EXECUTE 'SELECT ARRAY['||cols_to_select||'] as AR FROM tabl' LOOP
-- how to access to fields of record I without knowing their names?
FOR j IN 1..array_upper(I.ar,1) LOOP
RAISE NOTICE '%', I.AR[j];
END LOOP;
END LOOP;
The problem with this solution is that fails when the columns have different types.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow