Solved

Dynamic column names in query

Posted on 2009-05-17
3
1,280 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
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 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 250 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

821 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