Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to make the column

Posted on 2014-02-24
4
Medium Priority
?
225 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 2000 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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .

971 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