title: SQL - create database and tables in one script
body: <p>Sorry if already asked, but I can't find anything on this.</p>
  
  <p>I am moving something over from MySQL to SQL Server I want to have a .sql file create a database and tables within the database. After working out syntax kinks I have gotten the files to work (almost).</p>
  
  <p>If I run</p>
  
  <pre><code>IF db_id('dbname') IS NULL 
      CREATE DATABASE dbname
  </code></pre>
  
  <p>it works fine, and if I run </p>
  
  <pre><code>CREATE TABLE dbname.dbo.TABLE1 (
  );
  ...
  CREATE TABLE dbname.dbo.TABLEN (
  );
  </code></pre>
  
  <p>it also works fine. But, if I run them in the same file I get this error</p>
  
  <pre><code>Database 'dbname' does not exist
  </code></pre>
  
  <p>Right now, the CREATE TABLE statements are not within the IF statement, which I would like, but I also cannot seem to find the syntax for that. ( { } does not work?)</p>
  
  <p>So my big question is, <strong>how do I ensure a particular command in a .sql file is completed before another in SQL Server?</strong></p>
  
  <p>My second question is, how do I include multiple instructions within an IF clause?</p>
  
  <p>To be clear, I have been running this into sqlcmd.</p>