Solved

Get comma separated column names from a table

Posted on 2015-01-30
5
107 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 11

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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.​
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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 different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

744 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

15 Experts available now in Live!

Get 1:1 Help Now