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

Answers were Sorted based on User's Feedback



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

Answer / 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

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

Answer / ish

use tempdb

sp_tables '##%'

Is This Answer Correct ?    3 Yes 1 No

Post New Answer

More SQL Server Interview Questions

What are the types of processing and explain each? : sql server analysis services, ssas

0 Answers  


What are built in functions?

0 Answers  


What is a transactions?

0 Answers  


I have to display ten columns values from diffrent ten tables. how many joins are require?

10 Answers   CarrizalSoft Technologies, HCL,


Differences between Standby Vs No recovery?

2 Answers   Wipro,






What is attribute hierarchy? : sql server analysis services, ssas

0 Answers  


What do you understand by the analysis services in sql server?

0 Answers  


how many layers of tcp/ip protocol combined of? : Sql server database administration

0 Answers  


What are logical database components? : SQL Server Architecture

0 Answers  


What languages bi uses to achieve the goal?

0 Answers  


Do you know spatial data types - geometry and geography in sql server 2008?

0 Answers  


What is sqlservr.exe - process - sql server (sqlex?press)?

0 Answers  


Categories