Connecting Tech Pros Worldwide Forums | Help | Site Map

Variable column name

Newbie
 
Join Date: Apr 2007
Posts: 2
#1: Apr 24 '07
I have a database(PostgreSQL) table(about 70k rows).I am developing an ASP
webpage and there is a list-box on it which contains the name of the
columns of that table. User selects the column name from that box and a
query is sent to the database. The data from that column should be
returned to the user.

Now the problem is that, I have seen the 'SELECT' statements with the
exact column name written in it.But in my case the column name is
variable.So, I am not able to understand how should I write such
query,where column name is like a parameter passed by the user.I am
working in .net for developing the webpage.

I tried to use a prepare statement....

PREPARE statement(text) AS SELECT $1 FROM table_name;
EXECUTE statement(column_name);

this returns....

?column?
---------------
column_name
column_name
column_name
column_name
column_name
..........
..........
column_name


I can't figure out the problem in this .........if there is another solution to this plz suggest.......

thanx

Moderator
 
Join Date: Nov 2006
Location: Boston, USA
Posts: 505
#2: Apr 25 '07

re: Variable column name


Although this manual does not say it explicitely, I don't think you can pass a table name, or a column name as an argument to prepared statement.

Try to run the plain SQL query instead.
Reply