title: Variables declaration in Create Database Script
body: <p>I have a large database creation query the beginning of which is as follows:</p>
  
  <pre><code>USE Master
  GO
  
  IF EXISTS(SELECT * FROM sys.sysdatabases where name = 'MyDatabase')
      DROP DATABASE MyDatabase
  GO
  CREATE DATABASE MyDatabase
  GO
  USE MyDatabase
  GO
  </code></pre>
  
  <p>I want to declare a variable at the beginning like this:</p>
  
  <pre><code>DECLARE @MainDB VARCHAR(30) = NULL
  USE Master
  GO
  
  IF EXISTS(SELECT * FROM sys.sysdatabases where name = @MainDB)
      DROP DATABASE @MainDB
  GO
  CREATE DATABASE @MainDB
  GO
  USE @MainDB
  GO
  </code></pre>
  
  <p>I would execute this query from the command line with the new database name being assigned using the sqlcmd tool. however sql is telling me that the variable @MainDB is not declared. Is this something I can do? If not how would you recommend I work around this problem?</p>