Table variable error: Must declare the scalar variable "@temp" Table variable error: Must declare the scalar variable "@temp" sql-server sql-server

Table variable error: Must declare the scalar variable "@temp"


A table alias cannot start with a @. So, give @Temp another alias (or leave out the two-part naming altogether):

SELECT *FROM @TEMP tWHERE t.ID = 1;

Also, a single equals sign is traditionally used in SQL for a comparison.


Either use an Allias in the table like T and use T.ID, or use just the column name.

declare @TEMP table (ID int, Name varchar(max))insert into @temp SELECT ID, Name FROM TableSELECT * FROM @TEMP WHERE ID  = 1 


There is one another method of temp table

create table #TempTable (ID int,name varchar(max))insert into #TempTable (ID,name)Select ID,Name from TableSELECT * FROM #TempTableWHERE ID  = 1 

Make Sure You are selecting the right database.