Page 1 of 1

MS SQL 2000 How to refer to a column name in query?

Posted: Wed Oct 15, 2008 3:23 pm
by enatefox
Hi,

I have a table [A] with a bunch of columns and another table with a list of those columns. Table not only lists the names of the columns but categorizes them.

I need to select from [A] where the column name exists in when the category = 'x'.

I know the layout is not ideal but a program sets up the table structures. Anybody know how I can reference the column names in the query?


I'm working with something like:

Select *
from [A]
where [A].* in
(Select field_name from where category = 'potatoes')

Re: MS SQL 2000 How to refer to a column name in query?

Posted: Thu Oct 16, 2008 1:31 am
by VladSun

Re: MS SQL 2000 How to refer to a column name in query?

Posted: Thu Oct 16, 2008 7:16 am
by enatefox
Thanks for the quick reply. Maybe I didn't specify my problem correctly:

TableA has a bunch (287) of columns and TableB has the names of the columns as entries (rows) along with a category for each. I want to build a (dynamic) query where I select only the columns in TableA where the name of the columns is in TableB for a given category. To clarify, TableB has a field named "field_name"

An example:
Parentheses are the names of the columns and directly below are example entries for both tables.

TableA
(question_1 , question_2 , question_3)
"NULL" | "NULL" | "NULL"

TableB
(field_name , category)
"question_1" | "apples"
"question_2" | "monkeys"
"question_3" | "sauerkraut"

In this case, I want to (pseudocode):
SELECT * FROM TableA WHERE TableA.* IN
(select field_name from TableB where category = 'apples')

I'm going to use the front end to manage this but I'd like to know if this is actually possible. Thanks for your help

Re: MS SQL 2000 How to refer to a column name in query?

Posted: Thu Oct 16, 2008 11:04 am
by VladSun
It's a weird DB design ;) Try to redesign.