Solved

Get comma separated column names from a table

Posted on 2015-01-30
5
109 Views
Last Modified: 2015-04-07
How can I set a string to a comma separated list of columns. I am using SQL Server 2008

example

@ColumnNames =  'Col1, Col2, Col3'
0
Comment
Question by:countrymeister
5 Comments
 
LVL 33

Expert Comment

by:Mike Eghtebas
ID: 40580806
What you have is correct. But how it is used matters. Show more. Give a bigger picture.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 40580854
You can only use that in a dynamic query, that is the SQL as string, ran with EXEC or sp_executesql.
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 40580855
0
 
LVL 4

Assisted Solution

by:Monika Bharti
Monika Bharti earned 250 total points
ID: 40581144
Hi,

To set a string with comma separated list of columns use the following code. Before running this code you can change the table name, which you are using.

USE AdventureWorks
GO
-- Check Table Column
SELECT [Name]
FROM HumanResources.Shift
GO
-- Get CSV values
SELECT STUFF(
(SELECT ',' + s.Name
FROM HumanResources.Shift s
ORDER BY s.Name
FOR XML PATH('')),1,1,'') AS CSV
GO

Open in new window


Hope this will resolve your query.
0
 
LVL 12

Accepted Solution

by:
Nakul Vachhrajani earned 250 total points
ID: 40582826
Based on your requirement, you can use any one of the methods highlighted here: Different Ways to generate Comma-separated string

Do go through the various comments in my post as well - there is some great information with respect to string manipulation there.
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

Suggested Solutions

Title # Comments Views Activity
Complex SQL script 1 30
tempdb latch contention 12 47
mysql joining from the same table 6 29
SQL Server Question 5 25
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.‚Äč
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

943 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

8 Experts available now in Live!

Get 1:1 Help Now