Declare or set column names

  • bit of a quickie here, but is there any way to use variables in select statements to define column or table names?

     

    ie:

    select @columnname from @tablename...

     

    im guessing for the tablename you could use #tablename, ie a temp table perhaps?

     

    Thanks

     

  • Why would you want to do that?

    Here is a must read if your thinking of going down this road.

    http://www.sommarskog.se/dynamic_sql.html

  • No, you cannot use variables in that way, directly.

    But you can construct a SQL text variable, and use sp_executesql to run the query.  But be aware of the warnings Ray referred to about dynamic sql.

    Hope this helps



    Mark

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

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