How should I find the temp tables are exists in database? - Community Credit Forums
in

    Community Credit Forums

How should I find the temp tables are exists in database?

Last post Thu, Feb 28 2008 9:44 AM by SatheeshBabu. 0 replies.
Page 1 of 1 (1 items)
Sort Posts: Previous Next
  • Thu, Feb 28 2008 9:44 AM

    How should I find the temp tables are exists in database?

    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

    Filed under:
Page 1 of 1 (1 items)
Powered by Community Server (Commercial Edition), by Telligent Systems