Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Selecting columns based on the rows returned?

Author  Topic 

SanthoshMReddy
Starting Member

1 Post

Posted - 2009-09-28 : 14:18:28
Hello all,

I have been stuck with this problem since morning and would appreciate any help in this regard. Here is the problem:

I have a huge table with say 100 columns. I query this table based on certain parameters but only select a few columns (say about 10). The columns to be selected are present in a 2nd table. For ex: table 2 will contain something like:

ColumnName1 Type1
ColumnName2 Type1
ColumnName3 Type1
ColumnName4 Type1
ColumnName5 Type2
ColumnName6 Type2
ColumnName7 Type2

So I have to select only ColumnName1, ColumnName2, ColumnName3, ColumnName4 for Type1 and ColumnName5, ColumnName6, ColumnName7 if Type2.

How do I construct this query?

In simple english, my query should be:

Select <the columns present in Table2 for Type1> from Table1.

I don't know how to populate the part: <the columns present in Table2 for Type1> dynamically. Any help is appreciated.

Thanks!
Santhosh

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-09-28 : 14:47:44
It would require dynamic SQL, which is highly not recommended due to performance issues. It sounds like your system is not properly designed as you shouldn't have a table with this kind of information stored in it.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-09-29 : 00:35:10
why is the columns to be selected stored as a value in another table? is nt it enough to handle this as your front end to show only reqd values?
Go to Top of Page
   

- Advertisement -