Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

sql - Dynamic column in SELECT statement postgres

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...