Link to home
Start Free TrialLog in
Avatar of MargusLehiste
MargusLehiste

asked on

How can you check if a Global Temporary table exists ?

How can you check if a Global Temporary table exists and if it doesnt - create this table.

You can't have multiple global temporary table with the same name - right?
(although multiple users can create local temporary tables with the same name...)
ASKER CERTIFIED SOLUTION
Avatar of ram2098
ram2098

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ram2098
ram2098

Also, you cannot have the multiple Global Temporary tables with the same name.
Or a little shorter:

If Object_ID('tempdb..##table_name') Is Null
  Create Table ##table_name (col1 int)

Avatar of MargusLehiste

ASKER

kselvia - in 'tempdb..##table_name' what are those 2 dots about?
Or, a little more portable:

If Not Exists(Select * From TempDB.Information_Schema.Tables Where Table_Name = '##Table_Name')
  Create Table ##table_name(Col1.Int)

This is guaranteed to work in future versions as well.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think Sjoerd was refering to Ram2098's answer rathe than mine. Yes, if you need to reference sysobjects, it is better to reference information_schema tables.  Sysobjects may not be supported in the future.