title: Microsoft SQL Server, restore a backup of a database with one command
body: <p>When we copy a database down from production, we make a backup of the database, zip it up and copy the backup down. Then we have to restore using the SQL Server GUI, which involves navigating through several menus and windows. As far as I know, you can not do this with SQL Server's built in stored procedures because you may not know the <code>logical filename</code> of the database (which is required to restore). So doing this via query consists of the following:</p>
  
  <pre><code>RESTORE FILELISTONLY
  FROM DISK = 'C:\backup_of_production_database.bak'
  GO
  </code></pre>
  
  <p>The above provides the logical file names from the backup file, you then have to use these logical names in the next query...</p>
  
  <pre><code>RESTORE DATABASE NewDevelopmentDatabase
  FROM DISK = 'C:\backup_of_production_database.bak'
  WITH MOVE 'YourMDFLogicalName' TO 'C:\mssql\data\DataYourMDFFile.mdf',
  MOVE 'YourLDFLogicalName' TO 'C:\mssql\data\DataYourLDFFile.mdf'
  </code></pre>
  
  <p>As you can see this seems inefficient because you must manually enter the logical file names into the next query.</p>
  
  <p>You can find my solution to this problem as an answer below.</p>