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

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
enatefox
Forum Newbie
Posts: 11
Joined: Fri Apr 04, 2008 12:58 pm

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

Post 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')
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

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

Post by VladSun »

There are 10 types of people in this world, those who understand binary and those who don't
enatefox
Forum Newbie
Posts: 11
Joined: Fri Apr 04, 2008 12:58 pm

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

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

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

Post by VladSun »

It's a weird DB design ;) Try to redesign.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply