SQL Server with Mr. Denny

Aug 11 2008   11:00AM GMT

Back To Basics: The RESTORE DATABASE Command

Denny Cherry Denny Cherry Profile: Denny Cherry

The restore database command is what is used to recover a database which has been backed up using the BACKUP DATABASE command.  The syntax of the RESTORE DATABASE command is very similar to the BACKUP DATABASE command in many respects.

 You start with where you are restoring the database from.

RESTORE DATABASE MyDatabase FROM DISK='E:\Backups\MyDatabase.bak'

Then if you need to move the physical files to another location because your disks are laid out differently, or because your folder layout is different you can add a MOVE command for each file you want to move.  For each MOVE command you specify the local file name, and the new physical file name.

RESTORE DATABASE MyDatabase FROM DISK='E:\Backups\MyDatabase.bak'
WITH MOVE 'MyDatabase_Data' TO 'D:\MSSQL\MSSQL.1\MSSQL\Data\MyDatabase_Data.mdf',
  MOVE 'MyDatabase_Log' TO 'D:\MSSQL\MSSQL.1\MSSQL\Data\MyDatabase_Log.ldf'

When restoring the database, many people think that you have to create the database first.  You do not.  When restoring a database through the UI (Enterprise Manager, or SQL Server Management Studio) if you create the database first, it will then be selectable in the drop down menu.  Even with using the UI, creating the database first is optional as you can simply type in the name of the new database in the UI.

If you are restoring from a striped database backup then you will need to specify the name of all the members of the strip.

RESTORE DATABASE MyDatabase FROM DISK='E:\Backups\MyDatabase1.bak',
  DISK='E:\Backups\MyDatabase2.bak'
WITH MOVE 'MyDatabase_Data' TO 'D:\MSSQL\MSSQL.1\MSSQL\Data\MyDatabase_Data.mdf',
  MOVE 'MyDatabase_Log' TO 'D:\MSSQL\MSSQL.1\MSSQL\Data\MyDatabase_Log.ldf'

If you need to restore a differential or log backups after you restore your full backup you will want to place you backup with the NORECOVERY flag. This will tell SQL Server not to complete the recovery process, and to leave the database in an unusable state. This will allow you to continue the restore process. Once the database has been switched into a writable state you won’t be able to restore any transaction logs to the database without restoring from the full backup again.

RESTORE DATABASE MyDatabase FROM DISK='E:\Backups\MyDatabase.bak'
WITH MOVE 'MyDatabase_Data' TO 'D:\MSSQL\MSSQL.1\MSSQL\Data\MyDatabase_Data.mdf',
  MOVE 'MyDatabase_Log' TO 'D:\MSSQL\MSSQL.1\MSSQL\Data\MyDatabase_Log.ldf',
  NO RECOVERY

Denny

2  Comments on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Gaurangbtripathi
    Hi Denny, Nice Post.. I would like to know something more about restore database with move files. Which file name I should use in WITH MOVE ‘MyDatabase_Data’ TO statement. Actually my company assigned me a task of bulk restore from files. I don't know the original file names when they were backed up. So which file name I should write in that statement ? Or any other option for bulk restore ? Thanks in advance
    0 pointsBadges:
    report
  • Denny Cherry
    In that case use the RESTORE FILELISTONLY command to get a list of the database files which are contained within the database backup. [CODE]RESTORE FILELISTONLY from disk='D:YourBackup.bak'[/CODE] Denny
    66,295 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: