Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2005
    Posts
    31

    Unanswered: return multiple rows into one single string

    Hi,

    I need to return multiple rows into one single string

    Declare @String varchar(1000)

    Create table Cus (CusId Int,CusName varchar(10))

    Insert into Cus Select 1,'John'
    Union All
    Select 2,'Bob'

    Select * from Cus returns

    1 John
    2 Bob

    I need to return the all the rows from Cus table into a single string. The return is dynamic.
    I do not know the number of rows returned

    My result should be

    @String = 1,John,2,Bob

    How can i do that ?

  2. #2
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Of course, the better question is WHY you want to do this...
    Code:
    declare	@YourBadString varchar(4000
    
    select	@YourBadString = isnull(@YourBadString + ', ', '') + cast(CusID as varchar(10)) + ', ' + CusName
    from	Cus
    order by CusId
    
    select	@YourBadString
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  3. #3
    Join Date
    Aug 2005
    Posts
    31

    It Worked

    Thanks a lot. It worked

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •