Solved

How to make the column

Posted on 2014-02-24
4
213 Views
Last Modified: 2014-02-24
I have table fieldstatus where field and flag column are there.

In this table field contains the list of fields from table mstchvs.
Now here there is flag column which contains 1 or 0.


If i write select * from mstchvs
I get all the column names.

But i want to list those columns where field=0 in fieldstatus table.

i,e

select (column names from fieldstatus table where flag=0) from mstchvs
0
Comment
Question by:searchsanjaysharma
  • 2
  • 2
4 Comments
 
LVL 11

Expert Comment

by:John_Vidmar
ID: 39882459
Assuming field contains a comma-delimited list of field(s), and using dynamic-SQL:
DECLARE	@SQL varchar(max)

SELECT	@SQL = 'select ' + field + ' from mstchvs'
FROM	fieldstatus
WHERE	flag = 0

EXEC(@SQL)

Open in new window

0
 

Author Comment

by:searchsanjaysharma
ID: 39882493
It picks only the last field
0
 
LVL 11

Accepted Solution

by:
John_Vidmar earned 500 total points
ID: 39882594
Assumes there are multiple rows that have flag = 0, field contains a single value; build a comma-delimited field-list for the dynamic SQL, and execute:
DECLARE	@SQL varchar(max)

SELECT	@SQL =	'select'
		+ Stuff((	SELECT ', ' + field AS [text()] 
				FROM	(	SELECT DISTINCT field
						FROM	fieldstatus
						WHERE	flag = 0
					) x
				For XML PATH ('')),1,1,'') 
		+ ' from mstchvs'

EXEC(@SQL)

Open in new window

0
 

Author Closing Comment

by:searchsanjaysharma
ID: 39882688
tx
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 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

20 Experts available now in Live!

Get 1:1 Help Now