Как получить доступ к полям типа записи в postgresql, не зная их имен? - PullRequest
2 голосов
/ 26 апреля 2009

Я программирую функцию 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;

1 Ответ

2 голосов
/ 26 апреля 2009

Это один из способов, который я понял:

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;

Проблема с этим решением состоит в том, что происходит сбой, когда столбцы имеют разные типы.

...