What is the difference between temp table and table variable?
Answers were Sorted based on User's Feedback
Answer / balasubramaiam s( ramco system
Table variables are Transaction neutral. They are variables
and thus aren't bound to a transaction.
Temp tables behave same as normal tables and are bound by
transactions.
BEGIN TRAN
declare @var table (id int, data varchar(20) )
create table #temp (id int, data varchar(20) )
insert into @var
select 1, 'data 1' union all
select 2, 'data 2' union all
select 3, 'data 3'
insert into #temp
select 1, 'data 1' union all
select 2, 'data 2' union all
select 3, 'data 3'
select * from #temp
select * from @var
ROLLBACK
select * from @var
if object_id('tempdb..#temp') is null
select '#temp does not exist outside the transaction'
We see that the table variable still exists and has all
it's data unlike the temporary table that doesn't exists
when the transaction rollbacked.
| Is This Answer Correct ? | 13 Yes | 0 No |
Answer / jerry joseph
SYNTAX
Temporary Tables: CREATE table #T (…)
Table Variables: DECLARE @T table (…)
Table Variables are out of scope of the transaction mechanism.
If you make changes to a Temp Table inside a Transaction and
Rollback the Transaction those changes will be lost.
Changes made to Table Variable inside a Transaction will
remain even if you Rollback the Transaction
Any procedure with a temporary table cannot be pre-compiled
An execution plan of procedures with table variables can be
statically compiled in advance
Table Variables exist only in the same scope as variables.
They are not visible in inner stored procedures and in
exec(string) statements
Table variables are in-memory structures that may work from
2-100 times faster than temp tables.
Access to table variables gets slower as the volume of data
they contain grows.
At some point, table variables will overflow the available
memory and that kills the performance.
Use table variables only when their data content is
guaranteed not to grow unpredictably; the breaking size is
around several thousand records.
For larger data volumes, use temp tables with clustered
indexes.
| Is This Answer Correct ? | 13 Yes | 4 No |
Answer / yugal
1) Table variables don't participate in transactions,
logging or locking. This means they're faster as they don't
require the overhead, but conversely you don't get those
features.
2) Procedure with a temporary table cannot be pre-compiled,
while an execution plan of procedures with table variables
can be statically compiled in advance. Pre-compiling a
script gives a major advantage to its speed of execution.
This advantage can be dramatic for long procedures, where
recompilation can be too pricy
3) Table variables exist only in the same scope as
variables. Contrary to the temporary tables, they are not
visible in inner stored procedures and in exec (string)
statements. Also, they cannot be used in an insert/exec
statement.
4) You can create a temp table using SELECT INTO, which can
be quicker to write (good for ad-hoc querying) and may
allow you to deal with changing datatypes over time, since
you don't need to define your temp table structure upfront.
5) You cannot truncate a table variable but temporary table
can be truncated.
6) You cannot drop a table variable when it is no longer
necessary—you just need to let it go out of scope but
temporary table can be dropped.
7) Table variables cannot be altered after they have been
declared but temporary table can be altered.
| Is This Answer Correct ? | 6 Yes | 3 No |
Answer / nalla
Temporary variables logs very less into transaction log, so
compare to Temp tables, temp varaibles are good in
performance wise.
| Is This Answer Correct ? | 2 Yes | 0 No |
Answer / rathikapoobalan
1. If we use Temporary Table in a stored procedure, We
should drop it at the end.
It is not necessary in the case of Table variable.
2. In Table variable we can hold maximum of 20000 records
only. If it exceeds, we can use temporary table
| Is This Answer Correct ? | 11 Yes | 16 No |
Answer / nandkumar karlekar
we can create the index on temp table which leads better
performance in join and searching .
temp table can be used in exec statements.
for Table variable above two point are not applicable
| Is This Answer Correct ? | 0 Yes | 7 No |
Answer / samba shiva reddy . m
temp tables physically exists in temp DB
but temp variables does not exists.
| Is This Answer Correct ? | 4 Yes | 13 No |
Answer / prakash
Temp table create automatically when you create any data
table create you must mention there create temporary table.
Table variable is must declare in procedure when you crate
procedure.
| Is This Answer Correct ? | 1 Yes | 13 No |
What are the differences between clustered and non-clustered index?
What does indexation mean?
How is foreign key related to primary key?
Explain how dts is used to extract, transform and consolidate data?
What is mean by clustered index and non clustered index, give syntax of creation? : sql server database administration
Can a trigger be created on a view?
how to delete duplicate rows from table
How to list all objects in a given schema?
How to know whether our backup is succesfully taken?
What is normalization and what are the advantages of it?
how to update a null value field in sql server eg a table contains 3 fields id,name,salary and 3 records salary of 1 record is null i want update the nullfield 111 arun 300 112 ddd 200 113 ttt null i want to update table with add 100 to every record include null after updation the recrds should be 111 arun 400 112 ddd 300 113 ttt 100
What is explicit mode in sql server?
Oracle (3259)
SQL Server (4518)
MS Access (429)
MySQL (1402)
Postgre (483)
Sybase (267)
DB Architecture (141)
DB Administration (291)
DB Development (113)
SQL PLSQL (3330)
MongoDB (502)
IBM Informix (50)
Neo4j (82)
InfluxDB (0)
Apache CouchDB (44)
Firebird (5)
Database Management (1411)
Databases AllOther (288)