title: SQL Server, can't check if #temporary table exists, with if else statement
body: <p>I'm creating a Trigger that handles a temp table (#attributeType). Since the trigger can be called more than once, I wanted to be sure and check if the temp table #attributeType is still there.</p>
  
  <p>This is my code in the trigger body that checks for the temp table:</p>
  
  <pre><code>IF OBJECT_ID('tempdb..#attributeType') IS NOT NULL
      BEGIN
          DROP TABLE #attributeType
          SELECT * INTO #attributeType
          FROM attributeType
          PRINT 'IN IF'+   CAST(OBJECT_ID('tempdb..#attributeType') AS NVARCHAR(80))
      END
  ELSE
      BEGIN
          SELECT * INTO #attributeType
          FROM attributeType
          PRINT 'IN ELSE'+  CAST(OBJECT_ID('tempdb..#attributeType') AS NVARCHAR(80))
      END
  </code></pre>
  
  <p>When I test the code by selecting it with F5 I get this error message, stating that the #attributeType temp table exist:</p>
  
  <pre><code>Msg 2714, Level 16, State 1, Line 11
  There is already an object named '#attributeType' in the database.
  </code></pre>
  
  <p>I know that in stored procedures the #tempTables will be deleted as soon as the sp ends, but still I can't understand why my code is wrong? </p>
  
  <p>N.B.
  When I comment out the ELSE block the code works.</p>