Difference between #temptable and ##TempTable?

Sql ServerTemp

Sql Server Problem Overview


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

Sql Server Solutions


Solution 1 - Sql Server

#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

Solution 2 - Sql Server

#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.

Solution 3 - Sql Server

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.

https://stackoverflow.com/a/2920883/2064292"> Taken from here

http://www.c-sharpcorner.com/UploadFile/97fc7a/local-and-global-temporary-tables-in-sql-server-2008/"> More on this

Solution 4 - Sql Server

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.

Solution 5 - Sql Server

"#tempTable" denotes Local Temporary Tables

  • It starts with single hash value "#" as the prefix of the table name.

  • A Local Temporary Table is only for the connection in which it was
    created. Each Local Temporary Table has a random value at the end of the table name.

  • A Local Temporary Table is automatically dropped when the existing connection is closed, or the user can explicitly drop it with the following command "drop table #TempTable".

  • If the Temporary Table is created in a Stored Procedure then it is automatically dropped on the completion of the Stored Procedure execution.

  • You can create a Local Temporary Table with the same name but in a different connection, and it is stored with the same name along with various random values.

"##tempTable" denotes Global Temporary Tables

  • It starts with the single hash value "##" as the prefix of the table name and its name is always unique.
  • There is no random number appended to the name.Global Temporary Tables are visible to all connections of SQL Server.
  • Global Temporary Tables are only destroyed when the last connection referencing the table is closed (in which we have created the Global Temporary Table).
  • You can access the Global Temporary Tables from all connections of SQL Server until the referencing connection is open.

Solution 6 - Sql Server

The table will be visible/available up until the process/connection that created it ends and only to the session that created it.

The table is global and will be available to everyone until the process/session that created it is ended.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVignesh Kumar AView Question on Stackoverflow
Solution 1 - Sql ServerRahulView Answer on Stackoverflow
Solution 2 - Sql ServerMichael CooperView Answer on Stackoverflow
Solution 3 - Sql ServerSamuraiJackView Answer on Stackoverflow
Solution 4 - Sql ServergoofyuiView Answer on Stackoverflow
Solution 5 - Sql ServerSamim HussainView Answer on Stackoverflow
Solution 6 - Sql ServeragohilView Answer on Stackoverflow