I have been looking for a way to access columns returned by a SQL select query in PostgreSQL dynamically. For e.g. see the below sample function:
CREATE FUNCTION my_func(col text) RETURNS integer LANGUAGE plpgsql
$$
DECLARE
val text;
rec record;
BEGIN
FOR rec IN
EXECUTE 'SELECT ' || col || ' FROM employee_tb WHERE sal > 10000'
LOOP
-- trying this but failing
val = rec[col];
-- this is also failing
val = rec.col;
-- What is the syntax for doing this. There should be one I think.
END LOOP;
RETURN 1;
$$
-- Call function
select my_func('emp_code');
This is very much possible in any other language. I have been looking for a solution for quite sometime now but still not able to find one.
Thanks in advance.