Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2003
    Location
    Dubai, United Arab Emirates
    Posts
    1

    Post Unanswered: HELP! Concatinated Values

    Hello everyone,

    I would really appreciate if someone could help me out with this one.

    I need to execute a select statement which returns more than one row but I need the values from all the rows returned in a single string.

    For example

    SELECT * FROM USERS would produce

    ID NAME
    1 Jason
    2 Mark
    3 Whatever

    I need the returned value to be a string with 'Jason,Mark,Whatever' as a returned result.

    Any thoughts anyone?

    Regards

  2. #2
    Join Date
    Oct 2003
    Location
    Sweden
    Posts
    45
    I would look towards using a cursor in a stored procedure to loop through each row and concatenating the values one by one.

    I'm no big fan of serverside cursors, even if they have their time and place too, so depending on the environment and application demands I would look into putting such logic in the middle tier or even client side.

    Cheers,
    Robert

  3. #3
    Join Date
    Sep 2003
    Posts
    212
    Originally posted by Rawbat
    I would look towards using a cursor in a stored procedure to loop through each row and concatenating the values one by one.

    I'm no big fan of serverside cursors, even if they have their time and place too, so depending on the environment and application demands I would look into putting such logic in the middle tier or even client side.

    Cheers,
    Robert
    declare @name varchar(40),@result varchar(500)
    declare Cursor1 cursor
    for
    SELECT * FROM USERS
    open Cursor1
    fetch from Cursor1 into @name
    Set @result=@name
    while(@@fetch_status=0)
    begin
    set @result = ','+@name
    fetch from Cursor1 into @name
    end

Posting Permissions

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