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
How to get a list all databases on the sql server?
What happens when converting big values to numeric data types?
What are the different index configurations a table can have?
Define inner join? Explain with an example?
Explain what is it unwise to create wide clustered index keys?
Explain what is the main purpose of having conversation group?
why would you call update statistics? : Sql server database administration
How retrieve field names from the table in SQL through JAVA code?
Why do you need a sql server?
What are the disadvantages of indexes?
What is Fragmentation and Defragmentation? For 32GB Table,How can we do the fragmentation?
what are user defined datatypes and when you should go for them? : Sql server database administration
How can you insert values in multiple rows using one Insert statement?
Give a example to search fr a string in all stored procedure in sql server.
How to configure odbc dsn with different port numbers?