How to Check Whether a Global Temporary Exists in a SQL
Database or not?
Answers were Sorted based on User's Feedback
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 return the second 5 rows in ms sql server?
What are the functions in sql server?
How do you delete duplicate records in sql server?
Explain the microsoft sql server delete command?
How to choose all records from the table?
What are recommended options to be used while using db mirroring? : sql server database administration
What is the difference between UNIQUE and DISTINCT keywords in DBMS?
What is a table called, if it has neither cluster nor non-cluster index? What is it used for?
Explain what are various ways to enhance the ssrs report?
How to change the data type of an existing column with "alter table" statements in ms sql server?
How to link tables in sql server?
what operator performs pattern matching?
Oracle (3259)
SQL Server (4518)
MS Access (429)
MySQL (1402)
Postgre (483)
Sybase (267)
DB Architecture (141)
DB Administration (291)
DB Development (113)
SQL PLSQL (3330)
MongoDB (502)
IBM Informix (50)
Neo4j (82)
InfluxDB (0)
Apache CouchDB (44)
Firebird (5)
Database Management (1411)
Databases AllOther (288)