What is the difference between temp table and table variable?

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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain the benefits of user-defined functions?

530


What is the difference between sql server 2000 object owner and sql server 2005 schema? : sql server database administration

591


How to use "begin ... End" statement structures in ms sql server?

524


what is the Ticketing tool used in Wipro technologies at Bangalore...???

7476


Define the one-to-one relationship while designing tables.

511






Do you know nested transaction?

512


What is it’s similarity with sql server?

143


Explain few examples of stored procedure over triggers?

523


What is the difference between TRUNCATE and DROP?

633


what are the different stages of Report Processing?

97


What is 1nf normalization form?

599


When do you think a developer should use sql server-based cursors?

523


Can you explain important index characteristics?

527


Explain filtered indexes benefits?

594


What is repeatable read?

542