• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 366
  • Last Modified:

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
0
WaldaInc
Asked:
WaldaInc
1 Solution
 
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
 
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now