如何在不知道其名称的情况下访问PostgreSQL中的记录类型的字段?

StackOverflow https://stackoverflow.com/questions/791364

  •  16-09-2019
  •  | 
  •  

我正在为PL/PGSQL函数编程:

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;
有帮助吗?

解决方案

这是我发现的一种方式:

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;

该解决方案的问题是,当列具有不同类型时失败。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top