我有一个具有双重嵌套 FOR 循环的 Postgresql PL/pgSQL 函数,我想在其中动态设置列名。但是我找不到以编程方式访问 RECORD 列的方法。
我将直接跳到一个带有一些代码的示例:
FOR loop_helper1 IN SELECT
id, name1, name2, name3, nameN,
FROM table1
LOOP
FOR loop_helper2 IN SELECT name FROM table2 LOOP
-- I want to set values of columns in loop_helper1,
-- with the column name given by loop_helper2.name
-- An EXECUTE would not allow me to do this:
EXECUTE 'loop_helper1.' || loop_helper2.name || ':= function_call(123);'
-- (Eg. 'loop_helper1.name2 := function_call(123);')
-- However, this produces: ERROR: syntax error at or near "loop_helper1"
END LOOP;
END LOOP;
有任何想法吗?
当然必须有办法做到这一点,但我似乎无法找到它。感谢所有帮助和建议。谢谢。