• Neet trick Ian!

    I quickly made a script to return columns for a table but your trick was way cooler. 🙂

    But if you're somewhere in a deserted island without a nifty GUI some thing like this might help.

    CREATE procedure util_GetColumnNames

    (

    @TableName varchar (100),

    @Delimiter varchar (10) = ',',

    @AsRow bit = 0

    )

    as

    if not exists(select 1 from sys.objects where type = 'U' and Name = @TableName)

    print 'Table does not exist.'

    else

    if (@AsRow = 1)

    begin

    declare @cols varchar(2000)

    SELECT @cols = ISNULL(@cols + @Delimiter + '[' + C.Name + ']',

    '[' + C.Name + ']')

    FROM sys.objects O

    inner join sys.columns C

    ON O.object_id = C.object_id

    where type = 'U'

    and O.Name = @TableName

    SELECT @cols

    end

    else

    select '[' + C.Name + ']' + @Delimiter

    from sys.objects O

    inner join sys.columns C

    ON O.object_id = C.object_id

    where type = 'U'

    and O.Name = @TableName

    This still returns a trailing delimiter in the last row, but Im not bothering to fix it.

    /Martin