Dynamic column names in query

Posted on 2009-05-17
Medium Priority
Last Modified: 2012-05-07

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


some thing similar
Question by:Shahzad Fateh Ali
LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 1000 total points
ID: 24405752
that won't work ...
you have to query the column list from the table, and build the sql statement from that.
LVL 31

Assisted Solution

RiteshShah earned 1000 total points
ID: 24405875
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.


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


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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.

Join & Write a Comment

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Recursive SQL is one of the most fascinating and powerful and yet dangerous feature offered in many modern databases today using a Common Table Expression (CTE) first introduced in the ANSI SQL 99 standard. The first implementations of CTE began ap…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question