Introduction
Before reading this article, I will recommend you read these articles:
Temporary Tables and Table variables in SQL Server, both have their own pros and cons. We need to decide which one to use and when.
Differences between Temporary Table and Table variable in SQL Server
- The table variable (@table) is created in the memory. Whereas, a Temporary table (#temp) is created in the tempdb database. However, if there is memory pressure the pages belonging to a table variable may be pushed to tempdb.
- Table variables cannot be involved in transactions, logging, or locking. This makes @table faster than #temp. So table variable is faster than the temporary table.
- Temporary tables are allowed CREATE INDEXes whereas, Table variables aren’t allowed CREATE INDEX instead they can have an index by using Primary Key or Unique Constraint.
- A table variable can be passed as a parameter to functions and stored procedures while the same cannot be done with Temporary tables.
- Temporary tables are visible in the created routine and also in the child routines. Whereas, Table variables are only visible in the created routine.
- A temporary table allows Schema modifications, unlike Table variables.
Table Variable in SQL Server – Example
Table variable is a very useful programming construct, like that of any other variable.
DECLARE @TStudent TABLE
(
RollNo INT IDENTITY(1,1),
StudentID INT,
Name INT
)
--Insert data to Table variable @TStudent
INSERT INTO @TStudent(StudentID,Name)
SELECT DISTINCT StudentID, Name FROM StudentMaster ORDER BY StudentID ASC
--Select data from Table variable @TStudent
SELECT * FROM @TStudent
--Next batch
GO
SELECT * FROM @TStudent --gives error
DECLARE @TStudent TABLE
(
RollNo INT IDENTITY(1,1),
StudentID INT,
Name INT
)
--Insert data to Table variable @TStudent
INSERT INTO @TStudent(StudentID,Name)
SELECT DISTINCT StudentID, Name FROM StudentMaster ORDER BY StudentID ASC
--Select data from Table variable @TStudent
SELECT * FROM @TStudent
--Next batch
GO
SELECT * FROM @TStudent --gives error
Temporary Tables in SQL Server – Example
In SQL Server, based on the scope and behavior, temporary tables are of two types,
Local Temporary Tables (#temp)
Global Temporary Tables (##temp)
CREATE TABLE #StudentTemp
(
StudentID int,
Name varchar(50),
Address varchar(150)
)
GO
INSERT INTO #StudentTemp VALUES ( 1, 'Dipendra','Pune');
GO
SELECT * FROM #StudentTemp
CREATE TABLE #StudentTemp
(
StudentID int,
Name varchar(50),
Address varchar(150)
)
GO
INSERT INTO #StudentTemp VALUES ( 1, 'Dipendra','Pune');
GO
SELECT * FROM #StudentTemp
Points to Remember
- Temporary Tables are physically created in the tempdb database. These tables act as the normal table and also can have constraints, and indexes like normal tables.
- Table Variable acts like a variable and exists for a particular batch of query execution. It gets dropped once it comes out of the batch. It is created in the memory database but may be pushed out to tempdb.
- Use the Table variable, if you have less than 1000 rows otherwise go for Temporary tables.
Conclusion
I hope I was able to explain the difference between Temporary Tables and Table variables in SQL Server.
Happy Reading!

RANJEET KUMARPosted Aug 28, 2022, 9:13 AM
By mistake you have forget to mention ## in the example of global temp table in "Temporary Tables in SQL Server – Example"
patel dipakPosted Dec 14, 2021, 6:54 AM
It is very nice explanation
NakulPosted Aug 13, 2019, 9:36 PM
Table Variables are stored in temp DB , pls correct it
Akhil KumarPosted Sep 11, 2018, 1:19 AM
What is the need of Temporary Tables or temp variable whereas it expire after session. why we not create a table or view.Please explain the uses of these.
Ramzanali MominPosted Aug 25, 2018, 2:42 AM
Good one....... Simple, short and crisp!
Dennis ThomasPosted Feb 21, 2018, 5:40 AM
Good one....... Simple, short and crisp!
Arvind PandeyPosted May 24, 2017, 1:52 AM
Very Nice Dipendra Singh JI Good luck...
Manish KumarPosted May 22, 2017, 3:31 AM
" Table variables cannot be involved in transactions", Just to know confirm we can not apply transaction on table variable like begin tran ...... commit/RollBack
Upendra Pratap ShahiPosted May 19, 2017, 1:47 PM
Nice explaination...
Thiruppathi RPosted May 19, 2017, 7:28 AM
Nice Explanation...
Dharmraj ThakurPosted May 19, 2017, 4:22 AM
Very nice ........ thanks for sharing...............