In SQL Server 2005, we can create temp tables one of two ways:
declare @tmp table (Col1 int, Col2 int);
or
create table #tm
Differences between Temporary Tables (##temp/#temp)
and Table Variables (@table)
are as:
Table variable (@table)
is created in the memory
. Whereas, a Temporary table (##temp/#temp)
is created in the tempdb database
. However, if there is a 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 then #temp
. So table variable is faster then temporary table.
Temporary table
allows Schema modifications unlike Table variables
.
Temporary tables
are visible in the created routine and also in the child routines. Whereas, Table variables are only visible in the created routine.
Temporary tables
are allowed CREATE INDEXes
whereas, Table variables
aren’t allowed CREATE INDEX
instead they can have index by using Primary Key or Unique Constraint
.