Force a SQL Restore

Really tired of having to google this and piece it together every time I want it, so I’m posting it here.

<span class="kwrd">alter</span> <span class="kwrd">database</span> DbName
    <span class="kwrd">set</span> single_user
    <span class="kwrd">with</span> <span class="kwrd">rollback</span> <span class="kwrd">immediate</span>
<span class="kwrd">go</span>

<span class="kwrd">restore</span> <span class="kwrd">database</span> DbName
    <span class="kwrd">from</span> <span class="kwrd">disk</span> = <span class="str">'path/to/backup.bak'</span>
    <span class="kwrd">with</span> <span class="kwrd">file</span> = 1, replace

Notables:

  • Works best to run against the master database.
  • The file = 1 part may need to be changed if there are multiple backups in the file and the one you want isn’t the first one.
This entry was posted in CodeMinder, SQL. Bookmark the permalink.