title: How to check existence of a table from a different sql db?
body: <p>I have db A and db B. At the beginning of a stored procedure I want to back up all rows from <code>B.mytable</code> to <code>B.mytablebackup</code>. The rest of the stored procedure runs against tables on db A (which gathers data and writes it to <code>B.mytable</code>).</p>
  
  <p>So I check to see if <code>B.mytablebackup</code> exists </p>
  
  <pre><code>IF EXISTS(SELECT 1 FROM B.dbo.mytablebackup)
  </code></pre>
  
  <p>and if it does, the stored procedure does an</p>
  
  <pre><code>INSERT INTO B..mytablebackup SELECT * FROM B..mytable
  </code></pre>
  
  <p>If it doesn't exist it does a </p>
  
  <pre><code>SELECT * INTO B..mytablebackup from B..mytable
  </code></pre>
  
  <p>But when I execute the stored procedure I get the error </p>
  
  <blockquote>
    <p>There is already an object named 'mytablebackup' in the database</p>
  </blockquote>
  
  <p>I added a <code>Print</code> statement and execution is taking the "does not exist" branch of the IF.</p>
  
  <p>What am I doing wrong?  </p>