What is the difference between temp table and table variable?

Answer Posted / 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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How to use order by with union operators in ms sql server?

753


What is transaction server consistency?

715


What is exporting and importing utility?

772


Can a stored procedure call itself or a recursive stored procedure? How many levels of sp nesting is possible?

795


What is database black box testing?

768






What is a constant or literal in ms sql server?

784


What are the ways available in sql server to execute sql statements?

777


How do I find the default sql server instance?

695


What is the difference between stored procedure and functions?

774


Do you know what is user defined datatypes and when you should go for them?

706


What are the steps you must follow to hide sql server instances?

703


What is an identity column in insert statements?

744


What is query optimizer in sql server?

731


Explain few examples of RDBMS?

766


What the different components of Replication and what is their use?

761