Here are some important points to consider while restoring a database backup. Let us first create a sample database named test using the following code
A new database named test will be created in your server and data and log files are created in the H: drive.
Let us assume that you regularly take backup of this database using the below code:
backup database test to disk='h:\test.bak'
Now if you want to create another database or restore this test database to a new database named testing, you can use the following code
restore database testing from disk='h:\test.bak'
However executing the code above will give you the following error
Msg 1834, Level 16, State 1, Line 2
The file 'h:\test_dat.mdf' cannot be overwritten. It is being used by database 'test'.
Msg 3156, Level 16, State 4, Line 2
File 'test_dat' cannot be restored to 'h:\test_dat.mdf'. Use WITH MOVE to identify a valid location for the file.
Msg 1834, Level 16, State 1, Line 2
The file 'h:\test_log.ldf' cannot be overwritten. It is being used by database 'test'.
Msg 3156, Level 16, State 4, Line 2
File 'test_log' cannot be restored to 'h:\test_log.ldf'. Use WITH MOVE to identify a valid location for the file.
Msg 3119, Level 16, State 1, Line 2
Problems were identified while planning for the RESTORE statement. Previous messages provide details.
Msg 3013, Level 16, State 1, Line 2
RESTORE DATABASE is terminating abnormally.
This error occurs because the data and log files are currently being used by the database test. So you need to give different names for those files while restoring, as shown below
restore database testing from disk='h:\test.bak'
with
move 'test_dat' to 'h:\testing.mdf',
move 'test_log' to 'h:\testing.ldf'
The above code will work fine and new database will be created with the name testing.
Just be aware of this point while restoring a backup of existing database!
No comments:
Post a Comment