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

what is database replicaion? : Sql server database administration

812


let's assume you have data that resides on sql server 6.5. You have to move it sql server 7.0. How are you going to do it? : Sql server database administration

700


What triggers long term care?

714


What is the process of normalising?

751


What is meant by indexing?

661






How many partitions a clustered index has by default in sql server 2012?

850


Write a sql query to delete duplicate records from a table called table1

776


What are “unrepeatable reads”?

940


Are resultset updatable?

700


How to run queries with sql server management studio express?

756


What happens if you are trying to access a schema not owned by you?

716


What are the different acid properties?

804


What do you understand by coalesce in sql server?

722


How can you ensure that the database and sql server based application perform well?

793


What is a document index?

769