For a normal SQL table we will do the check in sysobjects similar to below,
IF EXISTS (SELECT 1
FROM sysobjects
WHERE xtype='u' AND name='tablename')
Select ‘Exists’
We cannot do the same for temp tables and it can be done by,
IF OBJECT_ID('tempdb..#temp') IS NOT NULL
PRINT 'Exists'
ELSE
PRINT 'Not Exists'
OBJECT_ID function will return the object id of the table and if it is not null then we can confirm its existence.
Thought of sharing this simple tip!
Some info about temp table:
• When we create a new temp table it is stored in “TempDB” database by default.
• Since it will be created for every session there will be an identifier that gets appended to every temp table name.
Happy Coding!!
Regards,
Satheesh
www.codedigest.com
www.satheeshbabu.com