mdf file cannot be overwritten when restoring a database in SQL Server
Solution 1:
If you restore a database, SQL Server will, by default, attempt to restore all the data and log files to their original locations. Since those original locations are still in use by the original database ("A"), the restore fails. You need to use the WITH MOVE clause to specify new locations for all the files in the database.
RESTORE DATABASE B FROM DISK = 'A.bak'
WITH MOVE 'DataFileLogicalName' TO 'C:\SQL Directory\DATA\B.mdf',
MOVE 'LogFileLogicalName' TO 'C:\SQL Directory\DATA\B.ldf',
REPLACE --Needed if database B already exists
Something like that anyway. Use RESTORE FILELISTONLY FROM DISK... to see the logical filenames in the backup if necessary.
Solution 2:
WITH MOVE
/ MOVE
is the right solution in T-SQL
.
By the way, if you want to use the GUI you can go to Files and rename:
- a.MDF
- a.NDF
- a.LDF
to
- b.MDF
- b.NDF
- b.LDF