Table variable error: Must declare the scalar variable "@temp"
Asked Answered
I

8

44

I am trying to achieve:

declare @TEMP table (ID int, Name varchar(max))
insert into @temp SELECT ID, Name FROM Table

SELECT * FROM @TEMP 
WHERE @TEMP.ID  = 1        <--- ERROR AT @TEMP.ID

But I'm getting the following error:

Must declare the scalar variable "@temp".

What am I doing wrong?

Iodide answered 1/8, 2013 at 13:23 Comment(2)
SELECT * FROM @TEMP T WHERE T.ID = 1Altagraciaaltaic
A table variable is inherently temporary -- there is no such thing as a "temporary table variable". What you are trying to do is declare a table variable. There is also a related but different concept of a temporary table.Funda
M
52

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

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

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

Mcnamee answered 1/8, 2013 at 13:24 Comment(0)
I
17

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 Table

SELECT * FROM @TEMP 
WHERE ID  = 1 
Icarus answered 1/8, 2013 at 13:24 Comment(0)
L
13

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 Table

SELECT * 
FROM #TempTable
WHERE ID  = 1 

Make Sure You are selecting the right database.

Langer answered 12/6, 2015 at 6:38 Comment(0)
C
8

If you bracket the @ you can use it directly

declare @TEMP table (ID int, Name varchar(max))
insert into @temp values (1,'one'), (2,'two')

SELECT * FROM @TEMP 
WHERE [@TEMP].[ID] = 1
Christianson answered 1/8, 2013 at 14:12 Comment(0)
C
1

You should use hash (#) tables, That you actually looking for because variables value will remain till that execution only. e.g. -

declare @TEMP table (ID int, Name varchar(max))
insert into @temp SELECT ID, Name FROM Table

When above two and below two statements execute separately.

SELECT * FROM @TEMP 
WHERE @TEMP.ID  = 1 

The error will show because the value of variable lost when you execute the batch of query second time. It definitely gives o/p when you run an entire block of code.

The hash table is the best possible option for storing and retrieving the temporary value. It last long till the parent session is alive.

Capreolate answered 21/7, 2017 at 5:54 Comment(0)
L
0

try the following query:

SELECT ID,
   Name
INTO #tempTable
FROM Table

SELECT *
FROM #tempTable
WHERE ID = 1

It doesn't need to declare table.

Loyalty answered 7/4, 2020 at 8:34 Comment(0)
P
0

You could stil use @TEMP if you quote the identifier "@TEMP":

declare @TEMP table (ID int, Name varchar(max));
insert into @temp SELECT 1 AS ID, 'a' Name;

SELECT * FROM @TEMP WHERE "@TEMP".ID  = 1 ;   

db<>fiddle demo

Pianist answered 11/4, 2020 at 6:50 Comment(0)
E
-4

You've declared @TEMP but in your insert statement used @temp. Case sensitive variable names.

Change @temp to @TEMP

Equation answered 18/6, 2015 at 15:4 Comment(1)
Variables are NOT case sensitive.Arlberg

© 2022 - 2024 — McMap. All rights reserved.