Dynamic column in SELECT statement postgres

postgresql, sql

Solution

In order to write a dynamic query you would have to do something like:

EXECUTE 'SELECT '|| get_columns()|| ' FROM table_name' INTO results

Please read the documentation: http://developer.postgresql.org/pgdocs/postgres/plpgsql-statements.html

Problem

I am quite new to the postgresql. what is the best way to achieve this? ``` SELECT get_columns() FROM table_name; ``` `get_columns()` will provide the column names for the query. I saw people advising to use EXECUTE statement but I couldn't got that working. Lets say there is table Test with columns a,b,c and I want to run ``` SELECT a,b FROM Test; SELECT a,c FROM Test; ``` with column names generated dynamically.

Original source