title: Check: does table exist in SQL Server database?
body: <p>I have an installation script containing the following:</p>
  
  <pre><code>IF NOT EXISTS(
      SELECT * FROM INFORMATION_SCHEMA.TABLES 
      WHERE TABLE_CATALOG = 'mydatabasename' 
        AND TABLE_SCHEMA  = 'dbo' 
        AND TABLE_NAME    = 'sometablename' 
        AND TABLE_TYPE    = 'BASE TABLE'
  )
  CREATE TABLE dbo.sometablename ...
  </code></pre>
  
  <p>Problem is that </p>
  
  <ul>
  <li>I have to allow the user to select a different database name</li>
  <li>I cannot use any placeholders ("variables") in the script.</li>
  <li>the database server may of course contain a different database which already contains a table <code>sometablename</code></li>
  </ul>
  
  <p>The installer does a <code>USE mydatabasename</code> before the script is executed; and the installer allows to select a variable database name for this. However, I can't use that variable inside the script, because every replacement in the SQL script is already done at build time of the installer.</p>
  
  <p>So, how can I check whether the used database already contains table <code>sometablename</code>?</p>