views:

78

answers:

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

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

I don't want to use the above script.

It is not working using the below mentioned script.

declare @var sysname
set @var = 'Table_name'

select * from @var

Is there any way other than apart from using a dynamic query? I want to use something like the second one.

A: 

You cannot do it this way. This answer to a similar question explains the correct way to do it.

RBarryYoung
+2  A: 

I do not think it is possible. If the name of a table is determined dynamically, the only way to use it in a SELECT statement would be via EXEC.

Alek Davis