What is the difference between temp table and table variable?

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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between delete and truncate statements?

563


What are constraints in microsoft sql server?

553


What is the difference between having clause and where clause in sql server?

566


how would you troubleshoot blocking? : Sql server database administration

514


Why is sql server log file full?

530






How to identify current user in ssrs report?

238


What is analysis service repository?

583


What is Lock table in SQL?

997


Explain aggregate functions?

552


what is a deadlock? : Sql server database administration

492


How do indexes help, types?

546


How to add an address record into adventureworkslt?

562


You have a stored procedure, which execute a lengthy batch job. This stored procedure is called from a trigger you do not want to slow the data entry process you do not want trigger to wait for this batch job to finish before it completes itself what you can do to speed up the process?

645


How to delete duplicate records based on single column from a table?

599


What are the differences between user defined functions and stored procedures?

529