What is the difference between #temptable
and ##TempTable
in 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
#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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With