Table name as variable in sql server

1.3k Views Asked by At

I am performing stored procedures the input option is as follows:

 Declare @table varchar (20)
 Set @query = 'Select * from' + @table + 'Where id = 1'
 Exec sp_executesql @query

The question is can it be done in some other way without the SQL being handled as varchar?

I was thinking along the lines of something similar to the following:

Declare @varchar varchar(20)
Set @tabla = 'MyTabla'
SELECT * FROM @table
0

There are 0 best solutions below