Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6

    Unanswered: using stuff to return a string of values from a column without a cursor

    I think it was Pat Phelan who posted a little trick here where he used the STUFF function to create a string fo values from a column without using a cursor.

    I am starting a brand new project and I did my table design and I am awaiting a finalized requirements document to start coding and I thought I would spend a little time writing some code to autogenerate some generic one record at a time SELECT, INSERT,UPDATE and DELETE stored procedures. With the coming holiday things are getting quiet around here.

    The code that is not working is listed below. It does not work. It returns Null. I suck.

    Code:
    DECLARE @column_names varchar(8000)
    
    SET @column_names = ''
    
    SELECT @column_names = STUFF(@column_names,LEN(@column_names),0,C.COLUMN_NAME + ', ')
    FROM INFORMATION_SCHEMA.COLUMNS C
    WHERE TABLE_NAME = 'MyTable'
    
    SELECT @column_names
    little help?
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  2. #2
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Quote Originally Posted by Thrasymachus
    It does not work. It returns Null. I suck.
    Actually it returns a ZLS

  3. #3
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    I'm not quite sure what the STUFF thing is supposed to do. You just after a CSV of columns?

    Code:
    DECLARE @column_names varchar(8000)
    
    SELECT @column_names = COALESCE(@column_names + ', ', '') + C.COLUMN_NAME
    FROM INFORMATION_SCHEMA.COLUMNS C
    WHERE TABLE_NAME = 'mytable'
    
    SELECT    @column_names
    Last edited by pootle flump; 12-13-07 at 12:18.

  4. #4
    Join Date
    Nov 2004
    Location
    on the wrong server
    Posts
    8,835
    Provided Answers: 6
    thanks pootle. i should have thought of that. hardly slept last night.

    i should go home before i break something.
    Last edited by Thrasymachus; 12-13-07 at 12:11.
    “If one brings so much courage to this world the world has to kill them or break them, so of course it kills them. The world breaks every one and afterward many are strong at the broken places. But those that will not break it kills. It kills the very good and the very gentle and the very brave impartially. If you are none of these you can be sure it will kill you too but there will be no special hurry.” Earnest Hemingway, A Farewell To Arms.

  5. #5
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    My pleasure. I learned it here anyway - just about paying back the man

  6. #6
    Join Date
    May 2004
    Location
    Seattle
    Posts
    1,313
    see this thread for a faster method if you are on 2005:

    http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81254

  7. #7
    Join Date
    Aug 2004
    Location
    Dallas, Texas
    Posts
    831
    We can skip a step by using the code below:

    DECLARE @COLUMN_NAME varchar(55)

    SELECT COALESCE(@COLUMN_NAME + ', ', '') + C.COLUMN_NAME AS [Column Name]
    FROM INFORMATION_SCHEMA.COLUMNS C
    WHERE TABLE_NAME = 'mytable'

Posting Permissions

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