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 |
What is raid? : SQL Server Architecture
Explain “@@rowcount” and “@@error” in sql server?
How to find the last update record in SQL Server?
What is the difference between Stored Procedures and triggers?
How to use column default values in insert statements in ms sql server?
What are defaults? Is there a column to which a default can't be bound?
How to create a stored procedure with a statement block in ms sql server?
What is the difference between views and stored procedures? Can we have input parameters for views?
How we can compare two database data?
What is Pointer ?
3 Answers Cap Gemini, CarrizalSoft Technologies,
How do I repair damaged sql server mdf file database? In previous day my mdf file has got damage due to unknown reasons then I used dbcc chekcdb command but it failed, MDF file is important for me, I don’ know that how to get back mdf file data. Please anyone suggest me?
What is difference between drop truncate and delete?