SQL-Server: Cannot be overwritten. It is being used by database

database-restoresql server

RESTORE DATABASE Tes
FROM DISK = '{7522204E-0256-47B3-9864-137D6D1FD449}6' 
WITH MOVE 'Test' TO 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\Tes.mdf',
MOVE 'Test_log' TO 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\Tes.ldf'

The above restore command is working fine for new dbs, But few old databases it showing a errors like

The file 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\db1.mdf' cannot be overwritten. It is being used by database 'windb'.
Msg 3156, Level 16, State 4, Line 1
File 'windb' cannot be restored to 'C:\Program Files (x86)\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\db1.mdf'. Use WITH MOVE to identify a valid location for the file

I found that only dbs having more than one ndf files having this issues ..

Any solutions this issue ?

Best Answer

Try to restore the database from the Restore Database Wizard. For that, in the Object Explorer, right-click on Databases -> select Restore Databases.... In the window that appears, set the necessary details for the Source and the Destination. And in the left pane, either select Options or Files, depending on the SSMS version you have.

In the table that appears, find the column Restore As. In each row, give a new file name, instead of the name that shows. For example, in your case it might be showing the file names as something similar to <Folder Path>\DATA\Tes.mdf and <Folder Path>\DATA\Tes.ldf. Give a different name other than Tes. (A name that does not already exist in the DATA folder).

Note: The file names do not have anything to do with the database name, but it will easy referencing later on to give a meaningful name, in case you have to backup or look for the files.