Passing column names to select list

  • I have the column name in a variable. I want to get the value from the variable and pass it to select list. Can I do it?

  • I would build the select statement in another variable and append the column name where you need it. You can then execute the select from the variable.

    Example:

    @column - holds column name

    @cmd - varchar to build select

    select @cmd = 'select col1, col2 ' + @column + ' from table_name where...

    exec (@cmd)

    This will run the select statement stored in the @cmd variable

  • Thanks Jamico, It worked. Thanks very much

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply