Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refer to column/table names using strings?

is it possible to refer to a column/table name by using a string? Something like SELECT * FROM 'my_table'::table_name_t?

The reason I'm asking: I have a table geometry_columns with some geometry tables. And I would like to know which objects are within a certain radius.

Thanks, Philip

like image 942
Philip Avatar asked Jan 21 '11 14:01

Philip


1 Answers

You will need a (stored) function to achieve this. The function takes the table name as an argument, creates the SQL dynamically and then returns the result of the SELECT based on that query.

here are some examples (not exactly what you need, but they should get you headed in the right direction):

http://forums.devshed.com/postgresql-help-21/plpgsql-variable-representing-table-name-137201.html
Dynamic column in SELECT statement postgres

like image 189
a_horse_with_no_name Avatar answered Oct 27 '22 02:10

a_horse_with_no_name