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?
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.
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.
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.
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.
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...
This is a pretty good reference on the different temp tables
Temp Tables vs Variables
For more details see this topic.
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.
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
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.
We can use the table variable inside the UDF (user defined function) but we can't use the temporary table inside the UDF.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With