Dynamic column names in query

Hi,

I wan to dynamically select columns from a table where the list of column will be provided by a sub query. ie

select (select `columns` from `column_table`) from table

 OR

some thing similar
LVL 9
Shahzad Fateh AliWeb Solutions Architect & Technical Project Manager- VentureDive (Pvt) LtdAsked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
that won't work ...
you have to query the column list from the table, and build the sql statement from that.
0
 
RiteshShahConnect With a Mentor Commented:
well, it is not possible with the way you want to do but it is possible if you store column name in variable fist and than generate SELECT statement from that variable. for a hint you can refer my below give article.

http://www.sqlhub.com/2009/03/dynamic-pivot-with-where-condition-in.html

you can see I have stored column name in variable @cols and used it to build SELECT statement.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.