I am trying to execute a statement in order to attach a variable to a table name. I am new to sql and I couldn't find a solution to this issue online.
Declare @t time
Set @t='12:00:00.1'
Declare @ttable varchar (255)
Set @ttable='Temp'+(Select top 1 ID from mtable)
Exec('Insert into '+@ttable+'(Timestamp) values ('+'@t'+')')
Whenever I run this query I get the following error:
Msg 137, Level 15, State 2, Line 1 Must declare the scalar variable "@t".
I am not sure what exactly I am running on. I just opened SQL Server Management Studio (SQL Server 2008 R2) and created a database and tables. Any help is much appreciated.