Sql-server – Problem restoring a database in SQL Server

restoresql serversql-server-2008

I am trying to follow the instructions on the following post:

https://stackoverflow.com/questions/10299811/creating-new-database-from-a-backup-of-another-database-on-the-same-server

To summarize; I am building the logical file list then restoring the database:

RESTORE FILELISTONLY
FROM DISK = 'C:\KeltieCochrane_v220120220.BAK';

RESTORE DATABASE KeltieCochrane_v2 
FROM DISK = 'C:\KeltieCochrane_v220120220.BAK'
WITH 
    MOVE 'KeltieCochrane_v2' TO 'C:\Program Files\...\KeltieCochrane_v2.mdf',
    MOVE 'KeltieCochrane_v2' TO 'C:\Program Files\...\KeltieCochrane_v2_1.ldf';

I have no .MDF or .LDF in the target folder and understand that they are contained in the .BAK backup file.

When I restore the database into an empty, new database, it states:

The backup set holds a backup of a database other than the existing 'KCDB'.

When I try to restore using the actual database name in the script (KeltieCochrane_v2) I get There are conflicting file locations specified for the file.

If I try to simply restore the database I get a standard 15105 error and the operating system cannot get the text for this error.

What am I doing wrong?

Best Answer

You're getting that first error because kdcb already exists. And the backed up database is not that same database. If you want to overwrite an existing, separate database (that isn't the same. With the same you don't need to specify replace), you need to include the REPLACE option:

RESTORE DATABASE KeltieCochrane_v2 
FROM DISK = 'C:\KeltieCochrane_v220120220.BAK'
WITH 
MOVE 'KeltieCochrane_v2' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\KeltieCochrane_v2.mdf',
MOVE 'KeltieCochrane_v2' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\KeltieCochrane_v2_1.ldf',
REPLACE

WARNING!!! You need to be very careful and make serious consideration by specifying REPLACE. Here is an exact excerpt from BOL regarding REPLACE:

Replace Option Impact

REPLACE should be used rarely and only after careful consideration. Restore normally prevents accidentally overwriting a database with a different database. If the database specified in a RESTORE statement already exists on the current server and the specified database family GUID differs from the database family GUID recorded in the backup set, the database is not restored. This is an important safeguard.

The REPLACE option overrides several important safety checks that restore normally performs. The overridden checks are as follows:

Restoring over an existing database with a backup taken of another database.

With the REPLACE option, restore allows you to overwrite an existing database with whatever database is in the backup set, even if the specified database name differs from the database name recorded in the backup set. This can result in accidentally overwriting a database by a different database.

Restoring over a database using the full or bulk-logged recovery model where a tail-log backup has not been taken and the STOPAT option is not used.

With the REPLACE option, you can lose committed work, because the log written most recently has not been backed up.

Overwriting existing files.

For example, a mistake could allow overwriting files of the wrong type, such as .xls files, or that are being used by another database that is not online. Arbitrary data loss is possible if existing files are overwritten, although the restored database is complete.