title: SQL Server - Create temp table if doesn't exist
body: <p>In my SQL Server 2012 environment, I've created a series of stored procedures that pass pre-existing temporary tables among themselves (I have tried different architectures here, but wasn't able to bypass this due to the nature of the requirements / procedures).</p>
  
  <p>What I'm trying to do is to, within a stored procedure check if a temporary table has already been created and, if not, to create it.</p>
  
  <p>My current SQL looks as follows:</p>
  
  <pre><code>IF OBJECT_ID('tempdb..#MyTable') IS NULL
      CREATE TABLE #MyTable
      (
          Col1 INT,
          Col2 VARCHAR(10)
          ...
      );
  </code></pre>
  
  <p>But when I try and run it <strong>when the table already exists</strong>, I get the error message </p>
  
  <blockquote>
    <p>There is already an object named '#MyTable' in the database</p>
  </blockquote>
  
  <p>So it seems it doesn't simply ignore those lines within the If statement.</p>
  
  <p>Is there a way to accomplish this - create a temp table if it doesn't already exist, otherwise, use the one already in memory?</p>
  
  <p>Thanks!</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>For whatever reason, following @RaduGheorghiu's suggestion from the comments, I found out that the system creates a temporary table with a name along the lines of <code>dbo.#MyTable________________________________________________0000000001B1</code></p>
  
  <p>Is that why I can't find it? Is there any way to change that? This is new to me....</p>