Solved

Dynamic column names in query

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now