?
Solved

Dynamic column names in query

Posted on 2009-05-17
3
Medium Priority
?
1,288 Views
Last Modified: 2012-05-07
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
0
Comment
Question by:Shahzad Fateh Ali
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 143

Accepted Solution

by:
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.
0
 
LVL 31

Assisted Solution

by:RiteshShah
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.

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

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

765 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