Results 1 to 2 of 2

Thread: Column name in Variable for select list

  1. #1
    Join Date
    Mar 2003
    United Kingdom

    Column name in Variable for select list

    I am dynamically trying to pass a particular column name to the select list of a select statement.

    that is

    @var = 'Col1'

    so I need the following query

    select col1 from tbl1

    I will not know the column name is 'Col1' but the value will be in @var. How can I use it?

    By the way I am using SQL Server 2000

  2. #2
    Join Date
    Nov 2002
    New Jersey, USA
    You should build your SELECT statement in a variable like

    declare @cmdstr varchar(100)

    set @cmdstr = 'select ' + @var + ' from table1'

    exec (@cmdstr)

Posting Permissions

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