Retrieving multiple values from within a SQL Query?

I have a Stored Procedure that has a main query. To effectively accomplish what I want, I'm going to need a separate query along with the main query within the same SP. This query will retrieve a single column from one DataTable.

What I need to do is create a variable that will concatenate the values from the column retrieved from this query, with each value separated by a comma. This variable will be a VARCHAR. This variable will be inserted into the SELECT clause of the main query (which I know how to do). How can I accomplish this?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Use a Common Table Expression (CTE)
    SELECT  RowID, Txt
    FROM    YourTable
    WHERE  RowID = 1
    SELECT  YourTable.RowID, CTE.Txt + ',' + YourTable.Txt
    FROM    CTE
    JOIN    YourTable ON YourTable. RowID = CTE.RowID + 1
    SELECT  Txt
    FROM    CTE
    WHERE  RowID = (SELECT MAX(RowID) FROM YourTable)

Open in new window

or Using a WHILE loop or Cursor
SET @Count = 1
SET @Txt = ''
SET @MaxCount = (SELECT MAX(RowID) FROM YourTable) 
WHILE @Count<=@MaxCount
    IF @Txt!=''
        SET @Txt=@Txt+',' + (SELECT Txt FROM YourTable WHERE RowID=@Count)
        SET @Txt=(SELECT Txt FROM YourTable WHERE RowID=@Count)
    SET @Count=@Count+1

Open in new window

Éric MoreauSenior .Net ConsultantCommented:
You want to concatenate values from a SQL query group by something. Right? In this case, have a look at
Carl TawnSystems and Integration DeveloperCommented:
If you just want to concatenate the values from a single column, without any grouping, you can do something like:
declare @output varchar(max);
set @output = ''

select @output = @output + [word] + ',' 
	from dbo.Test

print substring(@output, 1, len(@output)-1)

Open in new window

IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

BlakeMcKennaAuthor Commented:

I tried what you suggested and it didn't work. See attached Image.
Carl TawnSystems and Integration DeveloperCommented:
Is [load] a numeric column? If it is you're going to need to cast it otherwise the query engine will try to do an addition rather than an append.

DECLARE @output         VARCHAR(250)
SET @output = ''

SELECT @output = @output + CAST([load] AS VARCHAR) + ','

PRINT SUBSTRING(@output, 1 LEN(@output) - 1)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BlakeMcKennaAuthor Commented:
Sorry, I forgot to mention is a numeric value and your last post worked great!

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.