How to restore to a different database in sql server?
You can create a new db then use the "Restore Wizard" enabling the Overwrite option or:
View the contents of the backup file:
RESTORE FILELISTONLY FROM DISK='c:\your.bak'
note the logical names of the .mdf & .ldf from the results, then:
RESTORE DATABASE MyTempCopy FROM DISK='c:\your.bak'
WITH
MOVE 'LogicalNameForTheMDF' TO 'c:\MyTempCopy.mdf',
MOVE 'LogicalNameForTheLDF' TO 'c:\MyTempCopy_log.ldf'
This will create the database MyTempCopy
with the contents of your.bak
.
(Don't create the MyTempCopy, it's created during the restore)
Example (restores a backup of a db called 'creditline' to 'MyTempCopy'):
RESTORE FILELISTONLY FROM DISK='e:\mssql\backup\creditline.bak'
>LogicalName
>--------------
>CreditLine
>CreditLine_log
RESTORE DATABASE MyTempCopy FROM DISK='e:\mssql\backup\creditline.bak'
WITH
MOVE 'CreditLine' TO 'e:\mssql\MyTempCopy.mdf',
MOVE 'CreditLine_log' TO 'e:\mssql\MyTempCopy_log.ldf'
>RESTORE DATABASE successfully processed 186 pages in 0.010 seconds (144.970 MB/sec).
SQL Server 2008 R2:
For an existing database that you wish to "restore: from a backup of a different database follow these steps:
- From the toolbar, click the Activity Monitor button.
- Click processes. Filter by the database you want to restore. Kill all running processes by right clicking on each process and selecting "kill process".
- Right click on the database you wish to restore, and select Tasks-->Restore-->From Database.
- Select the "From Device:" radio button.
- Select ... and choose the backup file of the other database you wish to restore from.
- Select the backup set you wish to restore from by selecting the check box to the left of the backup set.
- Select "Options".
- Select Overwrite the existing database (WITH REPLACE)
- Important: Change the "Restore As" Rows Data file name to the file name of the existing database you wish to overwrite or just give it a new name.
- Do the same with the log file file name.
- Verify from the Activity Monitor Screen that no new processes were spawned. If they were, kill them.
- Click OK.
For SQL Server 2012, using Sql Server Management Studio, I found these steps from the Microsoft page useful to restore to a different database file and name: (ref: http://technet.microsoft.com/en-us/library/ms175510.aspx)
Note steps 4 and 7 are important to set so as not to overwrite the existing database.
To restore a database to a new location, and optionally rename the database
- Connect to the appropriate instance of the SQL Server Database Engine, and then in Object Explorer, click the server name to expand the server tree.
- Right-click Databases, and then click Restore Database. The Restore Database dialog box opens.
On the General page, use the Source section to specify the source and location of the backup sets to restore. Select one of the following options:
Database
Select the database to restore from the drop-down list. The list contains only databases that have been backed up according to the msdb backup history.
Note If the backup is taken from a different server, the destination server will not have the backup history information for the specified database. In this case, select Device to manually specify the file or device to restore.
Device
Click the browse (...) button to open the Select backup devices dialog box. In the Backup media type box, select one of the listed device types. To select one or more devices for the Backup media box, click Add. After you add the devices you want to the Backup media list box, click OK to return to the General page. In the Source: Device: Database list box, select the name of the database which should be restored.
Note This list is only available when Device is selected. Only databases that have backups on the selected device will be available.
- In the Destination section, the Database box is automatically populated with the name of the database to be restored. To change the name of the database, enter the new name in the Database box.
- In the Restore to box, leave the default as To the last backup taken or click on Timeline to access the Backup Timeline dialog box to manually select a point in time to stop the recovery action.
- In the Backup sets to restore grid, select the backups to restore. This grid displays the backups available for the specified location. By default, a recovery plan is suggested. To override the suggested recovery plan, you can change the selections in the grid. Backups that depend on the restoration of an earlier backup are automatically deselected when the earlier backup is deselected.
- To specify the new location of the database files, select the Files page, and then click Relocate all files to folder. Provide a new location for the Data file folder and Log file folder. Alternatively you can keep the same folders and just rename the database and log file names.