SQL Server SSMS - Restore database failed because .mdf cannot be overwritten

4nkexdtk  于 2023-04-28  发布在  其他
关注(0)|答案(2)|浏览(117)

I am trying to restore a DB from the backup. Suddenly, it is giving me the following error everytime while trying to restore.
Restore of database 'Something_Secret' failed.
The file 'C:\Some\Path\Something_Secret.mdf' cannot be overwritten. It is being used by database 'Something_Secreter'.

How to get rid this of error?

o3imoua4

o3imoua41#

Don't use the UI for this, since this won't be the last thing it gets wrong for you. Learn the RESTORE DATABASE command .

RESTORE DATABASE Something_Secret_NewName
  FROM DISK = 'D:\Baseline\Path\To\Something_Secret.bak'
  WITH REPLACE, RECOVERY, 
  MOVE 'Something_Something_Logical_Primary'
    TO 'C:\Program Files\...\Something_Secret_NewName.mdf',
  MOVE 'Something_Something_Logical_Log'
    TO 'C:\Program Files\...\Something_Secret_NewName.ldf';

If you want more specific guidance, show the output of:

RESTORE FILELISTONLY FROM DISK = 'D:\Baseline\Path\To\Something_Secret.bak';
mjqavswn

mjqavswn2#

Here is a solution for those who would like to continue using the UI (I'm using version 15.+). Once you select the backup media, click the "Files" link in the menu on the left. This will display a dialog that allows you to change the names of the db's files. In the "Restore As" field change the name of the files to match the name of your database.

Alternatively, you could add a level to the directory after the "DATA" directory where the new directory is the name of your database and leave the actual files names as they are.

相关问题