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

How to get a list all databases on the sql server?

784


What happens when converting big values to numeric data types?

760


What are the different index configurations a table can have?

698


Define inner join? Explain with an example?

713


Explain what is it unwise to create wide clustered index keys?

717


Explain what is the main purpose of having conversation group?

703


why would you call update statistics? : Sql server database administration

748


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

1589


Why do you need a sql server?

700


What are the disadvantages of indexes?

706


What is Fragmentation and Defragmentation? For 32GB Table,How can we do the fragmentation?

4027


what are user defined datatypes and when you should go for them? : Sql server database administration

764


How can you insert values in multiple rows using one Insert statement?

809


Give a example to search fr a string in all stored procedure in sql server.

709


How to configure odbc dsn with different port numbers?

764