How to Check Whether a Global Temporary Exists in a SQL
Database or not?

Answer Posted / guest

Checking whether a table exists in a Microsoft SQL Server
database is easy. You can use this query:

SELECT 'x'
FROM sysobjects
WHERE type = 'U' and NAME = 'mytable'
But this query will not work while searching for global
temporary tables. Global temporary tables are stored in tempdb.
Use this syntax for the search:

DECLARE @temp_table VARCHAR(100)
SET @temp_table = '##my_temp_table'
IF NOT EXISTS (SELECT 'x'
FROM tempdb..sysobjects
WHERE type = 'U' and NAME = @temp_table)
PRINT 'temp table ' + @temp_table + ' does not exist'

ELSE
PRINT 'temp table ' + @temp_table + ' exists.'
Note: You cannot search for local temporary tables (# prefix
tables) in this way. This is because SQL Server appends a
unique number to the name you supply. For example, if you
specified "#temp," the name in sysobjects would be something
like "#temp____1234."

Is This Answer Correct ?    2 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is an entity-relationship diagram (erd)?

579


Explain the difference between primary keys and foreign keys?

505


List the types of recovery model available in sql server?

499


How many databases can we create in a single server?

187


List the different types of collation sensitivities in sql server?

520






Explain data warehousing in sql server?

597


What can be used instead of trigger?

617


What is SQL Azure Fabric?

92


Which is the main third-party tool used in sql server?

471


How retrieve field names from the table in SQL through JAVA code?

1394


can SSRS reports Cache results?

113


What is the difference between for auto and for nested?

499


Why I am getting "the microsoft .net framework 2.0 in not installed" message?

504


Does partitioning improve performance sql server?

481


What is optimization and its types?

527