Using variable inplace of table in select statment in sql server 2005

  • declare @var sysname declare @sql varchar(5000) set @var = 'Table_name'

    set @sql = 'select * from '+@var exec (@sql)

    ------------I don't want to use above script--------------

    ---Using below script it is not working ------------- declare @var sysname set @var = 'Table_name'

    select * from @var

    It is not working Is there ant way other than using dynamic query like first above , i want to use second one

    Please give me help how to use variable name of table in sql select statement ?

    _____________________________________________________________________________________________________________
    Paresh Prajapati
    ➡ +919924626601
    http://paresh-sqldba.blogspot.com/[/url]
    LinkedIn | Tweet Me | FaceBook | Brijj

  • Not that I know of. Only variables can be dynamic is a regular SQL query. If you want dynamic object names, you must wrap the statement with EXEC or sp_executesql

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

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