Difference Between Temporary Table and Table Variable in SQL Server
Difference Between Temporary Table and Table Variable in SQL Server
Both the Temporary Tables and Table Variables in Sql Server provide a mechanism for storing of the
result-set temporarily for further processing.
Local temporary tables are temporary tables Its scope is in the stored procedure, user
that are available only to the session that defined function or batch where it is declared
created them. Global temporary tables are like any local variable we create with a
temporary tables that are available to all DECLARE statement.
sessions and all the users.
Local temporary tables are automatically Table variables are automatically cleaned up
destroyed at the end of the procedure or at the end of the user defined function,
session that created them. Global temporary stored procedure, or batch in which they are
tables are dropped automatically when the defined.
last session using the temporary table has
completed
We can also drop temporary tables explicitly
using drop command similar to normal table.
Temporary table name can be of maximum Table variable name can be of maximum 128
116 characters characters
PRIMARY KEY, UNIQUE, NULL, CHECK etc can PRIMARY KEY, UNIQUE, DEFAULT values,
be implemented at the time of creating NULL, CHECK can be added, but they must be
temporary tables using CREATE TABLE incorporated with the creation of the table in
statement or can be added after the table has the DECLARE statement. FOREIGN KEY not
been created. FOREIGN KEY not allowed. allowed.
Temporary table supports adding Indexes Table Variables doesn’t allow the explicit
explicitly even after creation and it can also addition of Indexes after it is declared, the
have the implicit Indexes which are the result only means is the implicit indexes which are
of Primary and Unique Key constraint. created as a result of the Primary Key or
Unique Key constraint defined at the time of
declaring Table Variable.
Temporary tables can also be directly created Table variables can’t be created using Select
and data can be inserted using Select Into Into statement because being a variable it
statement without creating a temporary table must be declared before use
explicitly.
The SET IDENTITY_INSERT statement is The SET IDENTITY_INSERT statement is not
supported in temporary table supported in table variables
We can’t return a temporary table from a We can return a table variable from a user-
user-defined function defined function
Temporary Table can be truncated like Table variables can’t be truncated like normal
normal table table or temporary tables.
The data in the temporary table will be rolled The data in the table variable will not
back when a transaction is rolled back similar be rolled back when a transaction is rolled
to normal table back
Temporary tables used in stored procedures Table variables used in stored procedures
cause more recompilations of the stored cause fewer recompilations of the stored
procedures than when table variables are procedures than when temporary tables are
used. used.
A temporary table will generally use more A table variable will generally use less
resources than table variable resources than a temporary table
Temporary tables can be access in nested Tables variables can’t be access in nested
stored procedures stored procedures
Can be altered using ALTER command Does not support ALTER command
Temporary tables should be used for large Table variables should be used for small result
result sets. sets and the everyday type of data
manipulation since they are faster and more
flexible than temporary tables