TempTable 和 TempTable 的区别?

在 SQLServer 中,#temptable##TempTable有什么区别?

113617 次浏览

#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

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

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

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.

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.

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