Solved

Dynamic column names in query

Posted on 2009-05-17
3
1,283 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 143

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

763 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