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?

Thanks!
BlakeMcKennaAsked:
Who is Participating?

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

x
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.

tibbaCommented:
Use a Common Table Expression (CTE)
;WITH CTE AS
    (
    SELECT  RowID, Txt
    FROM    YourTable
    WHERE  RowID = 1
    UNION ALL
    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
DECLARE @MaxCount INTEGER
DECLARE @Count INTEGER
DECLARE @Txt VARCHAR(MAX)
SET @Count = 1
SET @Txt = ''
SET @MaxCount = (SELECT MAX(RowID) FROM YourTable) 
WHILE @Count<=@MaxCount
    BEGIN
    IF @Txt!=''
        SET @Txt=@Txt+',' + (SELECT Txt FROM YourTable WHERE RowID=@Count)
    ELSE
        SET @Txt=(SELECT Txt FROM YourTable WHERE RowID=@Count)
    SET @Count=@Count+1
    END
SELECT @Txt AS Txt

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 http://emoreau.com/Entries/Blogs/2009/10/SQL-Concatenate-values-multiple-from-multiple-lines.aspx
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:
Carl,

I tried what you suggested and it didn't work. See attached Image.
Capture.JPG
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.

i.e.
DECLARE @output         VARCHAR(250)
SET @output = ''

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

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 that...it is a numeric value and your last post worked great!

Thanks!
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.