Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between #temptable and ##TempTable?

What is the difference between #temptable and ##TempTable in SQL Server?

like image 601
Vignesh Kumar A Avatar asked Jan 09 '14 03:01

Vignesh Kumar A


4 Answers

#table refers to a local temporary table - visible to only the user who created it

##table refers to a global temporary table - visible to all users

like image 164
Rahul Avatar answered Nov 14 '22 15:11

Rahul


#TempTables aren't just local to the User, or connection. They are local to the process that created them and any processes that the creating process spawns. For example if I have the following:

Declare @strDynamicSQL as varchar(8000)

Set @strDynamicSQL = 'Select GetDate() As TheDate Into #TheDateTable'
Execute(@strDynamicSQL)

Select *
From #TheDateTable

I get the following error:

Msg 208, Level 16, State 0, Line 7 Invalid object name '#TheDateTable'.

But if I do:

Declare @strDynamicSQL as varchar(8000)

Create Table #TheDateTable (
    TheDate     DateTime
)

Set @strDynamicSQL = 'Insert Into #TheDateTable Select GetDate() As TheDate'
Execute(@strDynamicSQL)

Select *
From #TheDateTable

I get no errors.

In the first example the Execute statement happens in a spawned process. Since the table is created in that process when it returns that process goes away. And with the process the table is "bye-bye".

In the second example the table is created by the top level process. It's then interacted with in the spawned process. The table is available to the process that it was created in and any process it spawns.

##tables break this. The process a ## table is created in will be the controlling process. The table will not get flagged for removal if this process is still alive even if there are no tasks against that process. Once the process that the ## table was created in goes away, the table is tagged for removal when the last task is executed against it.

Here is a simple way to see it. # tables are available only in the scope of the process that it was created in. ## are available in the same way as any other table except that the existence comes and goes with the process it was created in.

like image 24
Michael Cooper Avatar answered Nov 14 '22 15:11

Michael Cooper


Local temporary tables are visible only to their creators during the same connection to an instance of SQL Server as when the tables were first created or referenced. Local temporary tables are deleted after the user disconnects from the instance of SQL Server. Global temporary tables are visible to any user and any connection after they are created, and are deleted when all users that are referencing the table disconnect from the instance of SQL Server.

Taken from here

More on this

like image 31
SamuraiJack Avatar answered Nov 14 '22 14:11

SamuraiJack


simple way of testing #localtable and ##globaltable

Try this in a different SQL Query Window

create table ##globaltemptable (id int )
go
insert into ##globaltemptable values (1)
go
select * from ##globaltemptable

Try this in a different SQL Query Window

create table #localtemptable (id int )
go
insert into #localtemptable values (1)
go
select * from #localtemptable

Now if you run the select query for the table : #localtemptable in the global window syntax , you will get an error as follows :-

Invalid object name '#localtemptable'.

While you run the select query for the table : ##globaltemptable in any query window of the same session, you will get the query results return.

like image 44
goofyui Avatar answered Nov 14 '22 14:11

goofyui