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 is dbcc updateusage?

0 Answers  


This question asked during interview, 2) At the end of each month, a new table is created for each bank that contains monthly metrics consolidated at the account level. The table naming convention is bankX_YYYYMM where X represents the numeric designation of the bank and YYYYMM indicates the 4 digit year and 2 digit month. The tables contain the following fields: name data type description account text account number registered boolean indicates whether the account is registered num_trans integer number of transactions made during the time period spend numeric(9,2) total spend during the time period a) Write a SQL query that will display the total number of transactions and total spend for "Bank1" during the 4th quarter of 2009. b) Write a SQL query that will display the total number of transactions and total spend at "Bank1" and "Bank2", broken out by registered vs. non-registered accounts, during January 2010 not sure what is correct answer and how to solve?

0 Answers   TCS,


How to download microsoft sql server 2005 express edition?

0 Answers  


How to add an address record into adventureworkslt?

0 Answers  


What does asynchronous call backs means?

0 Answers   Alcatel-Lucent,


What are the differences between sql server and mysql.

0 Answers  


What is a ddl statement?

0 Answers  


What are the advantages to use stored procedures?

0 Answers   Ernst Young,


What is the difference between lock, block and deadlock? : sql server database administration

0 Answers  


Explain temporary table vs table variable by using cursor alternative?

0 Answers  


how many non clustered index in sql server 2008,2010,2012

2 Answers   Accenture,


How many columns can exist together per table?

0 Answers   Cap Gemini,


Categories