• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

SQL 2005 - check if Temp Table exists

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    SQL 2005 - check if Temp Table exists

    SQL2005 SP2

    Suggections on how I check if a temp table (#messages_temp) exists in a database?

    something like...
    if exists(#messages_temp)
    BEGIN
    DROP #messages_temp
    END
    ELSE
    BEGIN
    CREATE #messages_temp
    END
    www.stormtrack.co.uk - My Stormchasing website.

    #2
    Quick reply, not checked - hope it helps

    USE PUBS
    GO

    CREATE TABLE #some_temp_name(id INT)
    GO

    USE TEMPDB
    GO

    SELECT OBJECT_NAME(OBJECT_ID('#some_temp_name'))

    USE PUBS
    GO


    IF OBJECT_ID('tempdb..#some_temp_name') IS NOT NULL
    PRINT '#some_temp_name exists.'
    ELSE
    PRINT '#some_temp_name does not exist.'
    Last edited by hyperD; 15 April 2008, 12:28.
    If you think my attitude stinks, you should smell my fingers.

    Comment


      #3
      spooky

      I was experimenting around and wrote this bit of SQL not more than an hour ago:
      Code:
      select * into #temp1 
      from  [add your source objects here]
      where 1=2 
      
      select * from tempdb.dbo.syscolumns
      where id = (select id from tempdb.dbo.sysobjects where name like '#temp1%')
      
      drop table #temp1
      Coffee's for closers

      Comment


        #4
        if object_id('tempdb..#temptbl', 'u') is not null
        ...
        @Spacecadet
        You should be using information_schema.tables and not sysobjects
        Last edited by Sir_Edward_Matheson; 15 April 2008, 13:18.

        Comment


          #5
          Originally posted by Sir_Edward_Matheson View Post
          @Spacecadet
          You should be using information_schema.tables and not sysobjects
          you are right, i was just messing around with something though
          Coffee's for closers

          Comment


            #6
            Great I have got it workling - it was actually the double dot .. between the tempDB and the table name that was scuppering me

            Thank's to all that suggest fixes
            www.stormtrack.co.uk - My Stormchasing website.

            Comment

            Working...
            X