Solved

How to make the column

Posted on 2014-02-24
4
211 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

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

20 Experts available now in Live!

Get 1:1 Help Now