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
What is precedence constraint?
Difference Between ORDER BY Clause and GROUP BY Clause in SQL?
What is tablesample?
What do you mean by an execution plan? Why is it used?
Define indexes?
How to delete existing rows in a table?
How to create function without parameter in sql server?
Can a unique index be created on a column, which contains null?
how do you test proper tcp/ip configuration windows machine? : Sql server database administration
Can we do dml on views?
How to implement one-to-one, one-to-many and many-to-many relationships while designing tables?
Explain query editor regions
What is page-level compression?
Why truncate is ddl?
Can two different columns be merged into single column? Show practically?