SQL Syntax - Combining two rows into one variable

Hi.

I have the SQL statement below

SELECT COALESCE(Cast(@ID as Varchar(10)) + ',', '') + Cast(ImproperID as Varchar(10))
FROM ImproperReason
WHERE ImproperCode = @Code

Which works fine, it returns two rows of data.  However I want to assign this to a variable that is comma delimted and when I do this

DECLARE @myVar varchar(max)
SET @myVar = (SELECT COALESCE(Cast(@ID as Varchar(10)) + ',', '') + Cast(ImproperID as Varchar(10))
FROM ImproperReason
WHERE ImproperCode = @Code)

I get an error about subquery returning more than one value.

How do I get the two rows returned to be one variable, comma delimted such as:

1,2
or
1,52
etc...

Thanks
WaldaIncAsked:
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.

metechguruCommented:
I don't know MS SQL . I am a user of DB2 and oracle. But it seems the correct syntax for cursor is
DECLARE @MyVariable CURSOR

SET @MyVariable = CURSOR SCROLL KEYSET FOR
SELECT LastName FROM Northwind.dbo.Employees

OR
DECLARE @MyVariable CURSOR
DECLARE MyCursor CURSOR FOR
SELECT LastName FROM Northwind.dbo.Employees

SET @MyVariable = MyCursor

Other example
DECLARE @VAR1 INT
DECLARE @VAR2 VARCHAR(20)
DECLARE @VAR3 VARCHAR(20)
DECLARE @VAR4 INT
DECLARE @VAR5 INT
DECLARE @CUR CURSOR

SET @CUR = CURSOR FOR
SELECT DISTINCT SNO , TYPE FROM TRANTABLES
OPEN @CUR
FETCH NEXT
FROM @CUR INTO @VAR1 , @VAR2
WHILE @@FETCH_STATUS = 0
BEGIN

0
Aneesh RetnakaranDatabase AdministratorCommented:
SELECT @myVar = COALESCE(@myVar +'','' ) +COALESCE(Cast(@ID as Varchar(10)) + ',', '') + Cast(ImproperID as Varchar(10))
FROM ImproperReason
WHERE ImproperCode = @Code
0

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
Patrick MatthewsCommented:
There is probably no need to do a cursor here.

Can you work up a simple example of what the query returns now, and what you want the end result to be?
0
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.

dqmqCommented:
I"m confused. Your working sql returns two rows of two columns each, in other words, 4 values in all.  But you seem to only want a variable containing two values, comma delimited.

But, let's say you just care about the ImproperId values, then do like this:

DECLARE @myVar varchar(max)
SET @myVar = NULL
SELECT @myVar = COALESCE(@MyVar + ',', '') + Cast(ImproperID as Varchar(10))
FROM ImproperReason
WHERE ImproperCode = @Code)




DECLARE @myVar varchar(max)
SELECT @myVar = ',' + Cast(@ID as Varchar(10))  
FROM ImproperReason
WHERE ImproperCode = @Code
FOR XML PATH ('')


   

0
SharathData EngineerCommented:
try this
;WITH cte 
     AS (SELECT COALESCE(CAST(@ID AS VARCHAR(10)) + ',','') + CAST(ImproperID AS VARCHAR(10)) ImproperID 
         FROM   ImproperReason 
         WHERE  ImproperCode = @Code) 
SELECT RTRIM(SUBSTRING(ISNULL((SELECT ',' + ImproperID 
                               FROM   cte 
                               for xml path('')),' '),2,2000)) AS ImproperID

Open in new window

0
WaldaIncAuthor Commented:
My syntax was messed up.

Thanks for everyone's reply.
0
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.