Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between TEMPORARY TABLE and TABLE VARIABLE in SQL 2008?

What is the difference between:

CREATE TABLE #temp ( [ID] INT)

INSERT INTO #temp
SELECT ...

and

DECLARE @temp TABLE ( [ID] INT)

INSERT @temp
SELECT ...

in SQL Server 2008?

like image 596
abatishchev Avatar asked Oct 20 '09 21:10

abatishchev


People also ask

What is the difference between temporary table and table variable?

A Temp table is easy to create and back up data. Table variable involves the effort when you usually create the normal tables. Temp table result can be used by multiple users. Table variable can be used by the current user only.

What is temp table and table variable in SQL?

Temporary Tables are physically created in the tempdb database. These tables act as the normal table and also can have constraints, index 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 batch.

Is table variable better than temp table?

Table Variables. As we can see from the results above a temporary table generally provides better performance than a table variable. The only time this is not the case is when doing an INSERT and a few types of DELETE conditions.

Which is better temp table or table variable in SQL Server?

As far as performance is concerned table variables are useful with small amounts of data (like only a few rows). Otherwise a SQL Server temp table is useful when sifting through large amounts of data. So for most scripts you will most likely see the use of a SQL Server temp table as opposed to a table variable.


4 Answers

Temporary tables are like ordinary tables in most characteristics, except they go into TempDB instead of the current Database, and they dissapear after limited scope, (depending on whether they are session based or global Temp Tables. But all changes to data in Temp tables is logged to the transaction log, with all the performance implications that that entails. otoh, you can also add as many indices or views, or triggers, or whatever else you want to a temp table exactly as you would to a ordinary table.

Table variables are a kind of short-cut in-memory table (they also use temp DB). Changes to them are not logged (this improves performance). But you can only get one index on them, (because indices cannot be created after the initial declaration statement, the only index you can create on a table variable is the one that can be included in the initial table variable declaration...

   Declare @Tab Table (myKey integer Primary Key Not Null, data varchar(20)) 

Because of these characteristics, temp tables are better choice for large tables, (wide and with many rows), and/or that will undergo more than one access pattern during their lifetime, whilst table variables are best when you need a very narrow table (keys only table, or key with only one data column), which will always be accessed by that indexed key...

like image 167
Charles Bretana Avatar answered Oct 13 '22 00:10

Charles Bretana


This is a pretty good reference on the different temp tables

Temp Tables vs Variables

like image 36
CSharpAtl Avatar answered Oct 12 '22 23:10

CSharpAtl


  1. There is no log for table variables
  2. Table variables have only local scope (you cannot access the same table variable from different procedures)
  3. Procedures with temporary tables cannot be pre-compiled

For more details see this topic.

like image 39
Manu Avatar answered Oct 13 '22 00:10

Manu


  1. Table variables have a well defined scope. They will be cleared automatically at the end of the batch (i.e current batch of statements) where as temporary table will be visible to current session and nested stored procedures. Global Temporary table will be visible to the all the sessions.

  2. Table variables are created using Declare statement. We can't create table variable using statement

    select * into @tableVariableName
    

    But we can create temporary table using Create table statement as well as statement

    select * into #tempTableName
    
  3. In SQL Server 2008 onwards we can pass the table variable as a parameter to the stored procedures. But we can't pass the temporary table as a parameter to the stored procedure.

  4. We can use the table variable inside the UDF (user defined function) but we can't use the temporary table inside the UDF.

like image 21
kuttychutty Avatar answered Oct 13 '22 00:10

kuttychutty